October 7, 2013 by Kenneth Fisher
Both TOP and SET ROWCOUNT are both valid methods of limiting the result sets from a query. They are however very different commands. The TOP clause of a command limits that single command, while the SET ROWCOUNT command limits all eligible queries within the connection until another SET ROWCOUNT is called.
If you have several queries where you only want the top 10 rows it might seem better to use SET ROWCOUNT.
SET ROWCOUNT 10 SELECT * FROM HumanResources.Department SELECT * FROM HumanResources.Employee SELECT * FROM HumanResources.EmployeeDepartmentHistory SELECT * FROM HumanResources.EmployeePayHistory SELECT * FROM HumanResources.JobCandidate SELECT * FROM HumanResources.Shift SET ROWCOUNT 0
SELECT TOP 10 * FROM HumanResources.Department SELECT TOP 10 * FROM HumanResources.Employee SELECT TOP 10 * FROM HumanResources.EmployeeDepartmentHistory SELECT TOP 10 * FROM HumanResources.EmployeePayHistory SELECT TOP 10 * FROM HumanResources.JobCandidate SELECT TOP 10 * FROM HumanResources.Shift
So why would I say use the TOP command instead of SET ROWCOUNT? Well among other things because it is more predictable. SET ROWCOUNT limits ALL eligible queries. That means Triggers also.
Here is an example of what can happen using AdventureWorks2008. I’m creating a trigger that will act like a cascading delete on a parent child relationship.
-- Setup Code -- Create temporary tables to test with to avoid the existing foreign keys -- Create a temp SalesOrderHeader table SELECT TOP 20 * INTO TempSalesOrderHeader FROM Sales.SalesOrderHeader -- Create a temp SalesOrderDetail table SELECT * INTO TempSalesOrderDetail FROM Sales.SalesOrderDetail WHERE SalesOrderId IN (SELECT SalesOrderId FROM TempSalesOrderHeader) GO -- Create Trigger CREATE TRIGGER dbo.tr_dl_SalesOrderDetail ON TempSalesOrderHeader AFTER DELETE AS BEGIN DELETE FROM TempSalesOrderDetail FROM TempSalesOrderDetail JOIN deleted ON TempSalesOrderDetail.SalesOrderId = deleted.SalesOrderId END GO
-- Run my delete using SET ROWCOUNT SET ROWCOUNT 10 DELETE FROM TempSalesOrderHeader SET ROWCOUNT 0 GO
-- Show orphaned OrderDetail rows SELECT * FROM TempSalesOrderDetail WHERE SalesOrderId NOT IN (SELECT SalesOrderId FROM TempSalesOrderHeader) GO
-- Cleanup code DROP TABLE TempSalesOrderHeader DROP TABLE TempSalesOrderDetail
The SET ROWCOUNT caused the delete of the Header table to only delete 10 rows. This was expected. Unfortunately it also caused the delete of the Detail table to only be 10 rows. This is probably not an expected behavior. Note that if I had used a TOP on my DELETE statement I would have deleted all 109 child rows.
Now BOL has said for the last few versions that SET ROWCOUNT will no longer be supported by DELETE, INSERT, and UPDATE statements. It still works with 2012 and I haven’t been able to check on 2014 yet but at some point this is going to happen. Once it does my above example is no longer going to be valid. Of course at that point the DELETE statement is no longer going to be limited either. And in general if you have written code that relies on SET ROWCOUNT to limit a DELETE, INSERT or UPDATE statement then your code will no longer work the way you expected.
I have to admit, if you are very careful, and are the only person writing T-SQL in your system so that no one changes anything behind your back then my above arguments aren’t going to really be that convincing.
Unfortunately for this next bit I wasn’t able to come up with a good example but according to BOL (Under TOP – Best Practices) SET ROWCOUNT is not used by the optimizer to improve the performance of the queries affected by it. Specifically BOL says:
As a part of a SELECT statement, the query optimizer can consider the value of expression in the TOP or FETCH clauses during query optimization. Because SET ROWCOUNT is used outside a statement that executes a query, its value cannot be considered in a query plan.
This means that the optimizer may optimize your query as if it was going to pull all 100mil rows instead of just the first 100 that you really wanted. This can result in a wildly different execution plan causing your query to take longer to pull that measly 100 rows that it should.
To be fair I’ve used SET ROWCOUNT plenty of times in my life, although after having read this myself I may cut back some. It does have its but in my opinion only in the simplest most controlled settings and even then probably only for ad hoc queries. SET TOP is just easier to control, has more options (percentages for instance) and is just as easy to put in my code.