【发布时间】:2017-07-17 10:20:27
【问题描述】:
这是我的表结构:
CREATE TABLE `instagram_user_followers_mapping` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`instagram_user_id` varchar(20) NOT NULL,
`instagram_profile_id` varchar(20) NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `instagram_unique_user_follower_mapping` (`instagram_user_id`,`instagram_profile_id`),
KEY `instagram_user_followers_mapping_created_at_index` (`created_at`),
KEY `instagram_user_followers_mapping_updated_at_index` (`updated_at`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPRESSED
我在这个表中有超过 1 亿行。当我尝试在两个或多个“instagram_user_id”之间获取共同关注者时,它适用于表中少于 20,000 行的个人资料。但是对于超过 200 万行的配置文件,它的运行速度非常慢。我想让这些数据实时显示以进行分析和报告。最终用户可能会选择配置文件的任意组合,因此在这里创建汇总表并不是一个很好的选择。
我用来获取交集的查询是:
select instagram_profile_id, count(*) as myCount
from instagram_user_followers_mapping
where instagram_user_id IN ('1142282','346115','663620','985530')
group by instagram_profile_id HAVING myCount >= 4
【问题讨论】:
-
什么版本的 MySQL?
-
基本上,
HAVING...之前的所有事情都需要完全完成——从 4 个 id 中的每一个中获取多行、排序、分组和计数。只有这样才能应用HAVING。 -
mysql 版本 5.7.18
标签: mysql optimization reporting bigdata