by snake_plissken · 4y ago · 0 comments
"Since indices are only used to speed up the searching for a matching field within the records, it stands to reason that indexing fields used only for output would be simply a waste of disk space and processing time when doing an insert or delete operation, and thus should be avoided."
Specifically "indexing fields used only for output", what does this mean? I interpreted it as "indexes used only for `select` statements", but that would see completely counter to the main reason you'd want an index e.g. to speed up record retrieval.
Suppose you have this query:
select a, b
from T
where x = 1
And suppose you have an index on x. You can locate the x=1 INDEX records using the index quickly (probably no more than 1-3 disk accesses). But then the qualifying TABLE records have to be retrieved. That index lookup could turn up thousands of qualifying records, and now you have to retrieve each record to get the a, b values.Now suppose that you replace your index on (x) with an index keyed by (x, a, b). You can still search for x=1 using this index, but now, the (a, b) values that you are SELECTing are present in the index itself. You don't have get the table records to get those values. That can save a lot of random accesses, and there are secondary effects, since the pages containing those records aren't brought into the disk cache.
Yes, this wider index has space and update costs, but the benefit is often worth it.
[1]: https://www.postgresql.org/docs/current/sql-createindex.html
I was teaching a database course a couple of years ago, using Postgres, and in exercises on query optimization, I found it surprisingly difficult to get columns added to indexes to produce a convincing improvement.
They are just SELECTed, i.e. sent in the output (the query results).