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? 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
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.
> 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: psimakov |
last post by:
There is a new article out by Pavel Simakov entitled:
Javascript Refactoring for safer, faster, better AJAX.
...
|
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...
|
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++)
|
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);
|
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,...
|
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...
|
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....
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |