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
 General SQL Server Forums
 New to SQL Server Programming
 Creating indexes

Author  Topic 

craigwg
Posting Yak Master

154 Posts

Posted - 2011-12-19 : 12:19:58
I have a series of tables I am developing for a new application. I have been asked to create indexes. I'm trying to code these by hand and not use automated tools. I want to post the structure of the table and the structure of a view that is created that uses a join. Then some questions about unique indexes and speed indexes.

Here is the table:


CREATE TABLE mqs
(
mqsId INT NOT NULL IDENTITY (1, 1),
mIdINT NOT NULL,
queueId INT NOT NULL,
deliveryId INT NOT NULL,
created DATETIME DEFAULT GETUTCDATE(),
updated DATETIME DEFAULT GETUTCDATE(),
CONSTRAINT pk_mqsId PRIMARY KEY (mqsId)
)


And the view:


SELECT
mqs1.mqsId,
mqs1.messageId,
mqs1.queueId,
mqs1.deliveryStateId,
mqs1.created
FROM mqs mqs1
INNER JOIN (SELECT messageId, queueId, created = max(created) FROM mqs GROUP BY messageId, queueId) mqs2
ON mqs1.messageId = mqs2.messageId
AND mqs1.queueId = mqs2.queueId
AND mqs1.created = mqs2.created
GROUP BY mqsId, mqs1.messageID, mqs1.queueId, mqs1.deliveryStateId, mqs1.created


I have planned to create a unique index on mqs using the columns messageId, queueId, and deliveryStateId.

I want to create a speed index but not sure what to do here. I am guessing on the columns used in the join: messageId,queueId and created. But I'm not sure. Nor do I quite understand the concept of using includes in an index.

Can someone help break it down?


Craig Greenwood

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-19 : 12:22:12
you should be creating clustered/non clustered indexes on table columns based on how they're used in the query.
see

http://www.simple-talk.com/sql/learn-sql-server/sql-server-index-basics/

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

craigwg
Posting Yak Master

154 Posts

Posted - 2011-12-19 : 12:29:53
The table in question here has a primary key constraint and thus a clustered index. Confirmed this by looking at the interface in SSMS. So my question in in regards to non clustered indexes. I'm reading the article you gave me now.

Craig Greenwood
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-19 : 12:36:48
ok then you need to look for non clustered options

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

GilaMonster
Master Smack Fu Yak Hacker

4507 Posts

Posted - 2011-12-19 : 14:36:18
Not about indexes, but why is there a group by in the outer query when there's no aggregation.

For indexes, maybe take a look at this series (3 parts)
http://www.sqlservercentral.com/articles/Indexing/68439/
http://www.sqlservercentral.com/articles/Indexing/68563/
http://www.sqlservercentral.com/articles/Indexing/68636/

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

craigwg
Posting Yak Master

154 Posts

Posted - 2011-12-19 : 17:39:27
Groupby noted and corrected. Thanks. Result of multiple staged at development.

Craig Greenwood
Go to Top of Page
   

- Advertisement -