Create partial index with single value

by csny   Last Updated June 29, 2020 20:06 PM

I have PostgreSQL 12 DB with a table with a several columns, two of them are a bigint ("numCol") and a boolean ("boolCol"). numCol can have any integer number, and 0 is about 0.1%-5% of the table. It is frequently updated. The table can be big (>1B rows)

I have frequent queries for numCol=0 and boolCol is false, so I created a partial btree index:

myindex(numCol,boolCol) where numCol = 0 and boolCol is false.

Although the query performance is good, this index will have a single value, and I think that btree might consume more disk space than necessary.

Is there a more suitable index type for this case? What is the best practice in such cases?



Answers 1


A partial index typically doesn't need the columns from the WHERE clause in the index as well. You should put other columns that you retrieve in the queries that make use of that index in the column. e.g. the PK or something else.

So if you e.g. frequently use this query:

select pk_column, other_column
from the_table
where num_col = 0
and bool_col = false;

Then it usually makes sense to create the index like this:

create index on the_table (pk_column, other_column)
where num_col = 0
and bool_col = false;

That way the index scan can be used to also get the values needed for the

a_horse_with_no_name
a_horse_with_no_name
June 29, 2020 19:40 PM

Related Questions




space usage on sys.allocation_units and sp_spaceused

Updated August 04, 2015 16:03 PM