Re: Indexes being improperly used when selecting data through a view



joshsackett (joshsackett@xxxxxxxxx) writes:
> I am having a problem with indexes on specific tables. For some reason
> a query that runs against a view is not selecting the correct index on
> a table. I run the same query against the table directly and it looks
> fine. Can anyone give me some insight? Thanks.
>
> PRODUCTION1:
> CREATE TABLE MyTest1 (ID INT IDENTITY(1,1), COLUMN1 CHAR(10), COLUMN2
> CHAR(10))
> CREATE CLUSTERED INDEX IDX_MyTest1 ON MyTest1 ON (ID)
> CREATE NONCLUSTERED INDEX IDX_MyTest2 ON MyTest1 ON (COLUMN2, ID)

You have:

> While in PRODUCTION1:
> SELECT ID, COLUMN2 FROM MyTest1 WHERE COLUMN2 = 'Testing'
> --> Clustered index seek PRODUCTION1..IDX_MyTest2
> --> Results returned

But that is not possible. Either you would have an Index Seek +
Bookmark Lookup oon IDX_MyTest2 or a Clustered Index Scan on IDX_MyTest1.

> REPORTDB:
> CREATE VIEW MyTest1 AS
> SELECT ID, COLUMN1, COLUMN2 FROM PRODUCTION1..MyTest1
> UNION ALL
> SELECT ID, COLUMN1, COLUMN2 FROM ARCHIVE1..MyTest1
>...
> While in REPORTDB:
> SELECT ID, COLUMN2 FROM MyTest1 WHERE COLUMN2 = 'Testing'
> --> Index seek PRODUCTION1..IDX_MyTest2
> --> Bookmark lookup PRODUCTION1..IDX_MyTest1
> --> Index seek ARCHIVE1..IDX_MyTest2
> --> Bookmark lookup ARCHIVE1..IDX_MyTest1
> --> Concatenate data and results returned

What happens if you actually have the UNION ALL, and not the UNION as
you correct later? With UNION SQL Server will have to do an operation
to sort out duplicates - that may affect the choice of plan.



--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.



Relevant Pages

  • Re: question about a query
    ... JOIN QuesNans AS B ON A.topicid = B.topicid ... CROSS JOIN (SELECT 1 AS n UNION ALL ... This is a crosstab query. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: Error using a derived table
    ... to re-write your query without all of the nested SELECTs and the CASE ... > union all ... > select s.misspelledname as 'Resortname' ... I am using SQL Server 2000 EE. ...
    (microsoft.public.sqlserver.programming)
  • Re: Very slow query
    ... with lots if information in Books Online, Books, Courses, the Net etc. ... While testing the query in QA, use the "Show Estimated Query Plan" feature. ... This is what I suggest you also pick up using Profiler. ... Tibor Karaszi, SQL Server MVP ...
    (microsoft.public.sqlserver.programming)
  • Re: Database Engine Tuning Advisor suggestion to replace syntax.
    ... What you said was what I first planned to say, but then I read the Books Online which implies that the batch is indeed reused, so I held back from saying that. ... the query optimizer will probably match the Transact-SQL statement in the second execution with the execution plan generated for the first execution. ... SQL Server does not have to compile the second statement." ...
    (microsoft.public.sqlserver.tools)
  • Re: Establishing Precedence In ORDERBY Condition Causing Problems.
    ... in the query, prefix all your columns with aliases (or the table ... of the procedure to avoid duplicates it seems to have thrown off the ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)