Postgres combining multiple Indexes

前端 未结 2 1528
别那么骄傲
别那么骄傲 2021-02-06 09:58

I have the following table/indexes -

CREATE TABLE test
(
   coords geography(Point,4326), 
   user_id varchar(50), 
   created_at timestamp
);
CREATE INDEX ix_co         


        
2条回答
  •  星月不相逢
    2021-02-06 10:49

    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 UPDATEs. 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.

提交回复
热议问题