(codefragment@googlemail.com) writes:
> I thought that given a table with an index, primary key and
> clustered index any non clustered index look ups would go via the
> clustered index and the primary key is irrelevant?
> (sql server 2000). A colleague has said that the primary key should be
> the clustered index
> because all index lookups will go via the primary key.
> Is this right? I thought the primary key was nothing more than a
> constraint on what data can be entered into the table.
> Does it matter if the primary key is the same as the clustered
> index?
Look-ups often go through the PK, since the reference is through the
PK. If you have a query like:
SELECT O.OrderDate, O.OrderID, C.CompanyName
FROM Orders O
JOIN Customers C ON O.CustomerID = C.CustomerID
WHERE O.EmployeeID = 1
The access to the Customers table is like to use the PK, no matter whether
this index is clustered or not.
Sometimes it is a very good idea to have the PK as the clustered index.
Sometimes the PK is a poor choice for the clustered index. For instance,
for an Orders table the order id is not likely to be a good choice.
How often do you run queries like "give me all orders with order
id between 11000 and 12000?". The order date, the customer id and
the employee id are all better choices.
On the other hand, for an OrderDetails table, it make sense to cluster
on the PK, because you will often read all details for one order.
--
Erland Sommarskog, SQL Server MVP, esquel DeleteThis @sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: confusion over indexs via primary key and clustered index