加入收藏 | 设为首页 | 会员中心 | 我要投稿 拼字网 - 核心网 (https://www.hexinwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

sql-server – SELECT语句中的OPTION FAST有什么作用?

发布时间:2021-01-09 00:00:59 所属栏目:MsSql教程 来源:网络整理
导读:我已经做了一些挖掘OPTION(FAST XXX)查询提示在SELECT语句中做了什么,并且仍然对它感到困惑.根据MSDN: Specifies that the query is optimized for fast retrieval of the first number_rows. This is a nonnegative integer. After the first number_rows a

我已经做了一些挖掘OPTION(FAST XXX)查询提示在SELECT语句中做了什么,并且仍然对它感到困惑.根据MSDN:

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

对我来说没有多大意义,但基本上查询可以真正快速获得前XXX行,然后以正常速度获得其余的行?

让我思考的Microsoft Dynamics查询是:

select pjproj.project,pjproj.project_desc,pjproj.customer,pjproj.cpnyid
from pjproj WITH (NOLOCK)
where project like  '%'
order by project OPTION(FAST 500)

任何人都可以准确地解释这个查询提示正在做什么,它比不使用它更有优势吗?

解决方法

快速N将告诉SQL Server生成执行计划,并快速返回定义为N的行数.

请注意,估计将按照N,因为您告诉sql server尽可能快地检索N行.

例如在500以下的查询下运行:

-- total rows : 19972
 SELECT [BusinessEntityID],[TotalPurchaseYTD],[DateFirstPurchase],[BirthDate],[MaritalStatus],[YearlyIncome],[Gender],[TotalChildren],[NumberChildrenAtHome],[Education],[Occupation],[HomeOwnerFlag],[NumberCarsOwned]
  FROM [AdventureWorks2012].[Sales].[vPersonDemographics]
  order by BusinessEntityID
  option (fast 500)

Est与实际行选项(快速500)

Est与没有选项的实际行(快速500)

用例是当应用程序正在进行缓存(在后台加载大量数据)并希望尽快向用户显示一片数据时.

另一个有趣的用例是在SSIS land that Rob Farley中描述了使用FAST N作为加速数据检索的催化剂.

By adding this hint,it felt like a magic wand had been waved across the query,to make it run several times faster.

见Remus Rusanu’s answer as well.

(编辑:拼字网 - 核心网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读