主查询在一个大表(数百万行)上,并在0.0012秒内返回约100行。
当我在1行上应用子查询时,它需要0.0022秒的时间
所以我希望第三次查询将在0.0012 + 100 * 0.0022秒(约0.2212秒)内执行
执行时间约为99秒。我认为子查询不会执行100次,而是每行(数百万行)
如何使子查询仅执行100次而不是数百万次?
// 1st查询可以非常快速地返回约100行
SELECT * FROM bigTable
WHERE
m like 'mystring%'
and DATE_CREATION >= '2019-07-17'
and DATE_CREATION <= '2019-07-18'
ORDER BY `DATE_CREATION` DESC
//子查询在1行执行,执行速度非常快
SELECT br FROM u_to_b
where
ag = (SELECT ag FROM log where IP_CREATION = '192.X.X.X' AND DATE_CREATION < '2019-07-17' ORDER BY `DATE_CREATION` DESC LIMIT 1)
//第三次查询,执行时间为99秒
SELECT *,
(SELECT br FROM u_to_b where ag = (SELECT ag FROM log where IP_CREATION = bigTable.IP_CREATION AND DATE_CREATION < bigTable.DATE_CREATION ORDER BY `DATE_CREATION` DESC LIMIT 1)) subqueryResult
FROM bigTable WHERE m like 'mystring%'
and DATE_CREATION >= '2019-07-17' and DATE_CREATION <= '2019-07-18'
ORDER BY `DATE_CREATION` DESC