473,426 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,426 software developers and data experts.

compond index and key faster/better?

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
3 1657
(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
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: SQLServer007 | last post by:
Download a FREE fully functional trial from http://www.x-sql.com NEW FOR xSQL OBJECT VERSION 1.5: - Support for full-text indexes and full-text catalogs; - Support for scripting of User's...
1
by: psimakov | last post by:
There is a new article out by Pavel Simakov entitled: Javascript Refactoring for safer, faster, better AJAX. ...
10
by: Extremest | last post by:
I know there are ways to make this a lot faster. Any newsreader does this in seconds. I don't know how they do it and I am very new to c#. If anyone knows a faster way please let me know. All...
17
by: onkar | last post by:
which one runs faster ?? for(i=0;i<100;i++) for(j=0;j<10;j++) a=0; OR for(j=0;j<10;j++) for(i=0;i<100;i++)
26
by: Vijay Kumar R. Zanvar | last post by:
Why does this program print 1 for a. I thought it should be an OOB access situation! #include <stdio.h> #include <stdlib.h> int main() { int a={1,2,3,4,5}; printf("%d\n", a);
11
by: Mahdi | last post by:
Hi This seems like a crazy question but I wanted to find out if anyone knows which one of those two conditions are better (faster, smaller memory footprint, can be optimized by the compiler,...
1
by: Steffen Stellwag | last post by:
Truely is often better to scan a table in full passing by an index , but if you can force the optimizer to use an index via a hint for testing and comparing the results. But the index in the...
84
by: Patient Guy | last post by:
Which is the better approach in working with Javascript? 1. Server side processing: Web server gets form input, runs it into the Javascript module, and PHP collects the output for document prep....
6
by: Henry J. | last post by:
I have a composite index on two columns in a table. However, the index is not used in a query that restricts the 2nd column to a constant. If both columns are linked with columns in other join...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.