How to use many LIKE operators and use index

前端 未结 1 1990
灰色年华
灰色年华 2021-01-03 01:56

In my query I want find rows that match one of many LIKE operators. I know 3 ways of doing that but only one of them can use index.

Lets start with table:



        
相关标签:
1条回答
  • 2021-01-03 02:35

    You can create a trigram index that will support your query.

    For that you need the pg_trgm extension; run the following as superuser:

    CREATE EXTENSION pg_trgm;
    

    Then you can create a GIN index:

    CREATE INDEX ON dir USING gin (path gin_trgm_ops);
    

    This index can be used with your second and third approach, so it should do the trick for you.

    With short patterns like the one in your examples, the index will not be very effective.

    You can also use a GiST index, that will probably be smaller, but slower to search.

    Note that you can use that index also with patterns that start with %.

    0 讨论(0)
提交回复
热议问题