Retrieve paging data instances using SQL directly

Source: Internet
Author: User
On the list page, the programmer used the "Wuqi. Webdiyer" Paging plug-in, which was slow and could not be found. After reading the code, I thought there was too much redundancy, so I decided to optimize it.

On the list page, the programmer used the "Wuqi. Webdiyer" Paging plug-in, which was slow and could not be found. After reading the code, I thought there was too much redundancy, so I decided to optimize it.

After looking for some information from the Internet, consider directly at the bottom of the SQL, directly retrieve the data on the current page, and then bind it, which should be more efficient.

The core SQL query statement is as follows:

The Code is as follows:
Select top @ size * from (@ sqlstring)
Where @ key <= (select min (@ key) from (select top @ star @ key from (@ sqlstring) a order by @ key desc))
And @ key> = (select min (@ key) from (select top @ end @ key from (@ sqlstring) a order by @ key desc))
Order by @ key desc

Where:

@ Size: The quantity of one page;

@ Sqlstring: the original query statement;

@ Key: key field/sorting field, which should be a unique field;

@ Star: Index of the number of records starting, star = size * (p-1) + 1;

@ End: Index of the number of ending records, end = size * p + 1;

If the sorting is different, the code should be changed accordingly:

The Code is as follows:
Select top @ size * from (@ sqlstring)
Where @ key> = (select max (@ key) from (select top @ star @ key from (@ sqlstring) a order by @ key asc))
And @ key <= (select max (@ key) from (select top @ end @ key from (@ sqlstring) a order by @ key asc))
Order by @ key asc


In this way, I construct a method to retrieve the dataset, pass the relevant parameters in, splice the query, and bind the returned dataset.

No error is found in this solution, and the efficiency is also improved.

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.