SELECT * FROM HistoryGPSData h WHERE h.UTC BETWEEN (""2015-11-02 12:00:45"") AND (""2015-11-02 13:00:45"") AND SUResourceInfo_Index=1 AND ( (h.PUID=""151000000012132264"" AND h.ResIdx=""0"") OR (h.PUID=""151000000028909480"" AND h.ResIdx=""0"") OR (h.PUID=""151000000045686696"" AND h.ResIdx=""0"") OR (h.PUID=""151000000062463912"" AND h.ResIdx=""0"") OR (h.PUID=""151000000079241128"" AND h.ResIdx=""0"") OR (h.PUID=""151000000096018344"" AND h.ResIdx=""0"") OR (h.PUID=""151000000112795560"" AND h.ResIdx=""0"") OR (h.PUID=""151000000129572776"" AND h.ResIdx=""0"") OR (h.PUID=""151000000146349992"" AND h.ResIdx=""0"") OR (h.PUID=""151000000163127208"" AND h.ResIdx=""0"") ) ORDER BY h.Index LIMIT 50
puid是设备号
utc是时间
SUResourceInfo_Index 、PUID、 ResIdx、 UTC 这四个列有个联合索引
全部数据都在HistoryGPSData一张表里有几亿数据
在查询的时候要是查询1个小时内10个设备的数据 很快
要是查询10个小时内的1个设备的数据就很慢
明明两者的数据量是一样多的 这是为什么查询会变慢 该怎么优化 希望高手来解答
解决方案:10分
贴出 explain select 以供分析。
解决方案:10分
解决方案:10分
SELECT *
FROM HistoryGPSData h
WHERE h.UTC >= “”2015-11-02 12:00:45″” and h.UTC<= “”2015-11-02 13:00:45″”
AND SUResourceInfo_Index=1
AND (
(h.PUID=””151000000012132264″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000028909480″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000045686696″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000062463912″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000079241128″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000096018344″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000112795560″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000129572776″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000146349992″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000163127208″” AND h.ResIdx=””0″”)
)
ORDER BY h.Index LIMIT 50
FROM HistoryGPSData h
WHERE h.UTC >= “”2015-11-02 12:00:45″” and h.UTC<= “”2015-11-02 13:00:45″”
AND SUResourceInfo_Index=1
AND (
(h.PUID=””151000000012132264″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000028909480″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000045686696″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000062463912″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000079241128″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000096018344″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000112795560″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000129572776″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000146349992″” AND h.ResIdx=””0″”)
OR (h.PUID=””151000000163127208″” AND h.ResIdx=””0″”)
)
ORDER BY h.Index LIMIT 50
解决方案:10分
SELECT * FROM HistoryGPSData h WHERE h.UTC BETWEEN (""2015-11-02 12:00:45"") AND (""2015-11-02 13:00:45"") AND SUResourceInfo_Index=1 AND h.PUID IN(""151000000012132264"",""151000000028909480"",""151000000045686696"",""151000000062463912"",""151000000079241128"",""151000000096018344"",""151000000112795560"",""151000000129572776"",""151000000146349992"",""151000000163127208"") AND h.ResIdx=""0"" ORDER BY h.Index LIMIT 50;
试试这个呢
解决方案:10分
语句改成这样:
SELECT * FROM HistoryGPSData h WHERE h.UTC BETWEEN (""2015-11-02 12:00:45"") AND (""2015-11-02 13:00:45"") AND SUResourceInfo_Index=1 AND h.ResIdx=""0"" AND ( (h.PUID=""151000000012132264"" ) OR (h.PUID=""151000000028909480"" ) OR (h.PUID=""151000000045686696"" ) OR (h.PUID=""151000000062463912"" ) OR (h.PUID=""151000000079241128"" ) OR (h.PUID=""151000000096018344"" ) OR (h.PUID=""151000000112795560"" ) OR (h.PUID=""151000000129572776"" ) OR (h.PUID=""151000000146349992"" ) OR (h.PUID=""151000000163127208"" ) ) ORDER BY h.Index LIMIT 50
索引建议改成这样:
SUResourceInfo_Index 、 ResIdx、PUID、 UTC