简体   繁体   English

为什么我的查询提示被忽略?

[英]Why is my query hint being ignored?

I don't get why I can't use the primary key as index in my view. 我不明白为什么我不能在视图中使用主键作为索引。

Here's the main table 这是主表

CREATE TABLE [dbo].[xFedDBLogMsg](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [msgType] [int] NOT NULL,
    [date] [datetime] NOT NULL,
    [delay] [time](7) NOT NULL,
    [error] [bit] NOT NULL,
    [processID] [int] NULL,
 CONSTRAINT [PK_tFedDBLogMsg] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Here's the view 这是视图

CREATE VIEW [dbo].[tFedDBLogMsg]
AS
SELECT
    L.ID
    , L.msgType
    , L.[date]
    , M.MsgSent
    , M.MsgReceived
    , L.[delay]
    , L.error
    , L.processID
    , NEWID() AS INTERNALID
FROM dbo.xFedDBLogMsg AS L
LEFT JOIN FedDBMsg.dbo.tFedDBLogMsg AS M ON (
    M.ID = L.ID
)

And here the procedure that gives me a warning: 这是给我警告的过程:

ALTER PROCEDURE spGetFedDBErrorsByID (
    @pIDS AS dbo.typeNumberList READONLY
)
AS
BEGIN
        SELECT
            MSG.ID
            , MSG.msgType
            , MSG.date
            , MSG.MsgSent
            , MSG.MsgReceived
        FROM (
            SELECT
                CAST(ID.n AS INT) AS ID
            FROM @pIDS AS ID
        ) AS X
        INNER JOIN MyGolf.dbo.tFedDBLogMsg AS MSG WITH (INDEX(PK_tFedDBLogMsg)) ON (
            MSG.ID = X.ID
        )

END   
GO

Warning: Index hints supplied for view 'MyGolf.dbo.tFedDBLogMsg' will be ignored.

PS: There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. PS:Lorem Ipsum的段落有很多变体,但大多数都因注入幽默感或看起来有些难以置信的随机单词而以某种形式发生了变化。 If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. 如果要使用Lorem Ipsum的段落,则需要确保文本中间没有隐藏任何令人尴尬的内容。 All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. Internet上的所有Lorem Ipsum生成器都倾向于根据需要重复预定义的块,这使其成为Internet上第一个真正的生成器。 It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. 它使用包含200多个拉丁词的字典,结合少量模型句子结构,生成看起来合理的Lorem Ipsum。 The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc. 因此,生成的Lorem Ipsum始终没有重复,注入幽默感或非特征性单词等。

In the part 在部分

WITH (INDEX(PK_tFedDBLogMsg))

is PK_tFedDBLogMsg besides the name of the constraint, a clustered index on the view as well? 约束名称之外, PK_tFedDBLogMsg还是视图上的聚簇索引?

If so, be sure to use the NOEXPAND option as well. 如果是这样,请确保也使用NOEXPAND选项。

"Automatic use of indexed view by query optimizer" is available in Enterprise (and Developer) edition only. 仅在Enterprise(和Developer)版本中提供“由查询优化器自动使用索引视图”。 This means that in enterprise edition you can also optimize queries by creating indexed views: sql server can use it even if you do not specify it in query text 这意味着在企业版中,您还可以通过创建索引视图来优化查询:即使您未在查询文本中指定它,sql server也可以使用它

"Direct query of indexed views (using NOEXPAND hint)" is available in all editions 所有版本中均提供“直接查询索引视图(使用NOEXPAND提示)”

https://docs.microsoft.com/en-US/sql/sql-server/editions-and-components-of-sql-server-2016 https://docs.microsoft.com/zh-CN/sql/sql-server/editions-and-components-of-sql-server-2016

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM