views:

571

answers:

5

If I have a lookup table with very few records in it (say, less than ten), should I bother putting an index on the Foreign Key of another table to which it is attached? For that matter, does the lookup table even need an index on the Primary Key?

Specifically, is there any performance benefit that outweighs the overhead of maintaining the indexes? If not, are there any benefits other than speed?

Note: an example of a lookup table might be Order Status, where the tuples are:

1 - Order Received
2 - In Process
3 - Shipped
4 - Paid
+3  A: 

My personal opinion is that you should... it may be small now but ALWAYS anticipate your tables growing in size. A good database schema will grow easily with more records. Foreign Keys are almost always a good idea.

Polaris878
The table with the foreign key will most certainly grow in size. The Order Status table...not so much.
Robert Harvey
+1 Anticipate growth and provide the db engine with the tools it needs to make decisions about the query plan. If it does not need the index it will ignore it, and the cost of an index on a small table is pretty much nothing.
RedFilter
+4  A: 

Yes, always have an index.

The query optimizer of a modern database management system (DBMS) will make the determination as to which is faster: (1) actually reading from an index on a column, (2) performing a full table scan.

The table size (in number of rows) needs to be "large enough" for use of the index to be considered.

Aaron F.
Will the index ever be used, if there are only four records in the table?
Robert Harvey
@Robert Harvey - With only four records, probably not - the entire table is on a single datapage, so no I/O is saved with the index. On the other hand, the index is virtually free for only 4 rows that don't change, so you can add it in case the table grows later.
Jonathan
+1  A: 

In sql server, the primary key is the clustered index if there isn't one already (clustered index that is).

Kevin
Meaning, the question of index or no index on the lookup table is probably irrelevant?
Robert Harvey
No the question is legitimate, the foreign key is not automatically indexed.
HLGEM
I was answering if the lookup table needs an index on the primary key.
Kevin
+2  A: 

Yes to both. Always index as a rule of thumb.

Points:

  • You also can't set up an FK without a unique index on the lookup table
  • What if you want to delete or update in the lookup table? Especially accidently...

However, saying that, we don't always.

We have very OLTP table (5 million rows+ per day) with several parent tables. We only indexes on the FK columns where we need them. We assume no deletes/key updates on some parent tables, so we reduce the amount of work needed and disk space used.

We used the SQL Server 2005 dmvs to establish that indexes weren't used. We still have the FK in place though.

gbn
+3  A: 

On a transactional system there may be no significant benefit to putting an index on such a column (i.e. a low cardinality reference column) as the query optimiser probably won't use it. It will also generate additional disk traffic on writes to the table as the indexes have to be updated. So for low cardinality FK's on a transactional database it is usually better not to index the columns. This particularly applies to high volume systems.

Note that you may still want the FK for referential integrity and that the FK lookup on a small reference table will probably generate no I/O as the lookup table will almost always be cached.

However, you may find that you want to include the column in a composite index for some reason - perhaps to create a covering index for a commonly used query.

On a table that is frequently bulk-loaded (e.g. a data warehouse) the index write traffic will be much larger than that of the table load if you have many indexed columns. You will probably need to drop or disable the FKs and indexes for a bulk load if any indexes are present.

On a Star Schema you can get some benefit from indexing low cardinality columns, even on SQL Server. If you are doing a highly selective query (i.e. one where the query optimiser decides that the row set returned will be small) then it can do a 'star query' plan where it uses a technique known as index intersection.

Generally, query plans on a star schema should be based around a table scan of the fact table or a highly selective process that bookmarks the fact table and then returns a smaller set of rows. Index intersection is efficient for the latter type of query as the selection can be resolved before doing any I/O on the fact table.

Bitmap indexes are a real win for low cardinality columns on platforms such as Oracle that support them, but SQL Server does not. Even so, low cardinality indexes can still participate in star query plans on SQL Server.

ConcernedOfTunbridgeWells