SQL techniques for efficient paging (using Oracle as an example)

Source: Internet
Author: User

The general paging method is like the following:The so-called "three-layer nesting" Statement),

 
 
  1. SELECT *  
  2. FROM   (SELECT a.*  
  3.               ,rownum rn  
  4.         FROM   (SELECT * FROM table_name) a  
  5.         WHERE  rownum <= 40)  
  6. WHERE  rn >= 21 

Performance problems may occur when a record with more than 1 million rows in a data table is displayed. The solution proposed by someone is to use the index. First, scan the index to find the rowid, and then use the rowid to find the required records. What if there is no index? Taking Oracle as an example, the following is a more general and efficient way of writing:

 
 
  1. SELECT *  
  2. FROM   <your table name> t  
  3. WHERE  ROWID IN (SELECT rwd  
  4.                  FROM   (SELECT t.rowid rwd  
  5.                                ,rownum  n  
  6.                          FROM   <your table name> t  
  7.                          WHERE  rownum <= 1000000 + 10)  
  8.                  WHERE  n >= 1000000) 

The optimization idea is to scan rowid only when jumping to the 1st million location, avoiding scanning all fields. When the number of fields is large, the speed will increase by 10 times. In this example, it takes less than 1 second to run on a common device. This method still has problems in the tens of millions or hundreds of millions of users, because the number of cycles has not been reduced. If a field such as rwd exists, the speed can be ensured through indexes, partitions, and other means. However, if we say it back, we will display hundreds of thousands of records on pages. Can we better solve the business objectives it wants to achieve?

This article is from the iData blog, please be sure to keep this source http://idata.blog.51cto.com/4581576/1121453

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.