都内で働くSEの技術的なひとりごと / Technical soliloquy of System Engineer working in Tokyo

都内でサラリーマンやってます。SQL Server を中心とした (2023年からは Azure も。) マイクロソフト系(たまに、OSS系などマイクロソフト以外の技術も...)の技術的なことについて書いています。日々の仕事の中で、気になったことを技術要素関係なく気まぐれに選んでいるので記事内容は開発言語、インフラ等ばらばらです。なお、当ブログで発信、発言は私個人のものであり、所属する組織、企業、団体等とは何のかかわりもございません。ブログの内容もきちんと検証して使用してください。英語の勉強のため、英語の

Useful SQL functions, syntax, and more - Part 19 (FAST?)

Recently I started working on SQL Server just a little bit on side job. Tuning is an area that I personally like very much because it's like putting the puzzle back together. While doing some research, I found a query hint that I'd never used called "FAST".

FAST number_rows
Specifies that the query is optimized for fast retrieval of the first number_rows. This result is a nonnegative integer. After the first number_rows are returned, the query continues execution and produces its full result set.

Is there anything this convenient for the keyword "fast search"? But I was interested in it, so I'll check out how it works as soon as possible. I'll use the following query.

SELECT 
    SOH.SalesOrderID,
    SOH.CustomerID,
    SOH.SalesPersonID
FROM 
    Sales.SalesOrderHeader SOH
WHERE 
    SalesPersonID = 279;

Above execution plan as follows.
f:id:koogucc11:20200831201858p:plain

Next, let's add OPTION (FAST n). n = 100.

SELECT
    SOH.SalesOrderID,
    SOH.CustomerID,
    SOH.SalesPersonID
FROM 
    Sales.SalesOrderHeader SOH
WHERE 
    SalesPersonID = 279
OPTION (FAST 100);

 Above Execution plan as follows.
f:id:koogucc11:20200831201946p:plain

Let's compare the two plans. The number of rows is different and the cost is lower with OPTION (FAST 100) added.
f:id:koogucc11:20200831202923p:plain

The estimated number of rows is different. The one with OPTION (FAST 100) has 100. There is an attribute "EstimateRowsWithoutRowGoal = 429" that I've never seen before. (My knowledge of SQL Server stops at version 2012, but I don't remember this being around for a long time.)
f:id:koogucc11:20200831203652p:plain

The estimated number of rows has been reduced here as well, and "EstimateRowsWithoutRowGoal = 31465" has been added.
f:id:koogucc11:20200831204006p:plain

Hmmm, I haven't written an article in a while, but I'm halfway through. But I guess this is convenient because it's a good story to write an article about above, lol.