Home Database Mysql Tutorial 利用自定义分页技术提高数据库性能

利用自定义分页技术提高数据库性能

Jun 07, 2016 pm 05:45 PM
Pagination use performance technology improve database customize

Web应用程序是显示数据库中数据的一个非常好的方法,通过它,你可以把业务复杂,并有访问和安全规则的数据库数据以一种简单、直观的方式向用户提供查询和更新的功能。用户判断数据库应用程序一个很常用的标准就是处理数据的快慢。许多Web页面都向用户提供了

     Web应用程序是显示数据库中数据的一个非常好的方法,通过它,你可以把业务复杂,并有访问和安全规则的数据库数据以一种简单、直观的方式向用户提供查询和更新的功能。用户判断数据库应用程序一个很常用的标准就是处理数据的快慢。许多Web页面都向用户提供了多种可搜索的列表显示来有效地定位记录的位置,一个比较简单而且常用的例子就是在线图书查询系统,它允许用户可以按作者,按书名或者按主题来检索图书信息。
      ASP.NET提供了一个DataGrid控件可以比以前的ASP方便地创建创建数据列表,DataGrid控件除了内建的数据表现和方法之外,还允许用户自己定义表现形式。分页技术为用户可管理的数据查找提供方便。DataGrid内建的分页技术很容易实现,但数据量很大时,它的方便性是以牺牲性能为代价的。下面,我们就看看如何通过自定义的分页方法来实现快速处理大量数据的结果集的办法。我们这里讨论的方法比DataGrid的默认分页方法是快速和更加有效的,这是因为每次请求不需要把全部的数据结果发送到Web服务器。相反,它只需要发送每个页面需要的那些数据集。例如:如果一个用户只要求100个页面中每页显示25条记录的第4页的结果集,服务器只需要发送第75-100行的数据即可,而不是1-1000行的完全数据。默认的传送方式如图1所示:

      从图中可以看出,DataGrid的内建分页方法是效率不高的,每次请求都必须把整个查询结果发送给Web服务器,Web服务器再把数据分成相应的页面。利用DataGrid的内建的分页方法尽管是很简单的,但是,由于Web应用的无序性特征,一个用户每次从一个页面转向另外一个页面时,DataGrid对象都被销毁并重新创建,这就意味着数据库服务器每次都必须发送全部的结果集。
      自定义的分页方法只返回所要检索的那些结果集,如下图2所示:

      从上面的图中可以看到,数据库每次只需要返回所要显示的数据记录。首先,我们在数据库中建立一个存储过程,香港虚拟主机,并有两个输入参数,分别是要返回数据的第一条记录数和最后一条记录数,在SQL Server7.0以上的版本中,都有一个top关键字限制返回到结果集中的前多少条记录数,然而不幸的是,没有一个方法可以返回中间一部分的数据,例如第75条记录到100条记录的数据。Oracle中有一个rownum()的扩展函数可以返回中间的记录,网站空间,比如:"select * form Authors where Author_Last_Name = 'Anderson' and rownum() >=75 and rownum()       要创建一个返回指定条记录结果的存储过程,首先必须指定返回结果集的条记录数,可以用临时表,也可以用table变量(SQL Server 2000),两个在性能上没有太大的差别,但是,table变量是存储在内存中的,如果你的服务器内存不多的话,可以考虑用临时表,临时表使用硬盘存储结果,临时表需要手工释放对象,而table变量在存储过程结束后自动释放。

      下面就是我们要创建的存储过程:

create proc GetAuthors
@Author_Last_Name as varchar(100) = null,
@StartRow as int = null,
@StopRow as int = null
AS

---- 建立有标识符列的table变量
declare @t_table table
(
[rownum] [int] IDENTITY (1, 1) Primary key NOT NULL ,
[Author_Last_Name] [varchar] (40) ,
[Author_First_Name] [varchar] (20) ,
[phone] [char] (12) ,
[address] [varchar] (40) ,
[city] [varchar] (20) ,
[state] [char] (2) ,
[zip] [char] (5)
)

---- 在返回指定的@StopRow行数之后停止处理查询
Set RowCount @StopRow

---- 插入到table变量中
insert @t_table
(
[Author_Last_Name],[Author_First_Name],[phone],[address],[city],[state],[zip]
)
SELECT [Author_Last_Name],[Author_First_Name],[phone],[address],[city],[state],[zip]

FROM authors
WHERE Author_Last_Name like '%' + @Author_Last_Name + '%'
ORDER BY Author_Last_Name

---- 返回到正确的结果
SELECT * FROM @t_table WHERE rownum >= @StartRow
ORDER BY rownum

GO

      参数@StartRow和@StopRow接收整数值,代表要返回的开始记录和结束记录,如果要在一个25条记录的页面中返回第4页,我们就可以设置@StartRow为76,@StopRow为100。我们在table变量@t_table中定义了一个叫rownum的整数类型的列,并指定为标识符列,这个列在我们这里介绍的分页技术中是很重要的,当我们插入数据时,这个列自动增加,它将在插入数据时起排序作用。SET ROWCOUNT语句对优化性能很关键,它告诉SQL Server进行限制要插入的数据,如果我们要76-100条记录之间的数据,那么就可以不必插入大于100条记录的数据。最后的SQL语句从@t_table的table变量选择rownum大于或者等于@StartRow的那些数据集,然后把它们返回到Web服务器,由Web服务器绑定到DataGrid对象。值得注意的是:如果要得到76到100条记录的数据,我们必须往table变量中插入100条记录的数据,这意味着:如果浏览者请求的页数越来越大,页面性能也会有所下降的。例如:要显示第100页的数据(从第2451条记录到第2500条记录),我们必须先向table变量或者临时表填充2500条记录,因此,性能依赖于你计算机的硬件和你要返回的记录数,有测试表明,在SQL Server 2000中使用这样的存储过程平均在200-250毫秒内返回第100页,而返回第一页只需要4毫秒。即使返回第500页的数据(从第12451到12500条记录)也可以在650到750毫秒内完成。应该说这种情况是很少见到的。 但为了减轻数据库和网络传输的压力,设计合理的查询结果页数是很见效的。

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 Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Performance comparison of different Java frameworks Performance comparison of different Java frameworks Jun 05, 2024 pm 07:14 PM

Performance comparison of different Java frameworks: REST API request processing: Vert.x is the best, with a request rate of 2 times SpringBoot and 3 times Dropwizard. Database query: SpringBoot's HibernateORM is better than Vert.x and Dropwizard's ORM. Caching operations: Vert.x's Hazelcast client is superior to SpringBoot and Dropwizard's caching mechanisms. Suitable framework: Choose according to application requirements. Vert.x is suitable for high-performance web services, SpringBoot is suitable for data-intensive applications, and Dropwizard is suitable for microservice architecture.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to optimize the performance of multi-threaded programs in C++? How to optimize the performance of multi-threaded programs in C++? Jun 05, 2024 pm 02:04 PM

Effective techniques for optimizing C++ multi-threaded performance include limiting the number of threads to avoid resource contention. Use lightweight mutex locks to reduce contention. Optimize the scope of the lock and minimize the waiting time. Use lock-free data structures to improve concurrency. Avoid busy waiting and notify threads of resource availability through events.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

More than just 3D Gaussian! Latest overview of state-of-the-art 3D reconstruction techniques More than just 3D Gaussian! Latest overview of state-of-the-art 3D reconstruction techniques Jun 02, 2024 pm 06:57 PM

Written above & The author’s personal understanding is that image-based 3D reconstruction is a challenging task that involves inferring the 3D shape of an object or scene from a set of input images. Learning-based methods have attracted attention for their ability to directly estimate 3D shapes. This review paper focuses on state-of-the-art 3D reconstruction techniques, including generating novel, unseen views. An overview of recent developments in Gaussian splash methods is provided, including input types, model structures, output representations, and training strategies. Unresolved challenges and future directions are also discussed. Given the rapid progress in this field and the numerous opportunities to enhance 3D reconstruction methods, a thorough examination of the algorithm seems crucial. Therefore, this study provides a comprehensive overview of recent advances in Gaussian scattering. (Swipe your thumb up

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Revolutionary GPT-4o: Reshaping the human-computer interaction experience Revolutionary GPT-4o: Reshaping the human-computer interaction experience Jun 07, 2024 pm 09:02 PM

The GPT-4o model released by OpenAI is undoubtedly a huge breakthrough, especially in its ability to process multiple input media (text, audio, images) and generate corresponding output. This ability makes human-computer interaction more natural and intuitive, greatly improving the practicality and usability of AI. Several key highlights of GPT-4o include: high scalability, multimedia input and output, further improvements in natural language understanding capabilities, etc. 1. Cross-media input/output: GPT-4o+ can accept any combination of text, audio, and images as input and directly generate output from these media. This breaks the limitation of traditional AI models that only process a single input type, making human-computer interaction more flexible and diverse. This innovation helps power smart assistants

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

See all articles