Oracle存在查询无法正常工作-DB链接错误?
-
24-10-2019 - |
题
我对此查询的结果感到困惑:
select count(*) from my_tab mt
where mt.stat = '2473'
and mt.name= 'Tom'
and exists (select * from company_users@colink.world cu,
personnel_records@colink.world pr
where cu.user_id = pr.user_id
and mt.name = pr.name
and mt.stat = cu.stat
)
返回:1
company_users@colink.world中有0个记录,stat ='2473',那么为什么它的存在呢?
如果我这样更改查询,它将返回0:
select count(*) from my_tab mt
where mt.stat = '2473'
and mt.name= 'Tom'
and exists (select * from company_users@colink.world cu,
personnel_records@colink.world pr
where cu.user_id = pr.user_id
and mt.name = pr.name
and cu.stat = '2473'
)
更新 好的,这真的很奇怪。只是为了看看会发生什么,我从其他数据库(由DB链接引用的数据库)执行了查询,并给出了不同的(正确)结果。
select count(*) from my_tab@mylink.world mt
where mt.stat = '2473'
and mt.name= 'Tom'
and exists (select * from company_users cu,
personnel_records pr
where cu.user_id = pr.user_id
and mt.name = pr.name
and mt.stat = cu.stat
)
返回0(如预期)。
解决方案
看看第一个查询的解释计划。我怀疑有一个错误,查询计划可能会显示如何进行无效的重写。
其他提示
您的问题中的第二个查询有些不同 - 它根本不看cu.Stat,因此Cu.Stat ='2473'的事实没有解决。如果执行执行,您会得到什么结果
select count(*)
from company_users@colink.world cu,
personnel_records@colink.world pr,
my_tab mt
where mt.stat = '2473' and
mt.name = 'Tom' and
pr.name = mt.name and
cu.user_id = pr.user_id and
cu.stat = mt.stat
我认为这与您的第一个查询相同,不使用不存在,应该提供正确的结果。
分享并享受。
不隶属于 StackOverflow