Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL primary key - complex primary or string with concatenation?

I have a table with 16 columns. It will be most frequently used table in web aplication and it will contain about few hundred tousand rows. Database is created on sql server 2008.

My question is choice for primary key. What is quicker? I can use complex primary key with two bigint-s or i can use one varchar value but i will need to concatenate it after?

like image 738
Siblja Avatar asked Feb 15 '26 13:02

Siblja


1 Answers

There are many more factors you must consider:

  • data access prevalent pattern, how are you going to access the table?
  • how many non-clustered indexes?
  • frequency of updates
  • pattern of updates (sequential inserts, random)
  • pattern of deletes

All these factors, and specially the first two, should drive your choice of the clustered key. Note that the primary key and clustered key are different concepts, often confused. Read up my answer on Should I design a table with a primary key of varchar or int? for a lengthier discussion on the criteria that drive a clustered key choice.

Without any information on your access patterns I can answer very briefly and concise, and actually correct: the narrower key is always quicker (for reasons of IO). However, this response bares absolutely no value. The only thing that will make your application faster is to choose a key that is going to be used by the query execution plans.

like image 79
Remus Rusanu Avatar answered Feb 17 '26 02:02

Remus Rusanu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!