简体   繁体   中英

Can the query performance be further improved (MySQL)

I have 2 tables in MySQL db. One table is for data logging and has the following columns:

(let this be called datalogger table)

id - primary key

site id, equipment id, equipment number, equipment parameter, value, date logged

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).

The other table (let this be called loggerparameterdetails table) has the following columns:

equipment id equipment parameter - these 2 are the composite primary key

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.

So I created the following indexes for the 2 tables:

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)

parameter name - loggerparameterdetails table (since this column is used in the join)

For a year date range, I profile the query and see that the Sending Data process shows around 3.5-4 seconds. 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'

'1';'SIMPLE';'logtbl';'range';'loggertbl_combined_idx';'loggertbl_combined_idx';'69';\\N;'528604';'Using where; Using join buffer'

TL; 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.

You might try rearranging the order of the compound index in order to confirm which is most efficient

Execute this query in 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.

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