Query optimization and paging algorithm program is a massive database (IV)

getdate()) order by fariqi desc

select O.gid, O.mid, O.title, O.fadanwei, O.fariqi from TGongwen O @ indextable Where did O.gid = t.nid

and t.id>@PageLowerBound and t.id< order by t.id

end

set nocount off

  Above stored procedure using the latest technology of SQL SERVER - table variables. It should be said that this process is a very good memory paging stored procedure. Of course, in this process, you can also watch the variable which is written temporary table: CREATE TABLE #Temp. But obviously, in SQL SERVER, the temporary tables are of no use table variables fast. So when I started using this stored procedure, I feel very good, better than the original speed of ADO. But then, I found a better way than this method.

  I have seen the Internet essay in "a method of recording the n-th to m-th article is removed from the data table", reads as follows:

Remove the m-th to n-th article from the article publish recorded in the table:
the SELECT the TOP-n + m *. 1
the FROM publish
the WHERE (the NOT ID the IN
    (n the TOP the SELECT-ID. 1
     the FROM publish))

id for the keyword publish tables

  I had read this article, really lifted the spirit of that idea very well. Wait until later, I was in for office automation system (ASP.NET + C # + SQL SERVER), when suddenly remembered the article, I think if this statement is modified look, this could be a very good paging stored procedure. So I went online to find the full article, I did not expect, did not find the article, but according to this statement to find an article written by a paging stored procedure, the stored procedure is currently the more popular a paging stored procedure, I am very sorry not first to this paragraph transformed into a stored procedure:

PROCEDURE pagination2 the CREATE
(
@SQL nvarchar (4000), - sorting statements without SQL statement
@Page int, - page
@RecsPerPage int, - receiving a number of records per
@ID VARCHAR (255), - need to sort Unique ID number of
@Sort VARCHAR (255) - sort fields and rules
)
the AS

DECLARE @Str nVARCHAR(4000)

SET @Str='SELECT TOP '+CAST(@RecsPerPage AS VARCHAR(20))+' * FROM (

PRINT @Str

EXEC sp_ExecuteSql @Str
GO

  In fact, the above statement can be simplified as follows:

SELECT TOP page size *

FROM Table1

WHERE (ID NOT IN

(SELECT TOP page size, number of pages id *

FROM 表

ORDER BY id))

ORDER BY ID

  But this stored procedure has a fatal flaw, it is that it contains the word NOT IN. Although I can transform it as:

SELECT TOP page size *

FROM Table1

WHERE not exists

(select * from (select top (页大小*页数) * from table1 order by id) b where b.id=a.id )

order by id

  That is, with not exists instead of not in, but we have already talked to the efficiency of the two is actually no difference.

  Even so, with a combination of NOT IN TOP of this method is faster than using a cursor to a number.

  Although with not exists not save efficiency of storage process, but the use of SQL SERVER the TOP keyword is a very wise choice. Because the ultimate goal is to optimize the paging to avoid large set of records, and we have already mentioned in the previous TOP advantage, it can achieve control of the amount of data through TOP.

  In the paging algorithm, the key factors that affect our query speed two points: TOP and NOT IN. TOP can improve our query speed, but NOT IN query will slow down our speed, so to increase the speed of our entire paging algorithm, it is necessary to reinvent NOT IN, to replace it with other methods.

  We know that almost any field, we can extract the maximum or minimum value in a field by max (field) or min (field), so if this field is not repeated, then you can make use of these non-repeating fields max min or as a watershed, making it a separate paging algorithm in each page reference. Here, we can complete this mission with the operator ">" or "<" sign, in line with SARG the query form. Such as:

Select top 10 * from table1 where id>200

  Then there is the paging scheme as follows:

select top page size *

from table1

where id>

(select max (id) from

(Select top ((p -1) * page size) id from table1 order by id) as T

)

order by id

  When selecting a value that is not repeated, but also easy to distinguish the size of the column, we often choose the primary key. The following table lists the author has used 10 million office automation system data tables, in order to GID (GID is the primary key, but not the clustered index.) As the sort column, extraction gid, fariqi, title field, respectively, to the first , 10,100,500,1000,1, 100,000, 250,000, 500,000, for example, the execution speed of more than three kinds of tests paging scheme :( unit: ms)

Page
Scheme 1
Scheme 2
Scheme 3

1
60
30
76

10
46
16
63

100
1076
720
130

500
540
12943
83

1000
17110
470
250

10000
24796
4500
140

100 000
38326
42283
1553

250 000
28140
128 720
2330

500 000
121 686
127 846
7168


  From the table, we can see three stored procedures when executing commands on page 100 the following pages, all can be trusted, the speed is very good. But the first embodiment, after performing the above tab page 1000, the speed drop down. The second program is about more than speed after the execution Page 10000 start falling down. The third option has always been no major downward trend, momentum is still very adequate.

  After determining the third paging scheme, we could then write a stored procedure. We all know that SQL SERVER stored procedure is a precompiled SQL statement, its efficiency through the implementation of efficiency than the WEB page came SQL statement to be higher. The following stored procedure contains not only paging scheme, but also to determine whether the total number of pages according to statistics came parameter.

- Get data specified page

CREATE PROCEDURE pagination3

@tblName varchar(255), -- 表名

@strGetFields varchar (1000) = '*', - the need to return to the column

@fldName varchar (255) = '', - sorting field name

@PageSize int = 10, - page size

@PageIndex int = 1, - p

@doCount bit = 0, - the total number of records returned, non-zero value

@OrderType bit = 0, - set the sort type, a non-zero value in descending order

@strWhere varchar (1500) = '' - the query criteria (Note: Do not add where)

AS

declare @strSQL varchar (5000) - the main statement

declare @strTmp varchar (110) - temporary variable

declare @strOrder varchar (400) - type of ordering


if @doCount != 0

begin

if @strWhere !=''

set @strSQL = "select count(*) as Total from [" + @tblName + "] where "

else

set @strSQL = "select count(*) as Total from [" + @tblName + "]"

end

- the code above means that if @doCount passed over is not zero, on the implementation of statistical aggregates. All of the following code is @doCount is 0

else

begin


if @OrderType != 0

begin

set @strTmp = "<(select min"

set @strOrder = " order by [" + @fldName +"] desc"

- If @OrderType not 0, on the implementation of descending, the sentence is very important!

end

else

begin

set @strTmp = ">(select max"

set @strOrder = " order by [" + @fldName +"] asc"

end


if @PageIndex = 1

begin

if @strWhere != ''

set @strSQL = "select top " + str(@PageSize) +" "+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder

else

set @strSQL = "select top " + str(@PageSize) +" "+ " from ["+ @tblName + "] "+ @strOrder

- If the first page on the implementation of the above code, it will speed up execution

end

else

begin

- The following code gives @strSQL to really execute SQL code

set @strSQL = "select top " + str(@PageSize) +" "+ " from ["

+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder


if @strWhere != ''

set @strSQL = "select top " + str(@PageSize) +" "+ " from ["

+ @tblName + "] where [" + @fldName + "]" + @strTmp + "(["

+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["

+ @fldName + "] from [" + @tblName + "] where " + @strWhere + " "

+ @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrder


Article Source: http://www.diybl.com/course/7_databases/sql/msshl/2007614/52157_4.html

Reproduced in: https: //www.cnblogs.com/200831856/articles/1381733.html

Guess you like

Origin blog.csdn.net/weixin_33972649/article/details/93711304