Take the following two tables:
table A
has ~100k rows and 50 columns, and it is being updated only twice per month, how long this update takes is not an issue (as long as it doesn't take more than one day, which I think is unlikely :D). It will grow very slightly, not more than 10k rows per year.table B
has ~25k rows and 25 columns,column F
is a foreign key tocolumn C
oftable A
. This table is being updated 100-1000 times a day, most of the time to add new rows, so it will easily grow to hundreds of thousands of rows.
Table A
has a one-to-many relation to table B
, both column F
and column C
must be not null, have their own index, and column C
is also unique but it is not the primary key of table A
. Every row of table B
has a unique correspondence with a row in table A
.
Both tables are being queried many thousands of times every day, individually or using their relation using an application that allows a few dozen different types of query.
-
Is it a good idea to index every column of
table A
that is involved in a query, plus making a composite index for every type of query? If no, what are the possible drawbacks? -
For
table B
is it a good idea to make a composite index only for the most time consuming queries? Or should I make a composite index also for every column intable B
that is being queried, since those queries may vary in the parameters but not in the form?
Best Answer
The thing about indexes are that they are excellent tools for speeding up queries, but they bring along overhead as well.
My experience shows that indexing just about everything will hurt your performance overall, for the reasons mentioned above.
Both
table A
andtable B
should only contain the indexes that would be of best use for your queries.Of course, your first indexing decisions may need to be refined over time as you learn more about your database's behavior. Do not be afraid to add additional indexes when needed. And do not be afraid to drop an index that is not being useful.