By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,333 Members | 1,843 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,333 IT Pros & Developers. It's quick & easy.

compond index and key faster/better?

P: n/a
In a situation where you have two tables in a hierarchy like this:

create table authors (authorid int identity (1,1))

create table books (
authorid int,
bookid int identity (1,1)
)

Is there any disadvantage to having the primary key and the clustered
index as a compound key, like this:

alter table books add constraint PK_books primary key clustered
(authored, bookid)

Normally, I would make bookid the key, but then I got to thinking, most
of the queries are going to be "select * from books where authorid =
@@some_authorID"

So, wouldn't a compound key and index make this a little faster?

Aug 31 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
(er*******@gmail.com) writes:
create table books (
authorid int,
bookid int identity (1,1)
)

Is there any disadvantage to having the primary key and the clustered
index as a compound key, like this:

alter table books add constraint PK_books primary key clustered
(authored, bookid)
That looks a little funny. Since bookid is unique, why add authorid
to the PK?

Then again, PK of a books table should probably be the ISBN, as that
is a natural key.

And there should probably be a relation table, as a book can more than
one author.
Normally, I would make bookid the key, but then I got to thinking, most
of the queries are going to be "select * from books where authorid =
@@some_authorID"

So, wouldn't a compound key and index make this a little faster?


Having the clustered index on authorid is probably better than clustering
on ISBN, yes. But there not really any reason to add it to the PK of
books. (In an bookauthors table that covers the many-to-many relation
between books and authors, the key would make sense.)

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp

Aug 31 '05 #2

P: n/a
Erland Sommarskog wrote:
That looks a little funny. Since bookid is unique, why add authorid
to the PK?
Only for the purpose of having the clustered index that way. I think
my example would have been better if I'd made it a case of, "should I
have a simple or a compond index" and left the key out of it. My
mistake.
Then again, PK of a books table should probably be the ISBN, as that
is a natural key.
Well, this is just an example off the top of my head. Books and
authors is just the first think I came up with.
And there should probably be a relation table, as a book can more than
one author.
Same response as above.
Having the clustered index on authorid is probably better than clustering
on ISBN, yes. But there not really any reason to add it to the PK of
books.


I concede that there's no reason to have it in the PK. But, having a
compond clustered index seems like a good idea if you constantly want
to find all books by a given author. The books will then be returned
in bookid order.

Aug 31 '05 #3

P: n/a
> But, having a
compond clustered index seems like a good idea if you constantly want
to find all books by a given author. The books will then be returned
in bookid order.
Although it is likely that data will be returned in sequence by bookid, the
order is guaranteed only when bookid is specified in an ORDER BY clause. A
clustered index including bookid will facilitate efficient ordering in this
case.
--
Hope this helps.

Dan Guzman
SQL Server MVP

<er*******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com... Erland Sommarskog wrote:
That looks a little funny. Since bookid is unique, why add authorid
to the PK?


Only for the purpose of having the clustered index that way. I think
my example would have been better if I'd made it a case of, "should I
have a simple or a compond index" and left the key out of it. My
mistake.
Then again, PK of a books table should probably be the ISBN, as that
is a natural key.


Well, this is just an example off the top of my head. Books and
authors is just the first think I came up with.
And there should probably be a relation table, as a book can more than
one author.


Same response as above.
Having the clustered index on authorid is probably better than clustering
on ISBN, yes. But there not really any reason to add it to the PK of
books.


I concede that there's no reason to have it in the PK. But, having a
compond clustered index seems like a good idea if you constantly want
to find all books by a given author. The books will then be returned
in bookid order.

Sep 1 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.