Since indexes speed up searches, why wouldn't the DBMS automatically create an index for every column of a table?

StackOverflow https://stackoverflow.com/questions/22156001

  •  19-10-2022
  •  | 
  •  

Question

I have recently started reading about database for interview and came across this question. Can anyone help me with this?

No correct solution

OTHER TIPS

Basically because having too many SQL Indexes make UPDATEs and INSERTs to be slower and also cost disk space. So having indexes in columns that you are not going to use either for filter or order is a bad idea.

Also sometimes you would need a complex index (formed by two or more columns) so its ridiculous for the RDBMS to create an index for every possible combination of columns.

Ditto Rafa. Let me add:

Having an index makes find a desired record faster. But it makes inserting and deleting records slower and it uses more disk space and other resources.

So it's a trade-off. If you will rarely or never use a certain field to find a record, there's no point creating and maintaining an index for it. Like suppose you had a database with customer names. Indexing on last name probably makes sense. You probably do a lot of searches by last name. But if you had a field for Junior/Senior/Esq/etc, there likely wouldn't be much point in indexing on that. How often would you say, "We don't know his account number, we don't know his name, we don't know his address, but we do know that he's a 'Junior'".

If a database is read very often but updated rarely, you might go ahead and create indexes in the close cases. If a database is mostly written and rarely read -- like a transaction archive maybe -- that would be a reason to have fewer indexes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top