PostgreSQL LIKE query performance variations

后端 未结 8 2556
青春惊慌失措
青春惊慌失措 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:40

    Possibly the fast ones are anchored patterns with case-sensitive like that can use indexes. i.e. there is no wild card at the beginning of the match string so the executor can use an index range scan. (the relevant comment in the docs is here) Lower and ilike will also lose your ability to use the index unless you specifically create an index for that purpose (see functional indexes).

    If you want to search for string in the middle of the field, you should look into full text or trigram indexes. First of them is in Postgres core, the other is available in the contrib modules.

提交回复
热议问题