Postgresql – If i index two columns (indepentently) do i get the same performance as putting and index on the two

indexindex-tuningpostgresql

I want to put an index on two columns to improve my query performance for each. I also want to perform queries where both columns are used.

From my understanding, if you had a BTREE index on each column, you could theoretically get the same performance by querying both indices and then combining the range you got – if that makes sense?


e.g.

SELECT * FROM table WHERE f_name = 'first' and l_name = 'last'

Instead checking an index for ('first', 'last') you'd check the f_name index for first and the l_name index for last. Now, you can check if the results of both index queries are the same (the result of the initial query). Obviously, this is slightly slower than a composite index, but still much faster than no index.

With this idea, you get performance benefits not only when querying both columns together, but either column independently.

My main question is: if I have these 2 independent indices, would I need another index on both, to improve the performance of queries using both columns?

Best Answer

Your question is a bit jumbled. No, you can't get theoretically the same performance, unless one of the two columns is unique or nearly unique. But yes, you can get performance that while slower than the composite is still much faster than no index at all. Will it be fast enough that you don't need the composite index? No one can answer that but you. We don't know either how fast you need it to be, nor how selective the condition against each column will be (both independently and jointly).

But if you are going to have indexes on (a) and on (b), you might as well just change the first one to be on (a,b) or change the second to be on (b,a). The extra cost will be nearly negligible (unless the added column contains wide data values) while the extra value at least has the potential to be large. The cost might be slightly higher in v13, because having the composite index might defeat the new deduplication logic compared to the one with just the prefix column.

Doing a detailed analysis of whether it is worthwhile is probably not worthwhile.