简体   繁体   中英

Variable in subquery not using index in timestamp field

I'm trying to optimize a query but when I use a variable in a subquery, the index is not being used.

set @dh = '2018-01-17 23:59:59'
...
inner join cons c1 on c1.idcons = xx.maxcons
left join conslog clog on clog.idconslog = (select max(clt.idconslog)
                                              from conslog clt
                                              where clt.idcons = c1.idcons
                                              and clt.date_create <= @dh)
...

I get with explain

+----+-------------+-------+------+---------------+-----+---------+-----+----------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref |   rows   |                       Extra                        |
+----+-------------+-------+------+---------------+-----+---------+-----+----------+----------------------------------------------------+
|  1 | PRIMARY     | clog  | ALL  |               |     |         |     | 40978775 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+-----+---------+-----+----------+----------------------------------------------------+

If, instead of using a varible, a run the query replacing it with the string, like:

...
inner join cons c1 on c1.idcons = xx.maxcons
left join conslog clog on clog.idconslog = (select max(clt.idconslog)
                                              from conslog clt
                                              where clt.idcons = c1.idcons
                                              and clt.date_create <= '2018-01-17 23:59:59')
...

explain gives me:

+----+-------------+-------+--------+---------------+---------+---------+------+------+-------------+
| id | select_type | table |  type  | possible_keys |   key   | key_len | ref  | rows |    Extra    |
+----+-------------+-------+--------+---------------+---------+---------+------+------+-------------+
|  1 | PRIMARY     | clog  | eq_ref | PRIMARY       | PRIMARY |       4 | func |    1 | Using where |
+----+-------------+-------+--------+---------------+---------+---------+------+------+-------------+

I've checked other answers here on SO, tried cast ing the variable, convert_tz to UTC, creating it using timestamp(date, time), date_format ... I'm running out of ideas.

date_create is of type:

date_create          TIMESTAMP DEFAULT CURRENT_TIMESTAMP         NOT NULL,

Why does this happen? Why does it have to check so many rows since I'm using idcons which is PK?

Thank you for your help

How about this instead, to simplify the only get the records you need. I think the clog.idcons=c1.idcons might help.

I think it might also help to change the nested query to use clog since that's the one associated with the nested =.

left join conslog clog on clog.idcons=c1.idcons and clog.idconsumolog = (select max(clt.idconslog)
                                              from conslog clt
                                              where clog.idcons = clt.idcons
                                              and clt.date_create <= @dh)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM