Under normal circumstances, the SQL Server server will create an appropriate index for a frequently used table
This will greatly improve the data retrieval speed of the database itself, and the indexing method will not be detailed.
If you need to return a large amount of data, from several hundred rows to tens of thousands of rows, or even hundreds of thousands of rows
At this time, you will find that the response speed is getting slower and slower, and even the response timeout error occurs.
In order to solve this big data request problem, we have to use the paging mode.
In this regard, JDBC is much more powerful. It can send the specified number of rows along with the SQL request to the SQL server. In this way, only the paging data is returned. The principle of JDBC is unclear, however, in actual use, the speed is still very fast.
If you cannot use JDBC, the most common method is the stored procedure!
Before writing this paging storage, I used a lot of related information on the Internet.Article, You can search by Keyword: SQL Server Page
They mainly use the top method in SQL and require an ID column for The retrieved data structure. If there is no ID column or a joint primary key, it will be very troublesome. In addition, the original SQL search part of the application needs to be modified in many places, resulting in a large workload.
Therefore, before writing this storage, I must be compatible with the original SQL script to the maximum extent.
After one afternoon, I worked with one of my colleagues (absolutely a master) to find out the following ideas:
1. Determine the storage input parameters:
1) SQL script. This parameter receives complete and correct SQL retrieval text. You can directly input the SQL script written in the original application.
2) The data capacity on each page is the number of data records on a page.
3) Current page number
2. Determine the paging mechanism:
1) execute the input SQL script and generate a temporary table
2) modify the structure of the temporary table and add an ID column Field
3) Calculate the record range in the specified page number based on the field of the identification column, and return
4) the total number of returned data entries for the client to display by PAGE
Based on the above ideas, compile the following general paging stored procedures:
CopyCode The Code is as follows: [Code]
-- // ==================================
-- // SQL Server General paging Stored Procedure
-- // Author: netwild
-- // Date: 2010/07/22
-- // Email: netwild@163.com
-- // QQ: 52100641)
-- // ==================================
Set quoted_identifier on
Go
Set ansi_nulls on
Go
Create proc execbypage
@ Sqlquery varchar (2000), -- // input parameter: SQL search statement or table name
@ Pagesize int, -- // input parameter: number of records per page
@ Pageindex int -- // input parameter: Current page number
As
Set nocount on
Set ansi_warnings off
Declare @ tmptablename varchar (50)
Set @ tmptablename = '# tb1516 _' + Replace (cast (newid () as varchar (40), '-', '') -- // generate a random temporary table name
Declare @ subindex int
Set @ subindex = charindex ('from', @ sqlquery)
If (@ subindex> 0)
Begin -- // standard search statement with from
Declare @ sqlquery1 varchar (2000)
Declare @ sqlquery2 varchar (2000)
Set @ sqlquery1 = substring (@ sqlquery, 1, @ subindex-1)
Set @ sqlquery2 = substring (@ sqlquery, @ subindex, Len (@ sqlquery ))
Set @ sqlquery = @ sqlquery1 + ', identity (numeric, 1, 1) as id1516 into' + @ tmptablename + ''+ @ sqlquery2
End
Else -- // table name without from
Begin
Set @ sqlquery = 'select *, identity (numeric, 1, 1) as id1516 into '+ @ tmptablename + 'from' + @ sqlquery
End
Exec (@ sqlquery) -- // create and initialize temporary table data
Declare @ indexstart varchar (20), @ indexend varchar (20)
Set @ indexstart = cast (@ pageIndex-1) * @ pagesize + 1 as varchar (20) -- // data start row ID
Set @ indexend = cast (@ pageindex * @ pagesize as varchar (20) -- // data end row ID
Exec ('select * from' + @ tmptablename + 'where id1516 between' + @ indexstart + 'and' + @ indexend) -- // retrieve the page data
Exec ('select max (id1516) as recordcount from '+ @ tmptablename) -- // extract the total number of entries
Exec ('drop table' + @ tmptablename) -- // delete a temporary table
Go
Set quoted_identifier off
Go
Set ansi_nulls on
Go
[/Code]