Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6991715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:32:24+00:00 2026-05-27T19:32:24+00:00

I have an indexed view but when I run queries on that view the

  • 0

I have an indexed view but when I run queries on that view the index which is built on View is not applied and the query runs without index. Below is my dummy script:
Tables + View+ Index on View

CREATE TABLE P_Test
  (
     [PID]      INT IDENTITY,
     [TID]      INT,
     [StatusID] INT
  )

CREATE TABLE T_Test
  (
     [TID] INT IDENTITY,
     [FID] INT,
  )

CREATE TABLE F_Test
  (
     [FID]      INT IDENTITY,
     [StatusID] INT
  )

GO

INSERT INTO F_Test
SELECT TOP 1000 ABS(CAST(NEWID() AS BINARY(6)) %10) --below 100
FROM   master..spt_values

INSERT INTO T_Test
SELECT TOP 10000 ABS(CAST(NEWID() AS BINARY(6)) %1000) --below 1000
FROM   master..spt_values,
       master..spt_values v2

INSERT INTO P_Test
SELECT TOP 100000 ABS(CAST(NEWID() AS BINARY(6)) %10000) --below 10000
                  ,
                  ABS(CAST(NEWID() AS BINARY(6)) %10)--below 10
FROM   master..spt_values,
       master..spt_values v2

GO

CREATE VIEW [TestView]
WITH SCHEMABINDING
AS
  SELECT P.StatusID AS PStatusID,
         F.StatusID AS FStatusID,
         P.PID
  FROM   dbo.P_Test P
         INNER JOIN dbo.T_Test T
           ON T.TID = P.TID
         INNER JOIN dbo.F_Test F
           ON T.FID = F.FID

GO

CREATE UNIQUE CLUSTERED INDEX [PK_TestView]
  ON [dbo].[TestView] ( [PStatusID] ASC, [FStatusID] ASC, [PID] ASC )
  WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO 

Now when I run the following queries the [PK_TestView] index is not being applied:

    SELECT PStatusID ,
        FStatusID ,
        PID  FROM [TestView] 

SELECT PStatusID ,
        FStatusID ,
        PID  FROM [TestView] 
WHERE [PStatusID]=1

SELECT COUNT(PStatusID) FROM [TestView] 
WHERE [PStatusID]=1

Can you help me fixing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:32:24+00:00Added an answer on May 27, 2026 at 7:32 pm

    You need to use the NOEXPAND hint. SQL Server will not consider matching indexed views without this (even if the view name is referenced in the query) unless you are on Enterprise Edition engine.

    SELECT COUNT(PStatusID) 
    FROM [TestView] 
         WITH (NOEXPAND) -- this line
    WHERE [PStatusID]=1
    

    This should give you the first, much cheaper, plan

    Plan

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C# you can not have indexed properties. That said, how do I convert
I have a Materialized view in Oracle that contains a LEFT JOIN which takes
I have an indexed array which I've generated from an associative array with this
I'm running into an issue in that I have a document indexed with elasticsearch
I have a TYPO3 site with Indexed Search Engine extension... The problem is that
We have a dev server that has managed to become indexed by Google. Page
I have a indexed view where I basically need to do this SELECT ...
I have created an indexed view: CREATE VIEW LogValueTexts WITH SCHEMABINDING AS SELECT ISNULL(LRVS_SLOG_ID*256+LRVS_IDX,0)
When I have a View Model setup with an ImportingConstructor that takes a single
I see from the 2005 documentation that you cannot create an indexed view from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.