Performance postgresql查询计划奇怪的行为
我必须选择一个表中的行,该表相当小,小于10K行,在另一个表中没有对应的行。表中有500K行,基于列。表中有一个索引btree。 如果我使用以下查询:Performance postgresql查询计划奇怪的行为,performance,postgresql,indexing,sql-execution-plan,Performance,Postgresql,Indexing,Sql Execution Plan,我必须选择一个表中的行,该表相当小,小于10K行,在另一个表中没有对应的行。表中有500K行,基于列。表中有一个索引btree。 如果我使用以下查询: select a.column1, a.column2, a.column3, a.column_X, b.column_X from table_A a left outer join table_B b on a.column_X = b.column_X where a.column_X <> 0
select a.column1,
a.column2,
a.column3,
a.column_X,
b.column_X
from table_A a
left outer join table_B b on a.column_X = b.column_X
where a.column_X <> 0
and b.column_X is null
查询结果行大约在600毫秒内执行。
另一方面,如果我尝试不同的查询:
select column1,
column2,
column3,
column_X
from table_A
where column_X not in (
select column_X
from table_B
where column_X is not null
)
and column_X <> 0
检索相同的168行大约需要8分钟。列X的类型为bigint,在第二个查询中,强制转换似乎没有什么区别,索引从未使用过。
有什么想法吗?不在子选项的优化效果比其他选项差得多。由于语义不同,PostgreSQL无法使用反连接。如果可以,不要使用这种模式。请使用“不存在”或“外部联接”。请共享解释分析结果-请查看web以进行共享。注意:不需要子查询中的where列_X不为null。@wildplasser如果列_X可为null,则确实需要该列。没有它,如果列实际包含NULL值,NOT IN表达式和任何IN表达式将导致NULL,这是ANSI SQL标准所要求的。这就是为什么PostgreSQL不会将该查询转换为反连接。。。。f、 前。列_X不在1中,NULL相当于列_x1,列_xnull相当于列_x1,NULL将始终为NULL。IMHO列_X不在1中,NULL相当于不列_X=1或列_X=NULL,这将变为非列_X=1或NULL ergo:即使子查询返回NULL,它们不能与任何值null或非null进行比较