有些事情你可以在不向架构中添加任何内容的情况下尝试:在你的子查询中,你永远不会从 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)