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

后端 未结 4 1434
小蘑菇
小蘑菇 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:34

    I concur with the rest of the answerers. displaying 1M records is ludicrous. However, you can display the first X records, and page through.

    The trick is in the Stored Procedure doing the fetching

    ALTER PROCEDURE [dbo].[MyHugeTable_GetWithPaging] 
    ( 
            @StartRowIndex      int, 
            @MaximumRows        int 
    ) 
    
    AS 
    SET NOCOUNT ON 
    
    Select 
        RowNum, 
        [UserName]
    From 
        (Select 
            [ID], 
            [UserName]
            Row_Number() Over(Order By [ID] Desc) As RowNum 
            From dbo.[MyHugeTable] t) 
    As DerivedTableName 
    Where RowNum Between @StartRowIndex And (@StartRowIndex + @MaximumRows) 
    

提交回复
热议问题