Sample Header Ad - 728x90

MSSQL efficient paging including a join on big tables

4 votes
1 answer
4670 views
We have a table with products and a table with a current selection/assortment of products. Now need to join them create an export file by some .net code. Currently using query like this: SELECT * FROM ASSORTMENT a INNER JOIN PRODUCTS p on a.clientID = 38 and a.productID = p.productID ORDER BY a.ID_ROW OFFSET 100000 ROWS FETCH NEXT 1000 ROWS ONLY Problem is that the assortment can contain discontinued products, that are not existing in products table anymore, so need a join to verify. Also need each time exactly same amount of rows per select, e.g. 1000. The query gets slower if the offset gets higher because it does a full join of products and assortment table each time. On 3 Million products and an assortment of 200.000 articles it takes multiple seconds to fetch next 1000 if the offset is over 50.0000 Is there a more efficient way of doing this?
Asked by Svn342 (41 rep)
Dec 31, 2017, 10:38 PM
Last activity: May 12, 2020, 03:13 PM