简体   繁体   中英

MySQL Select Query to Widen Table Optimization

I have a MySQL table with the following configuration:

CREATE TABLE `MONITORING` (  
`REC_ID` int(20) NOT NULL AUTO_INCREMENT,  
`TIME` int(11) NOT NULL,  
`DEVICE_ID` varchar(30) COLLATE utf8_unicode_ci NOT NULL,  
`MON_ID` varchar(10) COLLATE utf8_unicode_ci NOT NULL,  
`TEMPERATURE` float NOT NULL,  
`HUMIDITY` float NOT NULL,  
PRIMARY KEY (`REC_ID`),  
KEY `SelectQueryIndex` (`TIME`,`MON_ID`))  
ENGINE=MyISAM AUTO_INCREMENT=102069 DEFAULT CHARSET=utf8  
COLLATE=utf8_unicode_ci 

Multiple Monitoring Devices send data, always exactly on the minute, but not all monitors are always online. I am using PHP to query the database and format the data to put into a Google Line Chart.

To get the data into the Google Chart I am running a SELECT Query which is giving the results with all of the MON_ID's on a single line.

The Query I am currently using is:

SELECT `TIME`, `H5-C-T`, `P-C-T`, `H5-C-H`, `P-C-H`, `A-T`, `A-H` FROM
    (SELECT `TIME`, `TEMPERATURE` as 'H5-C-T', `HUMIDITY` as 'H5-C-H' FROM `MONITORING` where `MON_ID` = 'H5-C') AS TAB_1,
    (SELECT `TIME` as `TIME2`, `TEMPERATURE` as 'P-C-T', `HUMIDITY` as 'P-C-H' FROM `MONITORING` where `MON_ID` = 'P-C') AS TAB_2,
    (SELECT `TIME` as `TIME3`, `TEMPERATURE` as 'A-T', `HUMIDITY` as 'A-H' FROM `MONITORING` where `MON_ID` = 'Ambient') AS TAB_3
    WHERE TAB_1.TIME = TAB_2.TIME2 AND TAB_1.TIME = TAB_3.TIME3

The results are exactly what I want (Table with TIME and then a Temp and RH column for each of the three monitors), but seems like the query is taking a lot longer than it should to give the results.

Opening the full table, or selecting all rows of just one monitoring device takes about 0.0006 seconds (can't ask for much better than that).

If I do the query with 2 of the monitoring devices it takes about 0.09 seconds (still not bad, but a pretty big percentage increase).

When I put in the third monitoring device the query goes up to about 2.5 seconds (this is okay now, but as more data is collected and more of the devices end up needing to be in charts at one time, it is going to get excessive pretty quick).

I have looked at a lot of posts where people were trying to optimize their queries, but could not find any which were doing the query the same way as me (maybe I am doing it a bad way...). From the other things people have done to improve performance I have tried multiple indexing methods, made sure to check, analyze, and optimize the table in PHP MyAdmin, tried several other querying methods, changed sort field / order of the table, etc. but have not been able to find another way to get the results I need which was any faster.

My table has a total of a little under 100,000 total rows, and it seems like my query speeds are WAY longer than should be expected based off of the many people I saw doing queries on tables with tens of millions of records.

Any recommendations on a way to optimize my query?

Maybe the answer is something like multiple MySQL queries and then somehow merge them together in PHP (I tried to figure out a way to do this, but could not get it to work)?

Flip things inside out; performance will be a lot better:

SELECT  h.`TIME`,
        h.TEMPERATURE AS 'H5-C-T',
        p.TEMPERATURE AS 'P-C-T',
        h.HUMIDITY AS 'H5-C-H',
        p.HUMIDITY AS 'P-C-H', 
        a.TEMPERATURE AS 'A-T', 
        a.HUMIDITY AS 'A-H'
    FROM MONITORING AS h
    JOIN MONITORING AS p ON h.TIME = p.TIME
    JOIN MONITORING AS a ON a.TIME = h.TIME
    WHERE h.`MON_ID` = 'H5-C'
      AND p.`MON_ID` = 'P-C'
      AND a.`MON_ID` = 'Ambient'

And use JOIN...ON syntax.

Is the combination of TIME and REC_ID unique? If so, performance would be even better if you switched to InnoDB, got rid of REC_ID and changed KEY SelectQueryIndex (TIME,MON_ID) into PRIMARY KEY(TIME, MON_ID) .

You should also consider switching to InnoDB.

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