开发者

SQL for selecting a cluster of records from a huge table with optimum SQL

开发者 https://www.devze.com 2023-04-10 09:54 出处:网络
I\'m trying to paginate using MS SQL Server 2005 in one of my ASP net applications. To fetch 10 records from the employee table I\'m running the following SQL.

I'm trying to paginate using MS SQL Server 2005 in one of my ASP net applications. To fetch 10 records from the employee table I'm running the following SQL.

;WITH CTE AS (
                SELECT EmployeeID,
                       [Name],
                       ROW_NUMBER() OVER(ORDER BY EmployeeID ASC) AS Ro开发者_如何学GowNo
                FROM   Employee
            ) SELECT *
              FROM   CTE WHERE RowNo BETWEEN 11 AND 20  

The problem with this is my employee table is having 100 thousands of records and since I'm running the above query it is taking lot time. I have seen in MySQL that there is a phrase called LIMIT to limit the select record count.

Kindly help me to fetch a particular number of records records without using Common Table Expression and without running a query like the above one. I would like to know if there is a better way than this.

Thanks in advance for your time and help.


I tried your query on a table with 150.000 rows, I tried with on a column with an index and one without an index. Both was less than 1 second execution time. I imagine you have a different problem.

Since I imagine your problem lies elsewhere, I surgest you try this:

;WITH CTE AS ( 
            SELECT EmployeeID, 
                   [Name], 
                   ROW_NUMBER() OVER(ORDER BY EmployeeID ASC) AS RowNo 
            FROM   Employee WITH (NOLOCK)
        ) SELECT * 
          FROM   CTE WHERE RowNo BETWEEN 11 AND 20  
0

精彩评论

暂无评论...
验证码 换一张
取 消