LOWER LIKE vs iLIKE

后端 未结 3 1393
名媛妹妹
名媛妹妹 2020-12-04 12:34

How does the performance of the following two query components compare?

LOWER LIKE

... LOWER(description) LIKE \'%abcde%\' ...
         


        
3条回答
  •  旧巷少年郎
    2020-12-04 12:36

    In my rails Project. ILIKE is almost 10x faster then LOWER LIKE, I add a GIN index on entities.name column

    > Entity.where("LOWER(name) LIKE ?", name.strip.downcase).limit(1).first
    Entity Load (2443.9ms)  SELECT  "entities".* FROM "entities" WHERE (lower(name) like 'baidu') ORDER BY "entities"."id" ASC LIMIT $1  [["LIMIT", 1]]
    
    > Entity.where("name ILIKE ?", name.strip).limit(1).first
    Entity Load (285.0ms)  SELECT  "entities".* FROM "entities" WHERE (name ilike 'Baidu') ORDER BY "entities"."id" ASC LIMIT $1  [["LIMIT", 1]]
    
    # explain analyze SELECT  "entities".* FROM "entities" WHERE (name ilike 'Baidu') ORDER BY "entities"."id" ASC LIMIT 1;
                                                                       QUERY PLAN
    ------------------------------------------------------------------------------------------------------------------------------------------------
     Limit  (cost=3186.03..3186.04 rows=1 width=1588) (actual time=7.812..7.812 rows=1 loops=1)
       ->  Sort  (cost=3186.03..3187.07 rows=414 width=1588) (actual time=7.811..7.811 rows=1 loops=1)
             Sort Key: id
             Sort Method: quicksort  Memory: 26kB
             ->  Bitmap Heap Scan on entities  (cost=1543.21..3183.96 rows=414 width=1588) (actual time=7.797..7.805 rows=1 loops=1)
                   Recheck Cond: ((name)::text ~~* 'Baidu'::text)
                   Rows Removed by Index Recheck: 6
                   Heap Blocks: exact=7
                   ->  Bitmap Index Scan on index_entities_on_name  (cost=0.00..1543.11 rows=414 width=0) (actual time=7.787..7.787 rows=7 loops=1)
                         Index Cond: ((name)::text ~~* 'Baidu'::text)
     Planning Time: 6.375 ms
     Execution Time: 7.874 ms
    (12 rows)
    

    GIN index is really helpful to improve ILIKE performance

提交回复
热议问题