Unix Technical Forum

Indexes being improperly used when selecting data through a view

This is a discussion on Indexes being improperly used when selecting data through a view within the SQL Server forums, part of the Microsoft SQL Server category; --> I am having a problem with indexes on specific tables. For some reason a query that runs against a ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-29-2008, 01:55 PM
joshsackett
 
Posts: n/a
Default Indexes being improperly used when selecting data through a view

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)

ARCHIVE1:
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)

REPORTDB:
CREATE VIEW MyTest1 AS
SELECT ID, COLUMN1, COLUMN2 FROM PRODUCTION1..MyTest1
UNION ALL
SELECT ID, COLUMN1, COLUMN2 FROM ARCHIVE1..MyTest1

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

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

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-29-2008, 01:55 PM
joshsackett
 
Posts: n/a
Default Re: Indexes being improperly used when selecting data through a view

Correction... "UNION ALL" should just say "UNION"

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-29-2008, 01:56 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Indexes being improperly used when selecting data through a view

joshsackett (joshsackett@gmail.com) 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@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 02:13 PM
joshsackett
 
Posts: n/a
Default Re: Indexes being improperly used when selecting data through a view

I realized that it had to do with the UNION command. It was performing
a poor search in the first place and when the UNION ran it had to
perform a sort.

Thanks for the help.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:59 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com