0

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. 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. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the 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. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.

Serge Profafilecebook
  • 1,165
  • 1
  • 14
  • 32
  • Sorry for the "PS" stackoverflow had me add details when I can't figure what more I could say. – Serge Profafilecebook Jan 19 '15 at 09:57
  • So, you have table `**x**FedDBLogMsg`, which has index called `PK_**t**FedDBLogMsg`. You have view `**t**FedDBLogMsg`, which references `**t**FedDBLogMsg` in (another?) database `FedDBMsg`. And then you have stored procedure that references `MyGolf` database. It is quite messy and a lot of very similar identifiers and a mix of databases. I guess, the first thing would be to check that there are no typos here. – Vladimir Baranov Jan 19 '15 at 11:24
  • **t**FedDBLogMsg is a view replacing an old table. thus it's named like the old table – Serge Profafilecebook Jan 19 '15 at 12:25
  • It looks like you should rewrite your question. Make your query without index hint, show execution plan, point out what is wrong with the execution plan and why you think the plan is wrong. Ask how you can force optimizer to change the execution plan to what you need. – Vladimir Baranov Jan 19 '15 at 22:02
  • From what you shown so far, the table `xFedDBLogMsg` has only one primary clustered index. So, I think that it will always be used in any query, there is simply no other choice. It can be used as a scan, or as a seek. From MSDN: "If a clustered index exists, INDEX(0) forces a clustered index scan and INDEX(1) forces a clustered index scan or seek." So, you can try this form of index hints to see if it makes any changes to the execution plan. – Vladimir Baranov Jan 19 '15 at 22:04
  • There are more than one index, I didn't copy them because they are irrelevant. Wihtouth query hints calls to the procedure are very slow. SQL Server does full index scan. I managed to fix the problem by not using the view, but directly do the views' join inside my query. But I still don't know why SQL Server refused the hint. – Serge Profafilecebook Jan 20 '15 at 08:27

2 Answers2

3

In the part

WITH (INDEX(PK_tFedDBLogMsg))

is PK_tFedDBLogMsg besides the name of the constraint, a clustered index on the view as well?

If so, be sure to use the NOEXPAND option as well.

sstan
  • 35,425
  • 6
  • 48
  • 66
Roeland
  • 820
  • 1
  • 9
  • 33
0

"Automatic use of indexed view by query optimizer" is available in Enterprise (and Developer) edition only. 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

"Direct query of indexed views (using NOEXPAND hint)" is available in all editions

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

Mikhail Lobanov
  • 2,976
  • 9
  • 24