Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Other SQL Server 2008 Topics
 views, unions, indexes

Author  Topic 

malvernlloyd
Starting Member

3 Posts

Posted - 2011-11-23 : 12:58:13
Let's say we have three databases, db1, db2, and db3. db1 and db2 have data, whereas db3 merely points at both of them and consists of views, like this:

create view db1_customers as
select 'db1' as db, customers.* from db1.dbo.customers

create view db2_customers as
select 'db2' as db, customers.* from db2.dbo.customers

create view all_customers as
select * from db1_customers
union
select * from db2_customers


Would the following query, executed in db3, be able to make use of the ix_customer_zipcode index on each of the customers tables in their respective databases, db1 and db2?

select * from all_customers where zipcode = '10025'

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-23 : 13:12:26
Yes. A view like this though, you may want to make this a partitioned view.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

malvernlloyd
Starting Member

3 Posts

Posted - 2011-11-23 : 14:31:22
When I examine the actual query Execution Plan, there is no use of the index on zip. Is the use of the index not automatic?


Select ..Stream Aggregate .. Sort....Concatenation..Compute Scalar......Table Scan

Cost: 0% .. Cost: 0%........Cost:63%...Cost: 0%........Cost: 0%..........Cost: 18%

......................................................Compute Scalar.....Table Scan
........................................................Cost: 0% ........Cost: 18%
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-11-23 : 14:51:17
Maybe your particular query wasn't going to use an index anyway?

If you don't already know the Ins & Outs of partitions suggest you look into that ... your example looks like that might be exactly what you are actually trying to achieve.
Go to Top of Page

malvernlloyd
Starting Member

3 Posts

Posted - 2011-11-23 : 15:26:55
What we have is a scenario where satellite locations with identical database structures are sending full backups periodically to a central data warehouse for consolidated reporting. These databases predate my arrival--I've inherited the situation.

Each of these databases contains about 150 tables. I don't think partitioning would simplify the logistics of this kludgy replication scenario. Right now it's a simple restore. If we used partitions, we would have to load 150 tables.

The databases were not designed with replication in mind: there are PK collisions because of autoincrementing integer PKs; each site was not assigned a range of values to prevent collisions.

So I was hoping to be able to create views as below, but I wasn't sure whether the indexes on the base tables would be used because I've never used views that are created by UNIONing several views, each of which goes against a table in some other database:

create view FOO as
select * from someView [which is based on a table from db1]
union
select * from anotherView [which is based on a table from db2]
union
select * from yetAnotherView [which is based on a table from db6]

The test table I'm working with has a single index on zipcode. It appears in db1 and db2 (and so forth) and each table contains about 1 million rows. The indexes are never used when view FOO is queried; each of the underlying tables is scanned.

I don't see where a table scan of a million rows would be more efficient than using the index, so something appears to be keeping the query against FOO from using the indexes on the ZIP column in the underlying base table.

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2011-11-23 : 15:56:35
"I don't see where a table scan of a million rows would be more efficient than using the index"

Statistics not up to date?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-11-23 : 15:59:53
Partitioned views does solve your problem. You need to look into it.

What is inside those views? If you don't have a good where clause on them and no suitable index for the where clause, then a scan is correct.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -