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 1669
(er*******@gmai l.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****@sommarsk og.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*******@gmai l.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 login, User's Membership to Database
Role, User's Login Membership to SQL Server Fixed roles;
- Ability to instruct the compare engine to exclude comments, both
multi-line and single-line, from Views, Stored Procedures, User
Defined Functions and...
|
by: psimakov |
last post by:
There is a new article out by Pavel Simakov entitled:
Javascript Refactoring for safer, faster, better AJAX.
http://www.softwaresecretweapons.com/jspwiki/Wiki.jsp?page=JavascriptRefactoringForSaferFasterBetterAJAX
He argues that its time for Javascript coding practices to mature into
professional software engineering, discusses various ways to improve
Javascript code and has examples of Javascript refactoring from
real-life projects....
|
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 I am doing is quering the db for
all the headers for a certain group and then going through
them to find all the parts of each post. I only want ones
that are complete. Meaning all segments for that one file
posted are there.
using System;
|
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, etc..)
1- if(i 0) {// do stuff}
2- if(i >=1) {// do stuff}
Thanks.
|
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 above example is not used , because the hint is
malformed,
if tables in a Select statment are named by aliases you have to
specify the alias name in the hint statment , not the table name
/*+ INDEX (ICWOIMP PK_ICWOIMP) */ change to /*+ INDEX (A...
|
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.
2. Client side processing: Web server gets form input and passes it to PHP
which includes the Javascript written in a way to make the form input
processed on the client side and rendered (probably using DOM function
calls) on that side as...
|
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
tables, the index will be used.
To illustrate it with an example, I have a query like this:
select s.ticker, p.quantity
from stock s, positions p
where s.type_id = 4
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |