I have the following table/indexes -
CREATE TABLE test
(
coords geography(Point,4326),
user_id varchar(50),
created_at timestamp
);
CREATE INDEX ix_co
I think Craig is correct with his answer, but I just wanted to add a few things (and it wouldn't fit in a comment)
You have to work pretty hard to force PostgreSQL to use an index. The Query optimizer is smart and there are times where it will believe that a sequential table scan will be faster. It is usually right! :) But, you can play with some settings (such as seq_page_cost, random_page_cost, etc) you can play with to try and get it to favor an index. Here is a link to some of the configurations that you might want to examine if you feel like it is not making the correct decision. But, again... my experience is that most of the time, Postgres is smarter than I am! :)
Hope this helps you (or someone in the future).
I don't know if Pg can combine a GiST index and regular b-tree indexes with a bitmap index scan, but I suspect not. You may be getting the best result you can without adding a user_id
column to your GiST index (and consequently making it bigger and slower for other queries that don't use user_id
).
As an experiment you could:
CREATE EXTENSION btree_gist;
CREATE INDEX ix_coords_and_user_id ON test USING GIST (coords, user_id);
which is likely to result in a big index, but might boost that query - if it works. Be aware that maintaining such an index will significantly slow INSERT
and UPDATE
s. If you drop the old ix_coords
your queries will use ix_coords_and_user_id
even if they don't filter on user_id
, but it'll be slower than ix_coords
. Keeping both will make the INSERT
and UPDATE
slowdown even worse.
See btree-gist
(Obsoleted by edit to question that changes the question completely; when written the user had a multicolumn index they've now split into two separate ones):
You don't seem to be filtering or sorting on user_id
, only create_date
. Pg won't (can't?) use only the second term of a multi-column index like (user_id, create_date)
, it needs use of the first item too.
If you want to index create_date
, create a separate index for it. If you use and need the (user_id, create_date)
index and don't generally use just user_id
alone, see if you can reverse the column order. Alternately create two independent indexes, (user_id)
and (create_date)
. When both columns are needed Pg can combine the two indepependent indexes using a bitmap index scan.