SQL Server optimization on paging stored procedures "Let the database execute the query plan as we intended" _mssql

Source: Internet
Author: User
Tags rowcount
Copy Code code as follows:

--code One declare @cc INT
SELECT Newsid,row_number () over (order by Sortnum DESC) as RowIndex into #tb from news with (NOLOCK) WHERE newstypeid= @NewsT Ypeid and Isshow=1
SET @cc = @ @ROWCOUNT
SELECT n.* from News as N with (NOLOCK) #tb as T WHERE t.rowindex> @PageIndex * @PageSize and t.rowindex<= (@PageIndex +1 ) * @PageSize and T.newsid=n.newsid
SELECT @cc
DROP TABLE #tb

Copy Code code as follows:

--code Two
DECLARE @cc INT
SELECT Newsid,row_number () over (order by Sortnum DESC) as RowIndex into #tb from news with (NOLOCK) WHERE newstypeid= @NewsT Ypeid and Isshow=1
SET @cc = @ @ROWCOUNT
SELECT NewsId into #tb2 #tb as T WHERE t.rowindex> @PageIndex * @PageSize and t.rowindex<= (@PageIndex + 1) *@ PageSize
SELECT * FROM News with (NOLOCK) WHERE NewsId in (SELECT * from #tb2)
SELECT @cc
DROP TABLE #tb
DROP TABLE #tb2

The answer is that code two is much higher than code one. The operation of bold code in code one causes an entire table scan, because the database engine stops query optimization when it considers that the where expression satisfies a condition record greater than a certain threshold, and uses table scans directly. Look at the execution information:
Table ' News '. Scan count 1, logical read 342 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
(98361 rows affected)
(1 rows affected)
(40 rows affected)
Table ' #tb________________________________________00000004C024 '. Scan count 1, logical read 257 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' News '. Scan count 1, logical read 2,805 times, physical read 0 times, read 235 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
(1 rows affected)
(1 rows affected)

Originally, I think of the execution plan, the bold part of the code should be focused index lookup, so performance is improved a lot. See Code two:
Table ' News '. Scan count 1, logical read 342 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
(98361 rows affected)
(1 rows affected)
Table ' #tb____________________________________00000004BEEF '. Scan count 1, logical read 257 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
(40 rows affected)
(1 rows affected)
(40 rows affected)
Table ' News '. Scan count 0, logical read 131 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
Table ' #tb2___________________________________00000004BEF0 '. Scan count 1, logical read 2 times, physical read 0 times, read 0 times, LOB logic read 0 times, lob physical read 0 times, lob read 0 times.
(1 rows affected)
(1 rows affected)

Obviously, the number of IO operands in code two and code one is significantly reduced. and code one as @pageindex more and more, the efficiency will be lower and higher, but the efficiency of code II will not change with @pageindex changes.
Related Article

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.