Postgresql: Matching Patterns between Two Columns

后端 未结 3 934
名媛妹妹
名媛妹妹 2021-01-20 00:27

I have two columns say Main and Sub. (they can be of same table or not).

Main is varchar of length 20 and Sub is

3条回答
  •  猫巷女王i
    2021-01-20 01:09

    Argument to a LIKE is an ordinary string, so all string manipulations are valid here. In your case you need to concatenate wildchars with the target substring, like @bksi suggests:

    ... LIKE '%'||CAST("SubColumn" AS test) ...
    

    Note, though, that such patterns (the ones starting with a % wildcard) are badly performing ones. Take a look at PostgreSQL LIKE query performance variations.

    I would recommend:

    • sticking with the current substr("MainColumn", 13, 8) approach;
    • avoid LIKE and use equality comparison (=) instead (although they're equal if LIKE pattern contains no wildcards, it is easier to read the query);
    • build an expression index on the "MainTable" the following way:

      CREATE INDEX i_maincolumn ON "MainTable" (substr("MainColumn", 13, 8));
      

    This combination will perform better in my view.

    And use lowercase names for the tables/columns, so that you can avoid doublequoting them.

提交回复
热议问题