Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql primary key and index

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?

like image 709
danifo Avatar asked Jan 20 '09 18:01

danifo


People also ask

What is difference between index and primary key in SQL?

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.

Is primary key the same as index?

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).

Do you need index If you have primary key?

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.

Does a primary key automatically create an index?

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.


2 Answers

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.

like image 64
dkretz Avatar answered Sep 26 '22 20:09

dkretz


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.

like image 24
red.clover Avatar answered Sep 24 '22 20:09

red.clover