Table Indexing on PostgreSQL for performance

佐手、 提交于 2019-12-04 06:16:14

问题


I am solving performance issues on PostgreSQL and I have the following table:

 CREATE TABLE main_transaction (
   id integer NOT NULL DEFAULT nextval('main_transaction_id_seq'::regclass),
   description character varying(255) NOT NULL,
   request_no character varying(18),
   account character varying(50),
   ....
 )

Above table has 34 columns including 3 FOREIGN KEYs and it has over 1 Million rows data. I have the following conditional SELECT query:

SELECT * FROM main_transaction
WHERE upper(request_no) LIKE upper(concat('%','20080417-0258-0697','%'))

Returning the result in over 2 seconds. I want to decrease working time by using table indexing. So far, I have used btree indexing. However, I didn't notice any fast result. My question is, how can I improve performance for above query?


回答1:


Your only chance to search for a pattern that begins with % is a trigram index:

CREATE EXTENSION pg_trgm;

CREATE INDEX ON main_transaction
   USING gin (upper(request_no) gin_trgm_ops);


来源:https://stackoverflow.com/questions/49938650/table-indexing-on-postgresql-for-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!