我目前有一个大规模的查询(使用PHP和MySQL),我不知道如何优化它以减少18-25秒的大量负载。我看过以前的答案,发现它们都写了类似的大量代码,导致类似的脚本执行时间,所以我想看看是否有人会有任何其他想法而不是已经有的构成:
TLDR; 我需要为每个分析ID选择最大列,这需要大量负载,所以我需要以某种方式减少它
$qry = "SELECT DISTINCT ip,country_code,country,
(SELECT MAX(region) FROM analytics RegOne WHERE bid='$id' AND RegOne.ip= IPOne.ip) AS region,
(SELECT MAX(city) FROM analytics CitOne WHERE bid='$id' AND CitOne.ip= IPOne.ip) AS city,
(SELECT MAX(os) FROM analytics OSOne WHERE bid='$id' AND OSOne.ip= IPOne.ip) AS os,
(SELECT MIN(browser) FROM analytics BroOne WHERE bid='$id' AND BroOne.ip= IPOne.ip) AS browser,
(SELECT MIN(resolution) FROM analytics ResOne WHERE bid='$id' AND ResOne.ip= IPOne.ip) AS resolution,
(SELECT MAX(timestamp) FROM analytics DateOne WHERE bid='$id' AND DateOne.ip= IPOne.ip) AS LatestDate
FROM analytics AS IPOne
WHERE bid='$id'
ORDER BY LatestDate DESC
LIMIT 15 OFFSET $offset";
这是从一个庞大的表中获取数据,所以我不认为它需要0秒,但任何时候减少都会很好而不是~21秒。
答案 0 :(得分:1)
为什么不
SELECT ip, country_code, country,
MAX(region) as region,
MAX...
MAX(timestamp) as LatestDate
FROM analytics
WHERE bid = '$id'
GROUP BY ip, country_code, country
ORDER BY LatestDate DESC
LIMIT 15 OFFSET $offset";
你应该有一些以bid
开头的索引。这可能会更好:
INDEX(bid, ip, country_code, country)
答案 1 :(得分:0)
您应该在(bid, ip)
上有一个复合KEY。
如果您在(bid)
上有KEY,则添加(或替换为(bid, ip)