简体   繁体   English

MySQL选择查询以扩大表优化

[英]MySQL Select Query to Widen Table Optimization

I have a MySQL table with the following configuration: 我有一个具有以下配置的MySQL表:

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. 我正在使用PHP查询数据库并格式化数据以放入Google折线图。

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. 为了将数据输入到Google图表中,我正在运行SELECT查询,该查询将所有MON_ID的结果都显示在一行上。

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. 结果正好是我想要的(三个表分别带有TIME的表和一个Temp和RH列的表),但是看起来查询花费的时间比给出结果要长得多。

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). 打开整个表或仅选择一个监视设备的所有行大约需要0.0006秒(不能要求比这更好的多)。

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). 如果我使用2个监视设备进行查询,则大约需要0.09秒(仍然不错,但是百分比增加了很多)。

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). 当我放入第三个监视设备时,查询时间大约为2.5秒(现在可以了,但是随着收集的数据越来越多,并且越来越多的设备最终需要一次显示在图表中,它将会变得越来越多余)非常快)。

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. 人们为了提高性能所做的其他事情,我尝试了多种索引方法,确保检查,分析和优化了PHP MyAdmin中的表,尝试了其他几种查询方法,更改了表的排序字段/顺序等,但是还没有找到另一种方法来获得我需要的结果,这更快。

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. 我的表的总行数不到100,000,而我的查询速度似乎比预期的要长得多,这是基于我看到的对具有数千万条记录的表进行查询的许多人的结果。

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)? 也许答案是类似多个MySQL查询,然后以某种方式将它们合并到PHP中(我试图找出一种方法来执行此操作,但无法使其正常工作)?

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. 并使用JOIN...ON语法。

Is the combination of TIME and REC_ID unique? TIME和REC_ID的组合是否唯一? 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) . 如果是这样,那么如果您切换到InnoDB,摆脱了REC_ID并将KEY SelectQueryIndex (TIME,MON_ID)更改为PRIMARY KEY(TIME, MON_ID) ,性能将更好。

You should also consider switching to InnoDB. 您还应该考虑切换到InnoDB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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