gpt4 book ai didi

mysql - 优化大数据的 MySQL 交集查询

转载 作者:行者123 更新时间:2023-11-29 18:37:49 36 4
gpt4 key购买 nike

这是我的表结构:

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

最佳答案

这应该运行得更快,但需要构建查询:

select  instagram_profile_id
from instagram_user_followers_mapping AS t
WHERE instagram_user_id = '1142282'
AND EXISTS
(
SELECT *
FROM instagram_user_followers_mapping
WHERE instagram_profile_id = t.instagram_profile_id
AND instagram_user_id = '346115'
)
AND EXISTS
(
SELECT *
FROM instagram_user_followers_mapping
WHERE instagram_profile_id = t.instagram_profile_id
AND instagram_user_id = '663620'
)
AND EXISTS
(
SELECT *
FROM instagram_user_followers_mapping
WHERE instagram_profile_id = t.instagram_profile_id
AND instagram_user_id = '985530'
);

此公式避免了文件排序,并避免收集给定 profile_id 的所有 user_id(反之亦然)。

innodb_buffer_pool_size 是否大于索引大小?

关于mysql - 优化大数据的 MySQL 交集查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45141639/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com