PostgreSQL LIKE query performance variations

后端 未结 8 2532
青春惊慌失措
青春惊慌失措 2020-11-22 00:54

I have been seeing quite a large variation in response times regarding LIKE queries to a particular table in my database. Sometimes I will get results within 20

8条回答
  •  迷失自我
    2020-11-22 01:15

    I recently had a similar issue with a table containing 200000 records and I need to do repeated LIKE queries. In my case, the string being search was fixed. Other fields varied. Because that, I was able to rewrite:

    SELECT owner1 FROM parcels
    WHERE lower(owner1) LIKE lower('%someones name%');
    

    as

    CREATE INDEX ix_parcels ON parcels(position(lower('someones name') in lower(owner1)));
    
    SELECT owner1 FROM parcels
    WHERE position(lower('someones name') in lower(owner1)) > 0;
    

    I was delighted when the queries came back fast and verified the index is being used with EXPLAIN ANALYZE:

     Bitmap Heap Scan on parcels  (cost=7.66..25.59 rows=453 width=32) (actual time=0.006..0.006 rows=0 loops=1)
       Recheck Cond: ("position"(lower(owner1), 'someones name'::text) > 0)
       ->  Bitmap Index Scan on ix_parcels  (cost=0.00..7.55 rows=453 width=0) (actual time=0.004..0.004 rows=0 loops=1)
             Index Cond: ("position"(lower(owner1), 'someones name'::text) > 0)
     Planning time: 0.075 ms
     Execution time: 0.025 ms
    

提交回复
热议问题