gpt4 book ai didi

sql - 与简单或条件相比,在条件中使用效率极低

转载 作者:行者123 更新时间:2023-12-04 19:26:17 26 4
gpt4 key购买 nike

我的查询结构如下:

select
distinct items,
invoice
from
table_name
where
location = 'warehouse'
and invoice in
(select
t.invoice
from
list_name t
where
t.invoice_date > to_date (('2015-04-18'),'yyyy-mm-dd')
)

子查询应该给我一个发票列表,然后我将其返回到主查询中以查找发票中的所有项目。发票数量不是静态的,这就是我想嵌套子查询的原因。

此查询的问题是它可能需要数小时才能处理,而不是:

select
distinct items,
invoice
from
table_name
where
location = 'warehouse'
and t.invoice = 'invoice1'
or t.invoice = 'invoice2'

这大约需要 0.3 秒。我在这里尝试将我的条件限制为两个值:

select
distinct items,
invoice
from
table_name
where
location = 'warehouse'
and invoice in ('invoice1','invoice2')

此查询仍需要大约 4 分钟才能运行。任何想法为什么这需要这么长时间?我以前使用过 in 条件,它从来没有这么慢,但我不明白为什么这个特定的实现不起作用。

编辑* 这是解释计划。在解决@hines 指出的问题后,第二和第三 block 代码是相同的。

Description                               object                  cost

select statement, goal=all_rows 422542
hash unique 422542
view index$_join$_001 422541
hash join
index range scan PKHIR_IX19 31382
index fast full scan PKHIR_IX16 351172

最佳答案

您使用的标准在第二个和第三个代码块之间是不同的。如果您正在尝试复制 IN 代码,那么您可能希望将“或”语句括起来。目前,即使位置不等于“仓库”,“或”语句也会返回任何包含 t.invoice = 'invoice2' 的行。

select
distinct items,
invoice
from
table_name
where
location = 'warehouse'
and (invoice = 'invoice1'
or invoice = 'invoice2')

关于sql - 与简单或条件相比,在条件中使用效率极低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29780022/

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