Home Database Mysql Tutorial sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查

sqlserver关于分页存储过程的优化【让数据库按我们的意思执行查

Jun 07, 2016 pm 06:02 PM
Pagination stored procedure

先来对比两段分页SQL,假设条件:news表有15万记录,NewsTypeId=10有9万记录,当前查询NewsTypeID=10。那么,你会认为哪个SQL效率会高呢?

代码如下:
--代码一DECLARE @cc INT
SELECT NewsId,ROW_NUMBER() OVER(ORDER BY SortNum DESC) AS RowIndex INTO #tb FROM news WITH(NOLOCK) WHERE NewsTypeId=@NewsTypeId AND IsShow=1
SET @cc = @@ROWCOUNT
SELECT n.* FROM news AS n WITH(NOLOCK), #tb As t WHERE t.RowIndex>@PageIndex*@PageSize AND t.RowIndexSELECT @cc
DROP TABLE #tb

代码如下:
--代码二
DECLARE @cc INT
SELECT NewsId,ROW_NUMBER() OVER(ORDER BY SortNum DESC) AS RowIndex INTO #tb FROM news WITH(NOLOCK) WHERE NewsTypeId=@NewsTypeId AND IsShow=1
SET @cc = @@ROWCOUNT
SELECT NewsId INTO #tb2 FROM #tb As t WHERE t.RowIndex>@PageIndex*@PageSize AND t.RowIndexSELECT * FROM news WITH(NOLOCK) WHERE NewsId IN (SELECT * FROM #tb2)
SELECT @cc
DROP TABLE #tb
DROP TABLE #tb2

答案是代码二远远高于代码一。在代码一中加粗代码的操作会引起整表扫描,因为数据库引擎在认为WHERE表达式中满足条件记录大于一定阀值的时候,就不再去进行查询优化,而直接使用表扫描。看执行信息,:
表 'news'。扫描计数 1,逻辑读取 342 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
(98361 行受影响)
(1 行受影响)
(40 行受影响)
表 '#tb________________________________________00000004C024'。扫描计数 1,逻辑读取 257 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
表 'news'。扫描计数 1,逻辑读取 2805 次,物理读取 0 次,预读 235 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
(1 行受影响)
(1 行受影响)

原本,我想的执行计划,加粗部分的代码应该是聚焦索引查找,这样性能就提高很多。看代码二:
表 'news'。扫描计数 1,逻辑读取 342 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
(98361 行受影响)
(1 行受影响)
表 '#tb____________________________________00000004BEEF'。扫描计数 1,逻辑读取 257 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
(40 行受影响)
(1 行受影响)
(40 行受影响)
表 'news'。扫描计数 0,逻辑读取 131 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
表 '#tb2___________________________________00000004BEF0'。扫描计数 1,逻辑读取 2 次,物理读取 0 次,预读 0 次,lob 逻辑读取 0 次,lob 物理读取 0 次,lob 预读 0 次。
(1 行受影响)
(1 行受影响)

很明显,代码二与代码一中的IO操作数大大降低。且代码一随着@PageIndex越来越大,效率会越来越低;但代码二的效率不会随@PageIndex变化而改变。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP development: How to implement table data sorting and paging functions PHP development: How to implement table data sorting and paging functions Sep 20, 2023 am 11:28 AM

PHP development: How to implement table data sorting and paging functions In web development, processing large amounts of data is a common task. For tables that need to display a large amount of data, it is usually necessary to implement data sorting and paging functions to provide a good user experience and optimize system performance. This article will introduce how to use PHP to implement the sorting and paging functions of table data, and give specific code examples. The sorting function implements the sorting function in the table, allowing users to sort in ascending or descending order according to different fields. The following is an implementation form

Steps and precautions for implementing batch updates using Oracle stored procedures Steps and precautions for implementing batch updates using Oracle stored procedures Mar 08, 2024 pm 04:12 PM

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

How to delete stored procedure in MySQL How to delete stored procedure in MySQL Sep 05, 2023 am 10:25 AM

MySQL methods for deleting stored procedures include using the DROP PROCEDURE statement, using MySQL Workbench, and using command line tools. Detailed introduction: 1. Use the DROP PROCEDURE statement. The steps are to first open the MySQL client or use any tool that supports MySQL, then connect to your MySQL database, and finally execute the following SQL statement to delete the stored procedure; 2. Use MySQL Workbench to delete Stored procedures and so on.

How to use JavaScript to implement table paging function? How to use JavaScript to implement table paging function? Oct 20, 2023 pm 06:19 PM

How to use JavaScript to implement table paging function? With the development of the Internet, more and more websites use tables to display data. In some cases where the amount of data is large, the data needs to be displayed in pages to improve user experience. This article will introduce how to use JavaScript to implement table paging function and provide specific code examples. 1. HTML structure First, we need to prepare an HTML structure to host tables and paging buttons. We can use <tab

Oracle stored procedure: Implementation method to determine whether a table exists Oracle stored procedure: Implementation method to determine whether a table exists Mar 08, 2024 pm 09:18 PM

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t

Detailed explanation of the principle of MyBatis paging plug-in Detailed explanation of the principle of MyBatis paging plug-in Feb 22, 2024 pm 03:42 PM

MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis

How to use Layui to develop a data display page with paging function How to use Layui to develop a data display page with paging function Oct 24, 2023 pm 01:10 PM

How to use Layui to develop a data display page with paging function Layui is a lightweight front-end UI framework that provides simple and beautiful interface components and a rich interactive experience. During development, we often encounter situations where we need to display large amounts of data and perform paging. The following is an example of a data display page with paging function developed using Layui. First, we need to introduce Layui related files and dependencies. Add the following code to the <head> tag of the html page

Vue component practice: paging component development Vue component practice: paging component development Nov 24, 2023 am 08:56 AM

Vue component practice: Introduction to paging component development In web applications, the paging function is an essential component. A good paging component should be simple and clear in presentation, rich in functions, and easy to integrate and use. In this article, we will introduce how to use the Vue.js framework to develop a highly customizable paging component. We will explain in detail how to develop using Vue components through code examples. Technology stack Vue.js2.xJavaScript (ES6) HTML5 and CSS3 development environment

See all articles