简体   繁体   English

查询性能能否进一步提高(MySQL)

[英]Can the query performance be further improved (MySQL)

I have 2 tables in MySQL db. 我在MySQL数据库中有2个表。 One table is for data logging and has the following columns: 一个表用于数据记录,并包含以下列:

(let this be called datalogger table) (将此称为数据记录器表)

id - primary key id-主键

site id, equipment id, equipment number, equipment parameter, value, date logged 站点ID,设备ID,设备编号,设备参数,值,记录日期

For selecting any records from this table the columns site id, equipment id, equipment number, equipment parameter are used in the where clause along with specifying the date range (date logged). 为了从该表中选择任何记录,在where子句中使用了站点ID,设备ID,设备编号,设备参数列以及指定日期范围(记录的日期)。

The other table (let this be called loggerparameterdetails table) has the following columns: 另一个表(此表称为loggerparameterdetails表)具有以下列:

equipment id equipment parameter - these 2 are the composite primary key 设备ID设备参数-这2是复合主键

parameter description and parameter unit are the other columns in this table 参数说明和参数单位是此表中的其他列

The datalogger table has a large number of records (year wise record count will be around a million records). 数据记录器表具有大量记录(逐年记录数将为一百万条记录)。 For the application reporting purpose I join the datalogger table with the loggerparameterdetails table on the parameter name and in the where clause specify the site id, equipment number, parameter name and the date logged range. 出于应用程序报告的目的,我将数据记录器表与参数名称上的loggerparameterdetails表结合在一起,并在where子句中指定站点ID,设备编号,参数名称和记录日期范围。

So I created the following indexes for the 2 tables: 因此,我为2个表创建了以下索引:

composite index including site id, equipment id, equipment number, parameter name and date logged - datalogger table (these columns come in the where clause so created an index for these) 综合索引,包括站点ID,设备ID,设备编号,参数名称和记录的日期-数据记录器表(这些列位于where子句中,因此为它们创建了索引)

parameter name - loggerparameterdetails table (since this column is used in the join) 参数名称-loggerparameterdetails表(因为在联接中使用此列)

For a year date range, I profile the query and see that the Sending Data process shows around 3.5-4 seconds. 对于年份日期范围,我对查询进行了分析,并看到“发送数据”过程显示在3.5-4秒左右。 The query looks like this: 查询如下所示:

select logtbl.date_logged, logparam.cmd_desc, logtbl.value, logparam.cmd_unit 
from datalogger logtbl join loggerparameterdetails logparam
on logtbl.cmd_name=logparam.cmd_name where logtbl.site_id=1
and logtbl.equipment_number=1 and logtbl.cmd_name='aaaabbab'
and logtbl.date_logged between '2016-02-02 00:00:00' and '2017-02-06 00:00:00'

Can this timing be further improved upon? 这个时间可以进一步改善吗?

Update: 更新:

The explain plan for the query is as below: 该查询的解释计划如下:

'id';'select_type';'table';'type';'possible_keys';'key';'key_len';'ref';'rows';'Extra' '1';'SIMPLE';'logparam';'ref';'mibobjName_idx';'mibobjName_idx';'52';'const';'1';'Using where' 'id';'select_type';'table';'type';'possible_keys';'key';'key_len';'ref';'rows';'Extra''1';'SIMPLE';'logparam' ;'ref';'mibobjName_idx';'mibobjName_idx';'52';'const';'1';'在哪里使用'

'1';'SIMPLE';'logtbl';'range';'loggertbl_combined_idx';'loggertbl_combined_idx';'69';\\N;'528604';'Using where; '1';'SIMPLE';'logtbl';'range';'loggertbl_combined_idx';'loggertbl_combined_idx';'69'; \\ N;'528604';'在何处使用; Using join buffer' 使用连接缓冲区”

TL; TL; DR; DR; so maybe I missed something useful in all that verbiage.., 所以也许我错过了所有这些有用的东西。

Anyway, a compound index on... 无论如何,...的综合指数

site_id
equipment_number
cmd_name
date_logged

...would seem most desirable - as well as an index on cmd_name on the other table. ...似乎是最可取的-以及另一个表上cmd_name的索引。

You might try rearranging the order of the compound index in order to confirm which is most efficient 您可以尝试重新排列复合索引的顺序,以确认哪种方法最有效

Execute this query in MySQL Workbench. 在MySQL Workbench中执行此查询。

EXPLAIN select logtbl.date_logged, logparam.cmd_desc, logtbl.value, logparam.cmd_unit 
from datalogger logtbl join loggerparameterdetails logparam
on logtbl.cmd_name=logparam.cmd_name where logtbl.site_id=1
and logtbl.equipment_number=1 and logtbl.cmd_name='aaaabbab'
and logtbl.date_logged between '2016-02-02 00:00:00' and '2017-02-06 00:00:00'

This will help you tune your indexes. 这将帮助您调整索引。 If there are table scans, probably your complex index includes columns in wrong sequence. 如果进行表扫描,则可能您的复杂索引包含错误顺序的列。 Regardless, EXPLAIN will tell you if further improvement is possible. 无论如何,EXPLAIN都会告诉您是否有可能进一步改进。

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

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