tags:

views:

27

answers:

4

I have a mysql table of 3 integer fields. None of the fields have a unique value - but the three of them combined are unique.

When I query this table, I only search by the first field.

Which approach is recommended for indexing such table? Having a multiple-field primary key on the 3 fields, or setting an index on the first field, which is not unique?

Thanks, Doori Bar

+2  A: 

Both. You'll need the multi-field primary key to ensure uniqueness, and you'll want the index on the first field for speed during searches.

eykanal
What if I don't need to 'ensure uniqueness', so you'd recommend simply having an index on the first field, with no primary key?
Doori Bar
The point of "uniqueness" is to ensure that you don't end up with two identical rows. This is a fundamental part of database design.
eykanal
A: 

You can have a UNIQUE Constraint on the three fields combined to meet your data quality standards. If you are primarily searching by Field1 then you should have an index on it.

You should also consider how you JOIN this table.

Your indexes should really support the bigger workload first - you will have to look at the execution plan to determine what suits you best.

Raj More
if the composite PK has Field1 first, an index on that field is unnecessary.
dnagirl
A: 

The primary key will prevent your application from accidenttly inserting dupe rows. You probably want that.

Order the columns in the PK correctly though or make an index on the first column clustered for better performance. Compare how the query runs (with the PK present) and with and without the index on the first column.

Tom Ritter
I don't quite understand why all of you mentioned the concept of 'uniqueness' ... if the table is part of the application design, why would I need to set 'rules' which passively 'preventing' my application from doing something that cannot happen by design?I really can't grasp this concept, from my POV - the database's data and validation being govern by the application, not the other way around
Doori Bar
A: 

If you're using InnoDB, you must have a clustered index. If you don't specify one, MySQL will use one in the background anyway. So, you may as well use a clustered (unique) primary key by combining all three columns.

The primary key will also then prevent duplicates, which is a bonus.

If you're returning all three integer fields, then you'll have a covered index, which means that the database won't even have to touch the actual record. It will get everything it needs right from the index.

The only caveat would be inserts (and appends). Updating a clustered index, especially on multiple columns, does have some performance penalization. It will be up to you to test and determine the best approach.

Marcus Adams