douzhan1963 2015-07-26 04:50
浏览 508
已采纳

在MySQL中选择多列的MAX()

I currently have a massive query (using PHP and MySQL) and I'm not sure how to optimize it in order to reduce its massive load of 18-25 seconds. I've looked at previous answers and have found that they all sort of write a similarly large amount of code resulting in a similar script execution time and so I'd like to see if anyone would have any alternative ideas as opposed to the ones already posed:

TLDR; I need to select the maximum column for each analytics ID and this takes on a massive load and so i need to reduce it somehow

$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";

This is taking data from a massive table and so i don't expect it to take 0 seconds however any time reduction would be fine rather than ~21 seconds.

  • 写回答

2条回答 默认 最新

  • douchang6770 2015-07-26 05:17
    关注

    You should have a composite KEY on (bid, ip). If you have KEY on (bid) then add (or replace it with) (bid, ip)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败