SQL Paging optimization

Source: Internet
Author: User

1. First (not available, no difference with not using top) "Time 15~20s"

SELECT * FROM
(
SELECT TOP row_number () over (ORDER by Id) as Num, * from
(
SELECT * from Goods_1 WHERE Name like '% test% ' UNION all
SELECT * from goods_2 WHERE Name like '% test% ' UNION all
SELECT * from Goods_3 WHERE Name '% test% ' UNION all
SELECT * from Goods_4 WHERE Name like '% test% ' UNION all
SELECT * from Goods_5 WHERE Name like '% test% ' UNION all
SELECT * from Goods_6 WHERE Name like '% test% ' UNION all
SELECT * from goods_7 WHERE Name like '% test% ' UNION all
SELECT * from Goods_8 WHERE Name '% test% '
) as T
) as T
WHERE Num between ((2-1) * + 1) and 2 * 10;

2. Second (usable, time-consuming significantly reduced) "time-consuming 2~4s"

SELECT * FROM
(
SELECT row_number () over (ORDER by Id) as Num, * from
(
SELECT TOP * from goods_1 WHERE Name '% test% ' UNION all
SELECT TOP * from goods_2 WHERE Name '% test% ' UNION all
SELECT TOP * from Goods_3 WHERE Name '% test% ' UNION all
SELECT TOP * from Goods_4 WHERE Name '% test% ' UNION all
SELECT TOP * from goods_5 WHERE Name '% test% ' UNION all
SELECT TOP * from goods_6 WHERE Name '% test% ' UNION all
SELECT TOP * from goods_7 WHERE Name '% test% ' UNION all
SELECT TOP * from goods_8 WHERE Name '% test% '
) as T
) as T
WHERE Num between ((2-1) * + 1) and 2 * 10;

Here 2 refers to the page number of pagination, 20 refers to the paging size, such a way to reduce time-consuming can be imagined ~

SQL Paging optimization

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.