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
 Index Covering Query

Author  Topic 

krisrajz
Starting Member

19 Posts

Posted - 2010-11-16 : 01:45:47
Hello,

How do I write index covering query that involves multiple tables?

Thank you

Regards
Raj

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-16 : 03:09:14
More information needed.

A covering index is an index that contains within it all the columns that a query needs from that table. I don't know what you mean by 'index covering query'

--
Gail Shaw
SQL Server MVP
Go to Top of Page

krisrajz
Starting Member

19 Posts

Posted - 2010-11-16 : 05:04:24
Want to create a covering index for the below query:
select Dname,Ename,Designation,Salary from Emp,Dept
where Emp.Deptno=Dept.Deptno
and ZipCode in ('Z01','Z02','Z03','Z04')
order by Emp.employeeno

Index for EmployeeNo, Deptno are already created

Now I need to create index covering query for the columns in the above SQL query. The able has huge amount of data and is going to be joined with few other tables too!

Thank you

Regards
Raj
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-16 : 05:30:28
First things first, write the query properly.

select Dname,Ename,Designation,Salary
from Emp inner join Dept on Emp.Deptno=Dept.Deptno
where ZipCode in ('Z01','Z02','Z03','Z04')
order by Emp.employeeno

If you want index help, post the definitions of all the indexes that are currently on the tables please.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

krisrajz
Starting Member

19 Posts

Posted - 2010-11-18 : 00:35:58
Employee number is primary key in Emp table and hence Clustered Index was already created
Deptno number is primary key in Dept table and hence Clustered Index was already created
Additionally I have created non-unique non-clustered index on deptno column of emp table

Raj
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2010-11-18 : 00:46:03
Looks like for this query, you want to create an index on ZipCode, and Include any other columns in the Select list. I don't know which columns are in which table so I can't say for sure what goes in it, but that is the only useful index I can see outside of the clustered indexes. (Looking at this query in isolation)

-Chad
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-18 : 03:16:00
Which table are the following in?
ZipCode
Designation
Salary

--
Gail Shaw
SQL Server MVP
Go to Top of Page

krisrajz
Starting Member

19 Posts

Posted - 2010-11-18 : 22:52:42
Dname is from Department's(DEPT) table and the rest from Employee's(EMP) table
ZipCode is in Department(DEPT) table

Raj

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-19 : 02:44:41
Create an index on DEPT on ZipCode, DeptNo Include DName
Change the index on deptNo on Emp and include EName, Designation, Salary

Do note that this is creating two indexes tailored for one query. It's generally a better idea to try and create indexes so that they are usable by as many queries as possible.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2010-11-19 : 23:45:11
quote:
Create an index on DEPT on ZipCode, DeptNo Include DName


No need to have DeptNo as part of the key, it is there automatically because it is the clustered index key.

-Chad
Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-20 : 07:07:55
quote:
Originally posted by chadmat

No need to have DeptNo as part of the key, it is there automatically because it is the clustered index key.


I would still recommend putting it there for a number of reasons
1) SQL's smart enough not to add it again
2) It makes it very clear to anyone reading the index what the required keys are
3) If the index is widened in the future, the position of DeptNo will not change

--
Gail Shaw
SQL Server MVP
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-11-20 : 12:18:07
"1) SQL's smart enough not to add it again"

I didn't know that, and it is certainly useful to know it! Thanks.
Go to Top of Page

krisrajz
Starting Member

19 Posts

Posted - 2010-11-24 : 00:59:52
Thanks indeed Gail Shaw!

Raj
Go to Top of Page

krisrajz
Starting Member

19 Posts

Posted - 2010-11-24 : 01:05:49
I still have some queries

>>Create an index on DEPT on ZipCode, DeptNo Include DName
>>Change the index on deptNo on Emp and include EName, Designation, Salary

>>Do note that this is creating two indexes tailored for one query. It's generally a better idea >>to try and create indexes so that they are usable by as many queries as possible.

You suggested to create two more indexes, 1 each on EMP and DEPT table.

I thought of having 3 indexes on DEPT

1. Deptno - Clustered (default)
2. DName - Unique, Non Clustered
3. ZipCode - Non-Unique, Non Clustered

Raj

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2010-11-24 : 02:43:18
Create whatever indexes you like. The ones I suggested are optimal for this query (do you understand why?)

Single column nonclustered indexes are generally not all that useful. Wider (within reason) is usually better. You asked for covering indexes. None of the indexes you proposed are covering for this query.

--
Gail Shaw
SQL Server MVP
Go to Top of Page

krisrajz
Starting Member

19 Posts

Posted - 2010-11-24 : 04:26:24
I understand, for deterministic queries (fixed columns) index based on multiple columns is better choice both storage and as well as performance wise.

Thank you for all the suggestions

Regards
Raj
Go to Top of Page
   

- Advertisement -