简体   繁体   中英

Mysql query with sub-selects taking too long..

Every 5 minutes my historical table is updated (by another process) with a new record (TAG_NAME remains the same but TIME and VALUE are updated). I am using the following query to return the LATEST record while also grouping by TAG_NAME.

SELECT 
TAG_NAME,max(TIME) as max_time, 
(select value from historical where TAG_NAME=hist.TAG_NAME order by time desc limit 1) as max_value 
FROM historical hist 
WHERE TIME IS NOT NULL AND GROUP BY TAG_NAME;

This query is taking as long as 1 minute on a table with 100k rows. Can anyone help me optimize this query?

Much Thanks!

Try this:

SELECT 
TAG_NAME, max(`TIME`) as max_time, max(`value`) as max_value
FROM historical
WHERE `TIME` IS NOT NULL 
GROUP BY TAG_NAME
;

You could get the combined maximum, so if two times are the same you get the biggest value.

select TAG_NAME, max(concat(time,value)) as time_value
from historical 
group by TAG_NAME

If necessary you can split time and value in mySQL, but you can also split it in the app.

http://sqlfiddle.com/#!9/6d210/1

SELECT 
h.*
FROM historical h
LEFT JOIN historical h1
ON h.TAG_NAME = h1.TAG_NAME
  AND h.time<h1.time
WHERE h1.time IS NULL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM