0

我有这样的声明:

select qulified_name
from table
inner join references_table on references_table.id = table.ref_id
where references_table.type = 'x' and table.value in (... +110 000 ids)

这非常慢。(网络应用程序崩溃并且没有得到结果。我在 rom-rb 的帮助下创建了我的声明,这使用了续集。但这是我查看声明时得到的声明。

如果我像这样重写语句,与第一个版本相比,性能真的很好:

select qulified_name
from table
inner join (select unnest(array['#{references_id.join("','")}']) id ) as tmp on tmp.id = businesspartner_references.value
inner join references_table on references_table.id = table.ref_id
where references_table.type = 'x'

这样我可以在 3 秒内得到结果。

有人可以向我解释为什么会这样吗?我不明白。。

4

1 回答 1

3

当您使用 IN 子句时,尤其是具有大量值时,数据库别无选择,只能迭代地将每个元组值与 IN 子句中的每个值进行比较,这将是低效的。

相反,当您使用子查询将其转换为派生表时,它现在变成了一个面向集合的连接操作。

数据库非常擅长评估面向集合的操作,并且可以为您的数据找到最佳的连接算法。

于 2017-03-07T14:00:19.747 回答