我有两个表eventsevent_params

第一个表使用这些列存储事件

events | CREATE TABLE `events` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `project` varchar(24) NOT NULL,
  `event` varchar(24) NOT NULL,
  `date` int(10) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `project` (`project`,`event`)
) ENGINE=InnoDB AUTO_INCREMENT=2915335 DEFAULT CHARSET=latin1


第二个使用这些列存储每个事件的参数

event_params | CREATE TABLE `event_params` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `event_id` int(10) unsigned NOT NULL,
  `name` varchar(24) NOT NULL,
  `value` varchar(524) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`name`),
  KEY `event_id` (`event_id`),
  KEY `value` (`value`),
) ENGINE=InnoDB AUTO_INCREMENT=20789391 DEFAULT CHARSET=latin1


现在我想获取在指定参数上具有各种值的事件计数

我为campaign参数编写了此查询,但这太慢了(响应需要15秒)

SELECT
    event_params.value as campaign,
    count(*) as count
FROM `events`
    left join event_params on event_params.event_id = events.id
                          and event_params.name = 'campaign'
WHERE events.project = 'foo'
GROUP by event_params.value


这是EXPLAIN查询结果:

+----+-------------+--------------+------------+------+---------------------+----------+---------+------------------+------+----------+----------------------------------------------+
| id | select_type | table        | partitions | type | possible_keys       | key      | key_len | ref              | rows | filtered | Extra                                        |
+----+-------------+--------------+------------+------+---------------------+----------+---------+------------------+------+----------+----------------------------------------------+
|  1 | SIMPLE      | events       | NULL       | ref  | project             | project  | 26      | const            |    1 |   100.00 | Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | event_params | NULL       | ref  | name,event_id,value | event_id | 4       | events.events.id |    4 |   100.00 | Using where                                  |
+----+-------------+--------------+------------+------+---------------------+----------+---------+------------------+------+----------+----------------------------------------------+


我可以加快查询速度吗?

最佳答案

您可以尝试在event_params表上添加以下索引,这可以加快连接速度:

CREATE INDEX idx1 ON event_params (event_id, name, value);


由于COUNT操作涉及对每个记录进行计数,因此可能无法对聚合步骤进行太多优化。

07-26 08:50