Comparison of Two Methods for parsing database pages (comparison of row_number () over () and top)

Source: Internet
Author: User

Today, the teacher gave me a review of the pages in the database. In general, today is a good day, because I have not forgotten what I learned before. Now, go to the topic,
First, let's talk about the top method.
The top method is to remove the data before the data of the number of pages you want to query and then retrieve the first few
Example:
Copy codeThe Code is as follows:
Take the data on the first page
-- Page 1
Select top 3 * from T_news;
Take the data on the fifth page
-- Page 5
Select top 3 * from T_News where id not in (select top (3*4) id from T_News) -- the key is that not in is used to remove the data from the previous pages.
If you want to set the number of data entries on each page and view the number of pages, you can add more stored procedures.
Create proc usp_fenye @ geshu int, @ yeshu int
As
Begin
Select top (@ geshu) * from T_News where id not in (select top (@ geshu * (@ yeshu-1) id from T_News)
End

Then, let's talk about the ROW_NUMBER () over () method.
This is actually adding a column to the data table to determine the number of data entries.
Example:
Copy codeThe Code is as follows:
Take the data on the first page
Select * from (select *, ROW_NUMBER () over (order by id asc) as number from T_News) as tb1
Where number between 1 and 3;
Data on page 5
Select * from (select *, ROW_NUMBER () over (order by id asc) as number from T_News) as tb1
Where number between 3*4 + 1 and 3*5;
Set the data entries on each page and view the page number.
Create proc usp_fenye @ geshu int, @ yeshu int
As
Begin
Select * from (select *, ROW_NUMBER () over (order by id asc) as number from T_News) as tb1
Where number between @ geshu * (@ yeshu-1) + 1 and @ geshu * @ yeshu;
End

Well, that's what I understand. I hope it can help people who see it ~

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.