gpt4 book ai didi

postgresql - 优化索引以查询具有多个连接的大表

转载 作者:行者123 更新时间:2023-11-29 11:35:46 27 4
gpt4 key购买 nike

我们有一个包含大约 2500 万条记录的 images 表,当我根据来自多个连接的值查询该表时,规划器的估计与行计数的实际结果大不相同。我们还有其他查询,这些查询在没有所有连接的情况下大致相同,而且速度要快得多。我想知道可以采取哪些步骤来调试和优化查询。此外,最好是有一个索引覆盖联接和 where 子句中包含的所有列,还是多个索引一个用于每个联接列,然后另一个索引包含 where 中的所有字段 子句?

查询:

EXPLAIN ANALYZE
SELECT "images".* FROM "images"
INNER JOIN "locations" ON "locations"."id" = "images"."location_id"
INNER JOIN "users" ON "images"."creator_id" = "users"."id"
INNER JOIN "user_groups" ON "users"."id" = "user_groups"."user_id"
WHERE "images"."deleted_at" IS NULL
AND "user_groups"."group_id" = 7
AND "images"."creator_type" = 'User'
AND "images"."status" = 2
AND "locations"."active" = TRUE
ORDER BY date_uploaded DESC
LIMIT 50
OFFSET 0;

解释:

Limit  (cost=25670.61..25670.74 rows=50 width=585) (actual time=1556.250..1556.278 rows=50 loops=1)
-> Sort (cost=25670.61..25674.90 rows=1714 width=585) (actual time=1556.250..1556.264 rows=50 loops=1)
Sort Key: images.date_uploaded
Sort Method: top-N heapsort Memory: 75kB
-> Nested Loop (cost=1.28..25613.68 rows=1714 width=585) (actual time=0.097..1445.777 rows=160886 loops=1)
-> Nested Loop (cost=0.85..13724.04 rows=1753 width=585) (actual time=0.069..976.326 rows=161036 loops=1)
-> Nested Loop (cost=0.29..214.87 rows=22 width=8) (actual time=0.023..0.786 rows=22 loops=1)
-> Seq Scan on user_groups (cost=0.00..95.83 rows=22 width=4) (actual time=0.008..0.570 rows=22 loops=1)
Filter: (group_id = 7)
Rows Removed by Filter: 5319
-> Index Only Scan using users_pkey on users (cost=0.29..5.40 rows=1 width=4) (actual time=0.006..0.008 rows=1 loops=22)
Index Cond: (id = user_groups.user_id)
Heap Fetches: 18
-> Index Scan using creator_date_uploaded_Where_pub_not_del on images (cost=0.56..612.08 rows=197 width=585) (actual time=0.062..40.992 rows=7320 loops=22)
Index Cond: ((creator_id = users.id) AND ((creator_type)::text = 'User'::text) AND (status = 2))
-> Index Scan using locations_pkey on locations (cost=0.43..6.77 rows=1 width=4) (actual time=0.002..0.002 rows=1 loops=161036)
Index Cond: (id = images.location_id)
Filter: active
Rows Removed by Filter: 0
Planning time: 1.694 ms
Execution time: 1556.352 ms

我们在 RDS db.m4.large 实例上运行 Postgres 9.4。

最佳答案

至于查询本身,您唯一能做的就是跳过 users 表。从 EXPLAIN 中,您可以看到它只执行 Index Only Scan 而实际上没有接触表。因此,从技术上讲,您的查询可能如下所示:

SELECT images.* FROM images
INNER JOIN locations ON locations.id = images.location_id
INNER JOIN user_groups ON images.creator_id = user_groups.user_id
WHERE images.deleted_at IS NULL
AND user_groups.group_id = 7
AND images.creator_type = 'User'
AND images.status = 2
AND locations.active = TRUE
ORDER BY date_uploaded DESC
OFFSET 0 LIMIT 50

剩下的就是索引了。 locations 似乎只有很少的数据,所以在这里优化对你没有任何好处。另一方面,user_groups 可以从索引ON (user_id) WHERE group_id = 7ON (group_id, user_id) 中受益。这应该删除对表格内容的一些额外过滤。

-- Option 1
CREATE INDEX ix_usergroups_userid_groupid7
ON user_groups (user_id)
WHERE group_id = 7;

-- Option 2
CREATE INDEX ix_usergroups_groupid_userid
ON user_groups (group_id, user_id);

当然,这里最重要的是images。目前,planer 会在 creator_date_uploaded_Where_pub_not_del 上进行索引扫描,我怀疑这不完全符合要求。在这里,根据您的使用模式,您会想到多个选项 - 从一个搜索参数相当常见的选项开始:

-- Option 1
CREATE INDEX ix_images_creatorid_typeuser_status2_notdel
ON images (creator_id)
WHERE creator_type = 'User' AND status = 2 AND deleted_at IS NULL;

完全动态参数:

-- Option 2
CREATE INDEX ix_images_status_creatortype_creatorid_notdel
ON images (status, creator_type, creator_id)
WHERE deleted_at IS NULL;

第一个索引更可取,因为它较小(值被过滤掉而不是索引)。

总而言之,除非您受到内存(或其他因素)的限制,否则我会在 user_groupsimages 上添加索引。指标的正确选择必须通过经验来确认,因为通常有多种选择,具体情况取决于数据的统计分布。

关于postgresql - 优化索引以查询具有多个连接的大表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51288661/

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