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
精彩评论