Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a table with a Composite Primary key (consisting of 4 columns) which is used to ensure no duplicates are entered into the table. I am now in need of a new table which will need to reference the keys in this table as foreign keys.

My question is which approach is more efficient for lookup speeds:

1) Do I create the new table including all 4 columns and reference them all in a foreign key.

or

2) Do I create a new identity column in the Primary Key table and use this as a foreign key in the new table.

This database is expected to hold a very large amount of data, so I have built it up until now with a view to minimising the amount of data held in each table. With this in mind, option 2 would be the best approach since I will save 2 int columns and a datetime column for every row, but I want to avoid increasing the lookup time if unnecessary.

share|improve this question
1  
I personally would almost always use a surrogate key (e.g. an INT IDENTITY) in such a case - makes referencing and joining that table just soooooo much easier. To avoid duplicates, put a UNIQUE constraint on those four columns. Also: narrow primary keys are much better for performance reasons (if they're used as clustering key) –  marc_s Oct 3 '13 at 14:13

2 Answers 2

up vote 3 down vote accepted

The cost of using a simple synthetic integer PK is small, and the benefit in your case would probably be quite considerable.

  • As you point out, you'll have a much simpler FK relationship.
  • A small PK makes for small (and fast) indices. Your total table space will probably be made less by adding such a column.
  • If business rules ever change, you won't have to reorder the table.

The only material downside that comes to mind is that you may lose performance on queries which benefited from clustering on the composite PK. If you think that's likely to be significant, than continue clustering on the composite candidate key, but put the PK on the synthetic key.

share|improve this answer

As so often in the SQL world, the answer is: "It depends."

Have a look at this question for some pointers: Do natural keys provide higher or lower performance in SQL Server than surrogate integer keys?

There are cases that see a performance improvement when using natural keys as Foreign Keys. However, in most cases you are going to be better of with smaller keys (read: surrogate keys).

If you introduce that IDENTITY column I would even make it the Primary Key and change the "natural" columns to be a UNIQUE CONSTRAINT instead.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.