「in」の代わりに「exists」を使うことを(昨日)学んだばかりです。
BAD
select * from table where nameid in (
select nameid from othertable where otherdesc = 'SomeDesc' )
GOOD
select * from table t where exists (
select nameid from othertable o where t.nameid = o.nameid and otherdesc = 'SomeDesc' )
そして、これについていくつか質問があります:
1)私が理解した説明は、「これが優れている理由は、可能な結果の膨大なリストを作成する代わりに、一致する値のみが返されるためです」 . つまり、最初のサブクエリは 900 件の結果を返しますが、2 番目のサブクエリは 1 件 ( yes または no ) しか返さないということですか?
2) 過去に RDBMS の苦情がありました:「最初の 1000 行しか取得できない可能性があります」、この 2 番目のアプローチはその問題を解決しますか?
3) 2 番目のサブクエリのエイリアスのスコープは何ですか?... エイリアスは括弧内にのみ存在しますか?
例えば
select * from table t where exists (
select nameid from othertable o where t.nameid = o.nameid and otherdesc = 'SomeDesc' )
AND
select nameid from othertable o where t.nameid = o.nameid and otherdesc = 'SomeOtherDesc' )
つまり、同じエイリアス ( o テーブル othertable ) を使用すると、2 番目の "exist" で最初の exists に問題が発生しますか? それとも完全に独立していますか?
これは Oracle のみに関連するものですか、それともほとんどの RDBMS で有効ですか?
どうもありがとう