Docs recommend using GIN index for array columns. However, I want to query by a combination of that column and a boolean column and I cannot add the boolean one to the index as GIN doesn't support that type. Am I better off (a) making a separate index for the boolean column, (b) using a different index type (which?), or (c) not indexing the boolean column given that in my case the result set from a search over the array column index will be just a few rows, so if the query optimizer searches for the matching boolean value within that it will only have a small number of comparisons?
create table foo (
id integer generated by default as identity primary key,
...
bar bool not null, -- TODO: Separate index? Cannot include bool in GIN index
...
baz smallint[] not null);
create index foo_baz_idx on lambdas using gin (baz);
Where most queries will be in the form of select * from foo where X = any(baz) and bar = Y
and there would be at most a small number of rows within a search for X
alone
It really depends on the nature of your data. If
where X = any(baz)
results in a small number of rows then there is no need to also indexbar
.If
where X = any(baz)
results in a large number of rows, a separate index onbar
may help; it would give the query planner more options. But since it's a boolean you can instead partition the table onbar
. Then every query is effectively indexed onwhere bar = ?
.