Paged query of the database

Source: Internet
Author: User
Tags null null rowcount

1. The limit usage of MySQL

When we use query statements, we often have to return the first few or the middle of a few rows of data, this time what to do? Don't worry, MySQL has provided us with such a feature.

SQL Code

1 SELECT * from table LIMIT [offset,] Rows | rows offset offset

The LIMIT clause can be used to force the SELECT statement to return the specified number of records. LIMIT accepts one or two numeric parameters. parameter must be an integer constant. Given two parameters, the first parameter specifies the offset of the first return record row, and the second parameter specifies the maximum number of rows to return records. The initial record line offset is 0 (not 1): MySQL also supports syntax: LIMIT # offset # In order to be compatible with PostgreSQL.

Sql Code

1Mysql> SELECT *  from TableLIMIT5,Ten;//Retrieving record lines6- the  2   3 //In order to retrieve all the record rows from an offset to the end of the recordset, you can specify the second parameter as-1:4Mysql> SELECT *  from TableLIMIT the,-1;//Retrieving record lines the-Last . 5   6 //If only one parameter is given, it indicates the maximum number of record rows returned:7Mysql> SELECT *  from TableLIMIT5;//Before retrieving5a record line8   9 //In other words, limit n is equivalent to limit0N.
performance analysis of 2.Mysql paged query statements

MySQL paging SQL statement, if compared with MSSQL's top syntax, then MySQL's limit syntax to appear a lot more elegant. It's a natural thing to use it for paging.

2.1 Most basic paging methods:

SQL Code

1 SELECT ... from ... WHERE ... ORDER by ... LIMIT ...

In the case of small amounts of data, such SQL is sufficient, and the only problem to be aware of is to ensure that the index is used.

For example, if the actual SQL is similar to the following statement, it is better to create a composite index on the category_id, id two column:

SQL Code

SELECT *  from WHERE = 123 ORDER  by  - Ten  

2.2 Sub-Query paging method:

As the amount of data increases, the number of pages is increasing, and looking at the next few pages of SQL can be similar:

SQL code

1 SELECT *  from WHERE = 123 ORDER  by 10000 Ten

Word, the more backward the page, the offset of the limit statement will be greater, the speed will be significantly slower.

At this point, we can improve paging efficiency by sub-query, roughly as follows:

SQL Code

1 SELECT *  from WHERE  >= 2  (SELECT from articles  WHERE & = 123 ORDER  by 10000 1 Ten

2.3JOIN Paging mode

SQL Code

1 SELECT *  from' Content ' asT12 JOIN(SELECTId from' Content 'ORDER  byIddescLIMIT ". ($page-1)*$pagesize. ",1) asT23 WHERET1.id<=T2.idORDER  byT1.iddescLIMIT $pagesize;

after my testing, the efficiency of join paging and sub-query paging is basically on one level, and the time consumed is basically the same.

Explain SQL statements:

ID select_type table Type possible_keys key Key_len ref rows Extra

1 PRIMARY <derived2> system NULL NULL NULL NULL 1

1 PRIMARY T1 range PRIMARY PRIMARY 4 NULL 6264 Using where

2 DERIVED content index NULL PRIMARY 4 null 27085 Using Index

----------------------------------------

Why is that? Because subqueries are done on the index, and ordinary queries are done on a data file, the index file is generally much smaller than the data file, so it is more efficient to operate.

You can actually use a similar strategy mode to deal with paging, such as judging if it is within 100 pages, using the most basic paging method, more than 100 pages, then use the sub-query paging method.

3.Oracle paged Query statement

Oralce Database

Retrieving N records from the database table, starting with article M Records

SQL code
    1. SELECT * FROM (select ROWNUM r,t1.* from table name T1 where ROWNUM < M + N) T2
    2. where T2.R >= M

For example, from table sys_option (primary key to sys_id), 20 records are retrieved starting from the 10th record, with the following statement:

SQL code
    1. SELECT * FROM (select ROWNUM r,t1.* from sys_option where ROWNUM <) T2
    2. Where T2. R >=
4.MSSQLSERVER paged Query statement

SQL Server mainly uses SELECT top statement paging, for specific scenarios, refer to

-------------------------------------

Paging Scenario One: (using not in and select top paging)

Statement form:

SQL code
    1. SELECT TOP Ten *
    2. From TestTable
    3. WHERE (ID not in
    4. (SELECT TOP ID
    5. From TestTable
    6. ORDER by ID))
    7. ORDER by ID
SQL code
    1. SELECT TOP Page Size *
    2. From TestTable
    3. WHERE (ID not in
    4. (SELECT TOP Page Size * Number of pages ID
    5. From table
    6. ORDER by ID))
    7. ORDER by ID
    8. SELECT TOP Page Size *
SQL code
    1. From TestTable
    2. WHERE (ID >
    3. (SELECT MAX (ID)
    4. From (SELECT TOP Page Size * Number of pages ID
    5. From table
    6. ORDER by ID) (as T) )
    7. ORDER by ID

------------------------------------

Paging Scenario Two: (using ID greater than how much and select top paging)

Statement form:

SQL code
    1. select top 10 *    
    2. FROM&NBSP;TESTTABLE&NBSP;&NBSP;&NBSP;
    3. where  (id >   
    4. ( select max (id)    
    5. from  (select top 20  id   
    6. from testtable   
    7. order by id)   as t))    
    8. order  by id   

-------------------------------------

Paging Scenario Three: (Paging with SQL cursor stored procedures)

SQL code
  1. Create procedure Xiaozhengge
  2. @sqlstr nvarchar (4000), --query string
  3. @currentpage int, --page n
  4. @pagesize int --Number of rows per page
  5. As
  6. SET NOCOUNT on
  7. DECLARE @P1 int, --P1 is the ID of the cursor
  8. @rowcount int
  9. EXEC sp_cursoropen @P1 output, @sqlstr, @scrollopt =1, @ccopt =1,@[email protected] output
  10. Select Ceiling (1.0* @rowcount/@pagesize) as total number of pages--, @rowcount as rows, @currentpage as current page
  11. Set @currentpage = (@currentpage-1) * @pagesize +1
  12. exec sp_cursorfetch @P1, @currentpage, @pagesize
  13. EXEC sp_cursorclose @P1
  14. SET NOCOUNT off

Other scenarios: If you don't have a primary key, you can use a temporary table, or you can do it with scenario three, but the efficiency will be low.

When tuning is recommended, the query efficiency increases with the primary key and index.

Using SQL Query Analyzer to show comparisons: My conclusion is:

Paging Scenario Two: (using ID greater than how much and select top paging) The most efficient, need to splice SQL statements

Paging Scenario One: (using not and select top paging) second, the need to splice SQL statements

Paging Scenario Three: (Paging with SQL cursor stored procedures) is the least efficient, but the most common

In the actual situation, the specific analysis

Paged query of the database

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.