mysql datetime field with index get a range 'like' vs. 'between and' performance

后端 未结 2 1314
情书的邮戳
情书的邮戳 2021-02-19 19:32

I just find mysql can query datetime using like:

like \'2013-06-12%\'

I think it can not use the index. I Google it, but can not find such subj

2条回答
  •  难免孤独
    2021-02-19 20:03

    Assuming that t.active_time's type is DATETIME,

    The following query cannot use an index because of the function call. All active_time must be converted on-the-fly to a DATE value before comparison with '2013-06-30'. This string is converted to a DATE value in the very first place, and this happens only once at the very beginning of the query.

    SELECT * FROM subscription t WHERE DATE(t.active_time) = '2013-06-30';
    

    The second query cannot use an index either for similar reasons. You are actually making a string comparison (because of the LIKE operator). All active_time values are converted to a string on-the-fly.

    SELECT * FROM subscription t WHERE t.active_time LIKE '2013-06-30%';
    

    Only the last one can use an index. The strings '2007-11-30' and '2007-12-01' are cast to DATETIME in this case, because the < and > operators allow this.

    SELECT * FROM subscription t WHERE t.active_time > '2007-11-30' AND t.active_time < '2007-12-01';
    

    The latter also applies to the = and BETWEEN operators.

    For information, all types can be compared to a string with the LIKE operator, suffering from the same problem as described above, because of the implicit conversion it requires.

    t.active_time = '2013-06-30' does not work as expected because '2013-06-30' is cast to a DATETIME value, that is to say '2013-06-30 00:00:00'.

提交回复
热议问题