Say I have an ID row (int) in a database set as the primary key. If I query off the ID often do I also need to index it? Or does it being a primary key mean it's already indexed?
Reason I ask is because in MS SQL Server I can create an index on this ID, which as I stated is my primary key.
Edit: an additional question - will it do any harm to additionally index the primary key?
The primary key are the column(s) that serves to identify the rows. An index is a physical concept and serves as a means to locate rows faster, but is not intended to define rules for the table.
A primary key is unique, whereas an index does not have to be unique. Therefore, the value of the primary key identifies a record in a table, the value of the index not necessarily. Primary keys usually are automatically indexed - if you create a primary key, no need to create an index on the same column(s).
But in the database world, it's actually not necessary to create an index on the primary key column — the primary index can be created on any non primary key column as well.
A primary index is automatically created for the primary key and ensures that the primary key is unique. You can use the primary index to retrieve and access objects from the database. The unique index is a column, or an ordered collection of columns, for which each value identifies a unique row.
You are right, it's confusing that SQL Server allows you to create duplicate indexes on the same field(s). But the fact that you can create another doesn't indicate that the PK index doesn't also already exist.
The additional index does no good, but the only harm (very small) is the additional file size and row-creation overhead.
As everyone else have already said, primary keys are automatically indexed.
Creating more indexes on the primary key column only makes sense when you need to optimize a query that uses the primary key and some other specific columns. By creating another index on the primary key column and including some other columns with it, you may reach the desired optimization for a query.
For example you have a table with many columns but you are only querying ID, Name and Address columns. Taking ID as the primary key, we can create the following index that is built on ID but includes Name and Address columns.
CREATE NONCLUSTERED INDEX MyIndex ON MyTable(ID) INCLUDE (Name, Address)
So, when you use this query:
SELECT ID, Name, Address FROM MyTable WHERE ID > 1000
SQL Server will give you the result only using the index you've created and it'll not read anything from the actual table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With