Equals(=) vs. LIKE

后端 未结 15 1996
情书的邮戳
情书的邮戳 2020-11-22 15:19

When using SQL, are there any benefits of using = in a WHERE clause instead of LIKE?

Without any special operators, LIKE

相关标签:
15条回答
  • 2020-11-22 15:47

    One difference - apart from the possibility to use wildcards with LIKE - is in trailing spaces: The = operator ignores trailing space, but LIKE does not.

    0 讨论(0)
  • 2020-11-22 15:48

    Depends on the database system.

    Generally with no special characters, yes, = and LIKE are the same.

    Some database systems, however, may treat collation settings differently with the different operators.

    For instance, in MySQL comparisons with = on strings is always case-insensitive by default, so LIKE without special characters is the same. On some other RDBMS's LIKE is case-insensitive while = is not.

    0 讨论(0)
  • 2020-11-22 15:52

    This is a copy/paste of another answer of mine for question SQL 'like' vs '=' performance:

    A personal example using mysql 5.5: I had an inner join between 2 tables, one of 3 million rows and one of 10 thousand rows.

    When using a like on an index as below(no wildcards), it took about 30 seconds:

    where login like '12345678'
    

    using 'explain' I get:

    enter image description here

    When using an '=' on the same query, it took about 0.1 seconds:

    where login ='12345678'
    

    Using 'explain' I get:

    enter image description here

    As you can see, the like completely cancelled the index seek, so query took 300 times more time.

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