The FAST number_rows Query Hint

Query hints are always about tradeoffs—typically you are giving up something, such as flexibility, to achieve more consistency, or very specific performance characteristic. One example of this migt when returning code results, that you would want to see the data as soon as possible, even before the complete set of data has been returned. Did you know that you can return a subset of rows to look at BEFORE the entire result set is returned?

The FAST n query hint allows the optimizer to return a specified number of rows as quickly as possible. Imagine an application result screen where users frequently wait for data to appear. Wouldn’t it make those users happy if you could return rows faster? Using the FAST 75 table hint for example will return the first 75 records of the results set while still working to return the remaining rows. This allows users start working with the data before the rest of the screen loads with data.

Let’s see it in action!

USE AdventureWorksDW2016CTP3
GO
SET STATISTICS TIME, IO ON
GO
SELECT [OrderDate],[UnitPrice],[OrderQuantity]
FROM [dbo].[FactResellerSalesXL]
ORDER BY OrderDate
GO

Note after 30 seconds it is still running and I have no results in the result grid. So, let’s kill it.

The fast hint works by telling the query optimizer to use techniques like nested join in lieu of hash joins to return the first rows more quickly. Always keep in mind that query hints can negatively impact the overall performance of the query, and you should test extensively before using any query hints including this one.

Now let’s run the same thing with the Query Hint.

SELECT [OrderDate],[UnitPrice],[OrderQuantity]
FROM [dbo].[FactResellerSalesXL]
ORDER BY OrderDate
OPTION ( FAST 75);

If you pop over to the results tab you immediately see the first 75 rows and it begins to populate right away. It was actually immediate but I couldn’t screen capture fast enough and it listed 75 rows 😉

You can imagine what impact this can have for users. If you have an application in which users need to start seeing data as soon as possible, this is a great option. Play around with it and see how it can help you.

About Monica Rathbun

Monica Rathbun lives in Virginia, is a Microsoft MVP for Data Platform and Microsoft Certified Solutions Expert. She has nearly two decades of experience working with a wide variety of database platforms with a focus on SQL Server and the Microsoft Data Platform. She is a frequent speaker at IT industry conferences on topics including performance tuning and configuration management. She is the Leader of the Hampton Roads SQL Server User Group. She is passionate about SQL Server and the SQL Server community, doing anything she can to give back. Monica can always be found on Twitter (@sqlespresso) handing out helpful tips. You can find Monica blogging at sqlespresso.com 
Bookmark the permalink.

One Comment

  1. Heh… Perception is reality for users, as well. I suppose this could also be used to give the user the perception that “the database” is actually running very quickly even though this technique might actually slow things down overall. 🙂

Comments are closed