スキーマに何も追加せずに試すことができることがいくつかあります。サブクエリでは、city_present から何も選択しないため、IN
/に変えることができます。EXISTS
select t.trader_id, t.name, t.city, t.state from
(
SELECT c.city, c.state
FROM city_state c
WHERE EXISTS (
select null
from city_present p
where
p.name = 'TEST_TEST'
AND c.city = p.city
AND c.state = p.state)
)
cs, trader t
where
AND t.city = cs.city
AND t.state = cs.state
AND t.name = 'john test'
AND t.is_valid= 1
次に、同じことが cs にも当てはまります。したがって、次のように書き換えることができます。
select t.trader_id, t.name, t.city, t.state from
trader t
where
exists (
SELECT null
FROM city_state c
WHERE EXISTS (
select null
from city_present p
where
p.name = 'TEST_TEST'
AND c.city = p.city
AND c.state = p.state)
AND t.city = c.city
AND t.state = c.state
)
AND t.name = 'john test'
AND t.is_valid= 1
サブクエリをフラット化することもできます。
select t.trader_id, t.name, t.city, t.state from
trader t
where
exists (
SELECT null
FROM city_present p,city_state c
WHERE p.name = 'TEST_TEST'
AND c.city = p.city
AND c.state = p.state
AND t.city = c.city
AND t.state = c.state
)
AND t.name = 'john test'
AND t.is_valid= 1
ここから、インデックス作成について調査する必要があります。
- trader.name および/または trader.id
- (city_state.city, city_state.state) および (city_present.city, city_present.state)