gpt4 book ai didi

sql-server - SQL Server - 不使用别名创建计划指南

转载 作者:行者123 更新时间:2023-12-03 00:20:26 26 4
gpt4 key购买 nike

我正在尝试在 SQL Server 2012 SP3 Enterprise Edition 中为应用程序运行的特定查询创建计划指南,这意味着我无法以任何方式更改该查询。

查询如下所示:

(@P1 nvarchar(5),@P2 bigint)
DELETE FROM INVENTSUMDELTA WHERE ((DATAAREAID=@P1) AND (TTSID=@P2))

为了创建计划指南,我使用了以下查询:

EXEC sp_create_plan_guide   
@name = N'INVENTSUMDELTAINDEX',
@stmt = N'DELETE FROM INVENTSUMDELTA WHERE ((DATAAREAID=@P1) AND (TTSID=@P2))',
@type = N'SQL',
@module_or_batch = NULL,
@params = N'@P1 nvarchar(5),@P2 bigint',
@hints = N'OPTION (TABLE HINT ( INVENTSUMDELTA, INDEX( I_2397TTSDIMIDX )))';

但是,我收到一个错误:

Msg 8724, Level 16, State 1, Line 1 Cannot execute query. Table-valued or OPENROWSET function 'INVENTSUMDELTA' cannot be specified in the TABLE HINT clause.

我检查了文档并发现以下内容:

TABLE HINT (exposed_object_name [ , [ [, ]...n ] ] ) Applies the specified table hint to the table or view that corresponds to exposed_object_name. [...]

exposed_object_name can be one of the following references:

  • When an alias is used for the table or view in the FROM clause of the query, exposed_object_name is the alias.

  • When an alias is not used, exposed_object_name is the exact match of the table or view referenced in the FROM clause. For example, if the table or view is referenced using a two-part name, exposed_object_name is the same two-part name.

据此,我认为应该可以为不使用别名的查询创建计划指南。但是,我无法让它工作。

所以我的问题是:如何在不使用别名且不更改原始查询的情况下创建计划指南?

最佳答案

该错误消息具有误导性。它与对象的性质无关(对于不存在的对象,您将得到相同的错误)。问题是它不适用于 DELETE 语句 - 任何引用作为 DELETE 目标表的 TABLE HINT 都会产生这个错误。这也不限于计划指南——带有选项的普通DELETE也会失败:

DELETE FROM does_not_exist 
OPTION (TABLE HINT (does_not_exist, INDEX (does_not_exist)))

Cannot execute query. Table-valued or OPENROWSET function 'does_not_exist' cannot be specified in the TABLE HINT clause.

这似乎是一个错误,因为如果该语句在表和查询级别都使用 WITH (ROWLOCK) 提示进行扩充,则错误就会消失:

DELETE FROM does_not_exist WITH (ROWLOCK)
OPTION (TABLE HINT (does_not_exist, ROWLOCK, INDEX (does_not_exist)))

Invalid object name 'does_not_exist'.

this question 中介绍了相同的问题,解决方案是以允许应用提示的形式重写查询。

在这种情况下,我们不能直接重写查询,但是我们仍然可以通过使用 fixed query plan guide 来获得想要的效果。 :

-- Alternate query using hint.
DECLARE @sql NVARCHAR(MAX) = N'WITH T AS (
SELECT *
FROM INVENTSUMDELTA WITH (INDEX (I_2397TTSDIMIDX))
WHERE ((DATAAREAID=@P1) AND (TTSID=@P2))
)
DELETE T';
DECLARE @params NVARCHAR(MAX) = N'@P1 nvarchar(5),@P2 bigint'

-- Put the execution plan in the cache.
EXEC sp_executesql @sql, @params = @params, @P1=NULL, @P2=NULL;

-- Retrieve it.
DECLARE @query_plan NVARCHAR(MAX);
SELECT @query_plan = query_plan
FROM sys.dm_exec_query_stats AS qs
CROSS APPLY sys.dm_exec_sql_text(qs.[sql_handle]) AS st
CROSS APPLY sys.dm_exec_text_query_plan(qs.[plan_handle], DEFAULT, DEFAULT) AS qp
WHERE st.[text] LIKE '(' + @params + ')%' + @sql;

-- Create a plan guide associating the query with the new execution plan.
EXEC sp_create_plan_guide
@name = N'INVENTSUMDELTAINDEX',
@stmt = N'DELETE FROM INVENTSUMDELTA WHERE ((DATAAREAID=@P1) AND (TTSID=@P2))',
@type = N'SQL',
@module_or_batch = NULL,
@params = @params,
@hints = @query_plan;

与往常一样,如果没有其他帮助(更新统计信息、创建新索引、删除次优索引),计划指南应该是最后的手段。此答案假设您已查看所有其他选项并且计划指南是必要的。

关于sql-server - SQL Server - 不使用别名创建计划指南,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44408728/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com