How can I measure the cost of a database index?

database, indexing, postgresql, sql

Solution

Generally speaking, unless you have a log or archive table where you wont be doing selects on very frequently (or it's ok if they take awhile to run), you should index on anything your select/update/deelete statements will be using in a where clause.

This however is not always as simple as it seems, as just because a column is used in a where clause and is indexed, doesn't mean the sql engine will be able to use the index. Using the `EXPLAIN` and `EXPLAIN ANALYZE` capabilities of postgresql you can examine what indexes were used in selects and help you figure out if having an index on a column will even help you.

This is generally true because without an index your select speed goes down from some O(log n) looking operation down to O(n), while your insert speed only improves from cO(log n) to dO(log n) where d is usually less than c, ie you may speed up your inserts a little by not having an index, but you're going to kill your select speed if they're not indexed, so it's almost always worth it to have an index on your data if you're going to be selecting against it.

Now, if you have some small table that you do a lot of inserts and updates on, and frequently remove all the entries, and only periodically do some selects, it could turn out to be faster to not have any indexes.. however that would be a fairly special case scenario, so you'd have to do some benchmarking and decide if it made sense in your specific case.

Problem

Is there a good method for judging whether the costs of creating a database index in Postgres (slower `INSERTS`, time to build an index, time to re-index) are worth the performance gains (faster `SELECTS`)?

Original source