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
 SQL Server Administration (2008)
 index suggestions

Author  Topic 

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-03-14 : 11:07:28
Hi,
I am performing some index tuning at a simple query. Assume there is a table called "Sales" where saves information of sales. See:

CREATE TABLE Sales
(product_name VARCHAR(15) NOT NULL
REFERENCES Products (product_name),
product_price DECIMAL(5,2) NOT NULL,
qty INTEGER NOT NULL,
sales_year INTEGER NOT NULL);
GO

And I have a SELECT statement like this:

SELECT product_name,
SUM(qty * product_price) AS year1
FROM Sales
WHERE sales_year = 1990
GROUP BY product_name;


I think a useful index suggestion on the query is:
CREATE NONCLUSTERED INDEX IX_0 ON Sales (sales_year) INCLUDE (product_name, qty, product_price)
But I am not completely sure. What index will be efficient for this? And Is a CLUSTERED index on the table necessary? (The table has not the primary key)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-03-14 : 17:23:17
A clustered index is not necessary, however it is highly recommended. Why don't you have a primary key on the table? Note that a primary key is not the same thing as a clustered index. A primary key can be clustered or non-clustered, but clustered is the default.

Yes your index looks good. Is product_name unique? If it is, I'd made that the primary key and have it be clustered.

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

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -