sql如下:
SELECT DISTINCT
a.equipment_code,
a.digit_name AS dn,
a.digit_identity AS di,
a.value_upperlimit AS vu,
a.value_lowerlimit AS vl
FROM
eventlog b
LEFT JOIN t_equipment_digit a ON a.is_trend_monitored = 1
AND a.equipment_code = ‘zxP111-15’
AND b.channelname = a.digit_identity
WHERE
b.time BETWEEN ‘2020-05-10 14:05:00’
AND ‘2020-06-10 15:05:00’
and a.digit_identity IS NOT NULL
LIMIT 20
发现个很奇怪的问题:
原始sql的时间范围是这样:b.time BETWEEN ‘2020-05-10 14:05:00’ AND ‘2020-06-10 15:05:00’ ;
改成这样(时间范围缩短为一个小时,以前是一个月零一个小时) :b.time BETWEEN ‘2020-06-10 14:05:00’ AND ‘2020-06-10 15:05:00’ ,这样b表过滤后的数据量变小。 造成的结果是:mysql执行起来变慢(比以前查一个月的慢很多,要5秒),tidb查询起来变快(毫秒级)。
explain analyze SELECT DISTINCT
a.equipment_code,
a.digit_name AS dn,
a.digit_identity AS di,
a.value_upperlimit AS vu,
a.value_lowerlimit AS vl
FROM
eventlog b
LEFT JOIN t_equipment_digit a ON a.is_trend_monitored = 1
AND a.equipment_code = ‘zxP111-15’
AND b.channelname = a.digit_identity
WHERE
b.time BETWEEN ‘2020-05-10 14:05:00’
AND ‘2020-06-10 15:05:00’
and a.digit_identity IS NOT NULL
LIMIT 20;