当我在mysql中查询它5次时,其中一次会很慢。 0.2s
四次,8.0s
一次。为什么?
我知道子查询会导致它变慢,但我不知道mysql做了什么。为什么它不总是缓慢,但有时会变慢。
SELECT C.base_id AS task_id, C.id AS strategy_queue_id, D.cluster,
A.type AS class_type, C.kind AS online_type,
C.task_type, C.traffic, mis_key,
CASE
WHEN C.kind = 1 THEN ‘traffic’
WHEN C.kind = 2 THEN ‘strategy’
END AS online_type_desc,
CASE
WHEN A.type = 1 THEN ‘all’
WHEN A.type = 2 THEN ‘one’
ELSE 'null'
END AS class_type_desc,
FROM_UNIXTIME(A.startTime, '%m-%d %H:%i:%S') AS start_time,
FROM_UNIXTIME(A.stopTime, '%m-%d %H:%i:%S') AS end_time,
A.startTime, A.stopTime
FROM sample_A A
JOIN sample_B B ON A.record_id = B.record_id
JOIN sample_C C ON B.queue_id = C.id
JOIN sample_D D ON C.base_id = D.task_id
WHERE D.task_id IN (
SELECT C.base_id FROM sample_C C
JOIN sample_B B ON B.queue_id = C.id
JOIN sample_A A ON A.record_id = B.record_id
WHERE A.startTime BETWEEN $startTime AND $stopTime
OR ( A.stopTime BETWEEN $startTime AND $stopTime )
OR (A.startTime BETWEEN $startTime AND $stopTime AND A.stopTime IS NULL)
)
AND C.task_type = 'SMALL_TRAFFIC'
ORDER BY D.task_id DESC, C.id, A.record_id
表格索引:
index of sample_A record_id
index of sample_B record_id + queue_id(two column as one index)
index of sample_C id, base_id
index of sample_D task_id
$startTime
和$stopTime
是可变的。