gpt4 book ai didi

performance - 加速 postgres 查询(适用于 2 个表)

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

我在 postgresql 中做这样的事情:

select A.first, 
count(B.second) as count,
array_agg(A.second) as second,
array_agg(A.third) as third,
array_agg(B.kids) as kids
from A join B on A.first=B.second
group by A.first;

而且要花很长时间(也因为 table 很大)。将输出限制为 10 行并查看 explain analyze 告诉我有一个嵌套循环,它很大并且占用了大部分时间。

有什么方法可以让我编写这个查询(然后我将在 CREATE TABLE AS 中使用它来创建一个新表)来加快它的速度,同时保留相同的输出,这正是我想要的?

谢谢!

最佳答案

确保将用作外键的列编入索引:

create index b_second on b(second);

如果没有这样的索引,a 的每一行都会导致 b 的表扫描,这将使您的查询爬行。

关于performance - 加速 postgres 查询(适用于 2 个表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14440090/

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