What would be the best way to fetch around a million record from DB?

后端 未结 4 1421
小蘑菇
小蘑菇 2021-02-09 05:04

I need to fetch and show data on a webpage whose number of records may vary based on filters from around 500 records to 1 million records.

Will caching be of any use her

4条回答
  •  隐瞒了意图╮
    2021-02-09 05:22

    I will suggest use dynamic query with paging . so, when you click on specific page fetch records only for those pages. To fetch record from database from specific range use following query.

    like this.

    Create proc Test
    
    @take smallint,
    @skip smallint,
    @orderBy nvarchar(20),
    @subscriptionid smallint,
    
    
    as 
    
    DECLARE @SQLQuery AS NVARCHAR(max)
    
    SET @SQLQuery=' Select ROW_NUMBER() OVER (ORDER BY P.ProductId desc) as RowNum,* from product"
    
    set @SQLQuery=@SQLQuery + ' and Subscriptionid='+CONVERT(nvarchar, @subscriptionid) 
    
    set @SQLQuery= ';WITH Results_CTE AS ( '+@SQLQuery
    
        set @SQLQuery= @SQLQuery +' ) SELECT * FROM Results_CTE WHERE RowNum > '+CONVERT(nvarchar, @skip)+' AND RowNum <= '+CONVERT(nvarchar, @skip+@take)  --//paging';
    END
    
    EXECUTE sp_executesql @SQLQuery 
    

提交回复
热议问题