ネットワークデバイスを監視しています。1 つのデバイスが複数のスイッチに表示される場合があります。
別のポートにも表示される場合に備えて、アップリンク/ポートチャネルにあるデバイスを除外したいと考えています。他のすべてのデバイスが選択されます。
テーブルが次のようになっているとします。
HOST, SWITCH, PORT
HostA, Switch1, 01
HostB, Switch1, 02
HostA, Switch2, Po - Po is portchannel / uplink
HostC, Switch2, Po - Po is portchannel / uplink
望ましい出力:
HostA, Switch1, 01
HostB, Switch1, 02
HostC, Swtich2, Po - is only on an uplink / so that is OK
エントリ HostA、Switch2、Po は、別のポートにも表示されるため、除外する必要があります。
ここでの問題は、効率的なクエリを作成する方法です。
SQL 用語では、HOST が 2 回出現する行を除くすべての行を選択します。次に、PORT が「Po」ではない行のみが必要です。
現在のクエリはサブクエリのために遅い!? サブクエリがデカルト積を作成していると思います-そうですか?
SELECT * FROM devices t1
WHERE NOT ((Port = 'Po') AND
((Select count(*) from table t2 where t1.host=t2.host AND NOT Port='Po') > 0))
繰り返しになりますが、より高速な SQL クエリを作成する方法は??