简体   繁体   English

子查询中的变量未在时间戳字段中使用索引

[英]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 我得到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: explain给我:

+----+-------------+-------+--------+---------------+---------+---------+------+------+-------------+
| 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. 我已经在SO上检查了其他答案,尝试变量convert_tz转换为UTC,使用timestamp(date,time), date_format创建它...我已经筋疲力尽了。

date_create is of type: date_create的类型为:

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? 自从我使用的是PK的idcons以来,为什么必须检查这么多行?

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. 我认为clog.idcons = c1.idcons可能有所帮助。

I think it might also help to change the nested query to use clog since that's the one associated with the nested =. 我认为将嵌套查询更改为使用clog也可能有所帮助,因为那是与嵌套=相关联的查询。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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