3.9 Index Write Cost

If indexes improve read performance, should we create as many indexes as possible? No, because an index must be modified every time a record changes.

Suppose we add one new customer. When indexes exist, it is not enough to create the customer record in a data page. The new record must be inserted into every index that has been created, such as the customer number index, the name index, and the region index. With four indexes, adding a customer creates a total of 5 records. Deleting and updating records work the same way. If a customer's name is changed, the name index must reflect it too.

An index also occupies storage and takes up space in the buffer pool. In exchange for making reads fast, an index makes writes slower and requires a lot of additional storage. It is good to use indexes on tables that are read more often. On tables with far more writes, however, creating an index calls for careful consideration.

ItemPostgreSQLMySQL (InnoDB)OracleAzure SQL
Default index structureB+Tree familyB+TreeB+Tree familyB+Tree
Table storageHeapClustered (by PK)Heap (IOT optional)Heap or clustered, selectable
What a secondary index points toRow addressPrimary key valueRow addressClustering key or row address
Expression/conditional indexExpression index, partial indexFunctional index since 8.0Function-based indexFiltered index