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
- SELECT * FROM (select ROWNUM r,t1.* from table name T1 where ROWNUM < M + N) T2
- 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
- SELECT * FROM (select ROWNUM r,t1.* from sys_option where ROWNUM <) T2
- 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
- SELECT TOP Ten *
- From TestTable
- WHERE (ID not in
- (SELECT TOP ID
- From TestTable
- ORDER by ID))
- ORDER by ID
SQL code
- SELECT TOP Page Size *
- From TestTable
- WHERE (ID not in
- (SELECT TOP Page Size * Number of pages ID
- From table
- ORDER by ID))
- ORDER by ID
- SELECT TOP Page Size *
SQL code
- From TestTable
- WHERE (ID >
- (SELECT MAX (ID)
- From (SELECT TOP Page Size * Number of pages ID
- From table
- ORDER by ID) (as T) )
- ORDER by ID
------------------------------------
Paging Scenario Two: (using ID greater than how much and select top paging)
Statement form:
SQL code
- select top 10 *
- FROM&NBSP;TESTTABLE&NBSP;&NBSP;&NBSP;
- where (id >
- ( select max (id)
- from (select top 20 id
- from testtable
- order by id) as t))
- order by id
-------------------------------------
Paging Scenario Three: (Paging with SQL cursor stored procedures)
SQL code
- Create procedure Xiaozhengge
- @sqlstr nvarchar (4000), --query string
- @currentpage int, --page n
- @pagesize int --Number of rows per page
- As
- SET NOCOUNT on
- DECLARE @P1 int, --P1 is the ID of the cursor
- @rowcount int
- EXEC sp_cursoropen @P1 output, @sqlstr, @scrollopt =1, @ccopt =1,@[email protected] output
- Select Ceiling (1.0* @rowcount/@pagesize) as total number of pages--, @rowcount as rows, @currentpage as current page
- Set @currentpage = (@currentpage-1) * @pagesize +1
- exec sp_cursorfetch @P1, @currentpage, @pagesize
- EXEC sp_cursorclose @P1
- 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