【问题标题】:Counting unique rows performance计算唯一行性能
【发布时间】:2018-07-19 00:34:33
【问题描述】:

我有一张包含用户统计信息的表格。生成请求 URL 的统计信息。 在这个带有 GET 参数 (event_id) 的 URL 下是 PHP 脚本。以透明 [1x1] GIF 响应。 事件是孩子,插入和插入是从活动等到帐户的孩子。 基于statistic_type 的两种统计类型在哪里。 一个帐号有多个空格(space_id)。

这是创建表:

CREATE TABLE `statistic` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`statistic_type` TINYINT(3) UNSIGNED NOT NULL,
`account_id` TINYINT(3) UNSIGNED NOT NULL,
`advertiser_id` SMALLINT(5) UNSIGNED NOT NULL,
`campaign_id` SMALLINT(5) UNSIGNED NOT NULL,
`insertion_id` MEDIUMINT(8) UNSIGNED NOT NULL,
`event_id` INT(10) UNSIGNED NOT NULL,
`space_id` SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
`date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`stat_platform_id` TINYINT(3) UNSIGNED NULL DEFAULT NULL,
`stat_browser_id` SMALLINT(5) UNSIGNED NULL DEFAULT NULL,
`stat_device_type_id` TINYINT(3) UNSIGNED NULL DEFAULT NULL,
`major_version` TINYINT(4) UNSIGNED NULL DEFAULT NULL,
`uid` CHAR(19) NOT NULL,
`referrer` VARCHAR(255) NULL DEFAULT NULL,
`useragent` VARCHAR(255) NULL DEFAULT NULL,
`ipv4` INT(11) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX `IDX_uid` (`uid`),
INDEX `Indeks 2` (`statistic_type`, `account_id`, `advertiser_id`, `campaign_id`, `insertion_id`, `event_id`, `space_id`, `date`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPRESSED
AUTO_INCREMENT=427891347;

现在我需要获取由statistic_typeevent_idspace 分组的campaign_id 的所有唯一(基于uid 字段)行数。

所有表现在都有 428067039 行。它每天仍在增长约 1500000 行。 结果最多应有 100 行。 最大过滤和字母聚合(具有一个 campaign_id 的行)行应最多为 5000000。

现在我使用这个查询:

SELECT 
`sub`.`account_id`, 
`sub`.`advertiser_id`, 
`sub`.`campaign_id`, 
`sub`.`insertion_id`, 
`sub`.`event_id`, 
`sub`.`space_id`,
SUM(`sub`.actions) as 'actions',
count(1) as 'unique_actions'
FROM (
SELECT 
    `statistic`.`account_id`, 
    `statistic`.`advertiser_id`, 
    `statistic`.`campaign_id`, 
    `statistic`.`insertion_id`, 
    `statistic`.`event_id`, 
    `statistic`.`space_id`,
    count(1) as 'actions'
FROM `statistic`
WHERE 
    (`statistic`.`statistic_type`=1) AND 
    (`statistic`.`account_id`=3) AND 
    (`statistic`.`advertiser_id`=679) AND
    (`statistic`.`campaign_id`=4475) AND
    (`statistic`.`insertion_id`=26841)
GROUP BY 
    `statistic`.`statistic_type`, 
    `statistic`.`account_id`,
    `statistic`.`advertiser_id`,
    `statistic`.`campaign_id`,
    `statistic`.`insertion_id`,
    `statistic`.`event_id`,
    `statistic`.`space_id`,
    `statistic`.`uid`
) sub
GROUP BY 
`sub`.`statistic_type`, 
`sub`.`account_id`,
`sub`.`advertiser_id`,
`sub`.`campaign_id`,
`sub`.`insertion_id`,
`sub`.`event_id`,
`sub`.`space_id`;

当我有 400000 个按 insertion_id 行过滤时,它的计数超过 7-10 分钟。

解释:

+----+-------------+------------+------+---------------+----------+---------+------+-------+----------------------------------------------+
| id | select_type | table      | type | possible_keys | key      | key_len | ref  | rows  | Extra                                        |
+----+-------------+------------+------+---------------+----------+---------+------+-------+----------------------------------------------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL     | NULL    | NULL | 157521 | Using temporary; Using filesort              |
|  2 | DERIVED     | statistic  | ref  | Indeks 2      | Indeks 2 | 9       |      | 627090 | Using where; Using temporary; Using filesort |
+----+-------------+------------+------+---------------+----------+---------+------+-------+----------------------------------------------+
2 rows in set (2.61 sec)

也许我需要更改索引,也许添加新的?

也许我需要在用户请求 URL 时添加新的 col 并插入一些值?

【问题讨论】:

  • 请阅读此内容。 meta.stackoverflow.com/a/271056 特别注意查询性能部分。请edit您的问题提供有关您的问题的更多信息。这类查询性能问题通常可以通过索引解决,但我们需要了解您的查询和表的详细信息以帮助您。 不要尝试将信息放在评论中,而是edit您的问题。

标签: mysql database-design innodb database-performance query-performance


【解决方案1】:

通常我建议不要使用长索引,但在这种情况下,我建议使用 8 列索引,其中包含 sub's GROUP BY 的所有列,完全按照该顺序。您拥有的那个以date 结尾;我不知道它是否对其他东西有用。我的结尾是uid

我希望这会加快子查询,但对外部查询没有影响。

一个额外的想法...由于 5 列是恒定的,您可以不将它们带到外部查询中吗?这将减少大约 157521 行的大部分临时表,从而可能加快外部部分的速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-03
    • 2016-07-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    相关资源
    最近更新 更多