[英]Which indexes do I need to add to speed up this MYSQL JOIN/GROUP BY/HAVING query?
The following query takes about 30 seconds to execute when current_vacature_response contains 88k records, and daily_vacature_response contains 10k records. 当current_vacature_response包含88k条记录,而daily_vacature_response包含10k条记录时,以下查询将花费大约30秒的时间执行。 Using EXPLAIN I've concluded that no indexes where used from the
current_vacature_response
table. 使用EXPLAIN,我得出的结论是,
current_vacature_response
表中未使用任何索引。 I've added some basic indexes, but none of them seem to be used. 我添加了一些基本索引,但是似乎没有一个被使用。 What kind of index do I need to set to speed up this query?
我需要设置哪种索引来加快查询速度?
Query: 查询:
SELECT c.`stats_date` as `stats_date`
FROM `current_vacature_response` c
LEFT JOIN `daily_vacature_response` d ON (c.`stats_date` = d.`stats_date` )
GROUP BY c.`stats_date`, d.`stats_date`
HAVING max(d.`last_stats_datetime`) IS NULL
OR MAX(d.`last_stats_datetime`) < MAX(c.`created_datetime`);
current_vacature_response table definition: current_vacature_response表定义:
CREATE TABLE `current_vacature_response` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`created_datetime` datetime NOT NULL,
`site_id` tinyint(1) unsigned NOT NULL,
`stats_date` date NOT NULL,
`type` enum('typ1', 'type2') NOT NULL,
`vacature` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
KEY `current_vacature_created_datetime` (`created_datetime`),
KEY `current_vacature_response_vacature` (`vacature`),
KEY `current_vacature_response_type` (`type`),
KEY `current_vacature_stats_date` (`stats_date`)
) ENGINE=MyISAM AUTO_INCREMENT=88210 DEFAULT CHARSET=utf8;
daily_vacature_response table definition: daily_vacature_response表定义:
CREATE TABLE `daily_vacature_response` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`contact` int(10) unsigned NOT NULL DEFAULT '0',
`site_id` tinyint(1) unsigned NOT NULL,
`spotlight_result` int(10) unsigned NOT NULL DEFAULT '0',
`stats_date` date NOT NULL,
`last_stats_datetime` datetime NOT NULL,
`vacature` int(10) unsigned NOT NULL,
`created_datetime` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `daily_vacature_response_key` (`site_id`,`vacature`,`stats_date`),
KEY `daily_vacature_response_last_stats_datetime` (`last_stats_datetime`),
KEY `daily_vacature_response_stats_date` (`stats_date`)
) ENGINE=MyISAM AUTO_INCREMENT=9802 DEFAULT CHARSET=utf8;
Explain output: 解释输出:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: c
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 88209
Extra: Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: d
type: ref
possible_keys: daily_vacature_response_stats_date
key: daily_vacature_response_stats_date
key_len: 3
ref: reporting_development.c.stats_date
rows: 99
Extra:
Try an index on daily_vacature_response(stats_date, last_stats_datetime)
. 尝试对
daily_vacature_response(stats_date, last_stats_datetime)
进行索引。
I'm suspicious that it'll make a huge difference, but that's the most likely candidate. 我怀疑这会带来巨大的改变,但这是最有可能的候选人。
Also, try rewriting the query a little bit (might not work in MySQL, but worth trying): 另外,尝试稍微重写一下查询(在MySQL中可能不起作用,但是值得尝试):
GROUP BY c.`stats_date`, c.`created_datetime`, d.`stats_date`
HAVING max(d.`last_stats_datetime`) IS NULL
OR max(d.`last_stats_datetime`) < c.`created_datetime`;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.