SQL performance: WHERE vs WHERE(ROW_NUMBER)

后端 未结 3 1612
后悔当初
后悔当初 2020-11-29 20:47

I want get n-th to m-th records in a table, what\'s best choice in 2 below solutions:

Solution 1:

    SELECT * FROM Table WHERE ID >= n AND ID <         


        
3条回答
  •  生来不讨喜
    2020-11-29 21:21

    They are different queries.

    Assuming ID is a surrogate key, it may have gaps. ROW_NUMBER will be contiguous.

    If you can guarantee you have no gaps in the data, then the 1st one because I'd hope it's indexed,. The 2nd one is more "correct" though.

提交回复
热议问题