What are the paging methods in SQL server?
This article talks about the paging method of SQL server, using the SQL server 2012 version. In the following, pageIndex is used to represent the number of pages, and pageSize represents the records contained on one page. And the following involves specific examples, set the query page 2, each page contains 10 records.
First of all, let’s talk about the difference between SQL server’s paging and MySQL’s paging. MySQL’s paging can be completed directly by using limit (pageIndex-1) and pageSize. However, SQL server does not have the limit keyword, only something like limit. The top keyword. So paging is more troublesome.
There are only four types of SQL server paging that I know: triple loop; using max (primary key); using the row_number keyword, offset/fetch next keyword (summarized by collecting other people’s methods on the Internet , there should be only these four methods at present, other methods are based on this deformation).
Partial records of the student table to be queried
#Method 1: Triple loop
Idea
First take the first 20 page, then reverse order, and take the first 10 records in reverse order, so that you can get the data required for paging, but the order is reversed. You can then return it in reverse order, or you can stop sorting and hand it over directly to the front-end for sorting.
There is another method that can be considered to be of this type. I won’t put the code here. I will just talk about the idea, which is to first query the first 10 records, and then use not in to exclude these 10 records, and then Inquire.
Code implementation
-- 设置执行时间开始,用来查看性能的 set statistics time on ; -- 分页查询(通用型) select * from (select top pageSize * from (select top (pageIndex*pageSize) * from student order by sNo asc ) -- 其中里面这层,必须指定按照升序排序,省略的话,查询出的结果是错误的。 as temp_sum_student order by sNo desc ) temp_order order by sNo asc -- 分页查询第2页,每页有10条记录 select * from (select top 10 * from (select top 20 * from student order by sNo asc ) -- 其中里面这层,必须指定按照升序排序,省略的话,查询出的结果是错误的。 as temp_sum_student order by sNo desc ) temp_order order by sNo asc ;
Query results and time
Method 2: Use max (Primary key)
First, top the first 11 row records, then use max (id) to get the largest id, and then re-query the first 10 records in this table, but you must add conditions, where id>max( id).
Code implementation
set statistics time on; -- 分页查询(通用型) select top pageSize * from student where sNo>= (select max(sNo) from (select top ((pageIndex-1)*pageSize+1) sNo from student order by sNo asc) temp_max_ids) order by sNo; -- 分页查询第2页,每页有10条记录 select top 10 * from student where sNo>= (select max(sNo) from (select top 11 sNo from student order by sNo asc) temp_max_ids) order by sNo;
Query results and time
set statistics time on;
-- 分页查询(通用型)
select top pageSize *
from (select row_number()
over(order by sno asc) as rownumber,*
from student) temp_row
where rownumber>((pageIndex-1)*pageSize);
set statistics time on;
-- 分页查询第2页,每页有10条记录
select top 10 *
from (select row_number()
over(order by sno asc) as rownumber,*
from student) temp_row
where rownumber>10;
Copy after login
Query results and time
set statistics time on; -- 分页查询(通用型) select top pageSize * from (select row_number() over(order by sno asc) as rownumber,* from student) temp_row where rownumber>((pageIndex-1)*pageSize); set statistics time on; -- 分页查询第2页,每页有10条记录 select top 10 * from (select row_number() over(order by sno asc) as rownumber,* from student) temp_row where rownumber>10;
set statistics time on;
-- 分页查询(通用型)
select * from student
order by sno
offset ((@pageIndex-1)*@pageSize) rows
fetch next @pageSize rows only;
-- 分页查询第2页,每页有10条记录
select * from student
order by sno
offset 10 rows
fetch next 10 rows only ;
Copy after login
offset A rows, discard the first A record, fetch next B rows only, read backward B data. Results and running time
set statistics time on; -- 分页查询(通用型) select * from student order by sno offset ((@pageIndex-1)*@pageSize) rows fetch next @pageSize rows only; -- 分页查询第2页,每页有10条记录 select * from student order by sno offset 10 rows fetch next 10 rows only ;
create procedure paging_procedure ( @pageIndex int, -- 第几页 @pageSize int -- 每页包含的记录数 ) as begin select top (select @pageSize) * -- 这里注意一下,不能直接把变量放在这里,要用select from (select row_number() over(order by sno) as rownumber,* from student) temp_row where rownumber>(@pageIndex-1)*@pageSize; end -- 到时候直接调用就可以了,执行如下的语句进行调用分页的存储过程 exec paging_procedure @pageIndex=2,@pageSize=10;
mysql tutorial"
The above is the detailed content of What are the paging methods in SQL server?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The DATETIME data type is used to store high-precision date and time information, ranging from 0001-01-01 00:00:00 to 9999-12-31 23:59:59.99999999, and the syntax is DATETIME(precision), where precision specifies the accuracy after the decimal point (0-7), and the default is 3. It supports sorting, calculation, and time zone conversion functions, but needs to be aware of potential issues when converting precision, range and time zones.

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

SQL IF statements are used to conditionally execute SQL statements, with the syntax as: IF (condition) THEN {statement} ELSE {statement} END IF;. The condition can be any valid SQL expression, and if the condition is true, execute the THEN clause; if the condition is false, execute the ELSE clause. IF statements can be nested, allowing for more complex conditional checks.

SQL paging is a technology that searches large data sets in segments to improve performance and user experience. Use the LIMIT clause to specify the number of records to be skipped and the number of records to be returned (limit), for example: SELECT * FROM table LIMIT 10 OFFSET 20; advantages include improved performance, enhanced user experience, memory savings, and simplified data processing.

Common SQL optimization methods include: Index optimization: Create appropriate index-accelerated queries. Query optimization: Use the correct query type, appropriate JOIN conditions, and subqueries instead of multi-table joins. Data structure optimization: Select the appropriate table structure, field type and try to avoid using NULL values. Query Cache: Enable query cache to store frequently executed query results. Connection pool optimization: Use connection pools to multiplex database connections. Transaction optimization: Avoid nested transactions, use appropriate isolation levels, and batch operations. Hardware optimization: Upgrade hardware and use SSD or NVMe storage. Database maintenance: run index maintenance tasks regularly, optimize statistics, and clean unused objects. Query

The DECLARE statement in SQL is used to declare variables, that is, placeholders that store variable values. The syntax is: DECLARE <Variable name> <Data type> [DEFAULT <Default value>]; where <Variable name> is the variable name, <Data type> is its data type (such as VARCHAR or INTEGER), and [DEFAULT <Default value>] is an optional initial value. DECLARE statements can be used to store intermediates

Methods to judge SQL injection include: detecting suspicious input, viewing original SQL statements, using detection tools, viewing database logs, and performing penetration testing. After the injection is detected, take measures to patch vulnerabilities, verify patches, monitor regularly, and improve developer awareness.

There are two ways to deduplicate using DISTINCT in SQL: SELECT DISTINCT: Only the unique values of the specified columns are preserved, and the original table order is maintained. GROUP BY: Keep the unique value of the grouping key and reorder the rows in the table.
