444.Plan guide

SQL Server 2005 introduces the sp_create_plan_guide system procedure for creating plan guide to optimize the performance of queries. This procedure can be used when you cannot or do not want to directly change the text of the query.
Plan guide influence optimization of queries by attaching query hints to them. In the sp_create_plan_guide statement, you specify the query that you want to optimize and the OPTION clause that contains the query hints you want to use to optimize the query. When the query executes, SQL Server matches the query to the plan guide and attached the OPTION clause to the query at run time.

Queries than can benefit from plan guides are generally parameter-based, and may be performing poorly because they use cached query plans whose parameter values do not represent a worst-case or most representative scenario. The OPTMIZE FOR and RECOMPILE query hints can be used to address this problem.
OPTMIZE FRO instructs SQL Server to use a particular value for parameter when the query is optimized. RECOMPILE instructs the server to discard a query plan after execution, forcing the query optimizer to recompile a new query plan the next time that the same query is executed.
Another common hit to use with plan guides is the USE PLAN query hint. This query hint applies when you are already aware of an existing execution plan that can be substituted for the one selected by the optimizer for a particular query because you know it perform better. USE PLAN forces SQL Server to use a particular query plan, specified explicitly in the hint syntax, when executing the query.
 Plan guides can be created to match queries that are executed in the following contexts:
OBJECT plan guides match queries that execute in the context of T-SQL stored procedures, multistatement table-valued functions, and DML triggers.
 SQL plan guides match queries that execute in the context of stand-along T-SQL statements and batches that are not part of a database object.
 TEMPLATE plan guides match stand-along queries that parameterize to a specified form. These plan are used to override the current PARAMATERIZATION database SET option of a database for a class of queries.

ContractedBlock.gif ExpandedBlockStart.gif Code
/*OBJECT PLAN GUIDE*/
-- create sp
CREATE PROCEDURE Sales.GetSalesOrderByCountry  (@Country nvarchar(60))
AS
BEGIN
    
SELECT * 
    
FROM Sales.SalesORderHeader h, Sales.Customer c, Sales.SalesTerritory t
    
WHERE h.CUstomerID = c.CUstomerID
        
AND c.TerritoryID = t.TerritoryID
        
AND CountryRegionCode = @Country
END

-- exec the sp
Sales.GetSalesOrderByCountry N'AU'

-- create a plan guide
sp_create_plan_guide
@name = N'Guide_AW_SP_GetSalesOrderByCountry',
@stmt = N'SELECT * 
    FROM Sales.SalesORderHeader h, Sales.Customer c, Sales.SalesTerritory t
    WHERE h.CUstomerID = c.CUstomerID
        AND c.TerritoryID = t.TerritoryID
        AND CountryRegionCode = @Country
',
@type=N'OBJECT',
@module_or_batch = N'Sales.GetSalesOrderByCountry',
@params = NULL,
@hints = N'OPTION (OPTIMIZE FOR (@Country = N''US''))'



/*SQL PLAN GUIDE*/
-- create t-sql
SELECT TOP 1 * FROM Sales.SalesORderHeader h ORDER BY OrderDate DESC

-- create plan guide
sp_create_plan_guide
@name = N'Guide_AW_SQL_GetLatestORder',
@stmt = N'SELECT TOP 1 * FROM Sales.SalesORderHeader h ORDER BY OrderDate DESC',
@type=N'SQL',
@module_or_batch = NULL,
@params = NULL,
@hints = N'OPTION (MAXDOP 1)'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<com.hryt.design.base.HHConstraintLayout android:layout_width="wrap_content" android:layout_height="@dimen/dimen_416" android:visibility="@{isGuide ? View.GONE : View.VISIBLE}" android:background="@drawable/map_scroll"> <com.hryt.design.toggle.HHToggleButton android:id="@+id/route_plan_service" android:layout_width="@dimen/dimen_264" android:layout_height="@dimen/dimen_154" android:clickable="true" android:gravity="center" android:splitMotionEvents="false" android:text="@string/guide_route_along_way_service" android:textStyle="bold" android:textSize="@dimen/font_28" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:hryt_toggleType="rectangle"/> <com.hryt.design.toggle.HHToggleButton android:id="@+id/route_plan_cloudy" android:layout_width="@dimen/dimen_264" android:layout_height="@dimen/dimen_154" android:clickable="true" android:gravity="center" android:splitMotionEvents="false" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="@dimen/dimen_122" android:text="@string/guide_route_along_way_cloudy" android:textStyle="bold" android:textSize="@dimen/font_28" app:layout_constraintStart_toStartOf="parent" app:hryt_toggleType="rectangle"/> <com.hryt.design.toggle.HHToggleButton android:id="@+id/route_road" android:layout_width="@dimen/dimen_264" android:layout_height="@dimen/dimen_154" android:clickable="true" android:gravity="center" android:splitMotionEvents="false" app:layout_constraintBottom_toBottomOf="parent" android:text="@string/guide_route_along_way_road" android:textStyle="bold" android:textSize="@dimen/font_28" app:layout_constraintStart_toStartOf="parent" app:hryt_toggleType="rectangle"/> </com.hryt.design.base.HHConstraintLayout>
06-02

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值