0

私は3つのテーブルtrader、city_state、city_presentを持っています。

trader テーブルに 400 万行あり、クエリに少なくとも 20 秒かかります。city_present と都市テーブルのレコードが少ない。

以下は私の質問です。

   

select t.trader_id, t.name, t.city, t.state from ( SELECT distinct c.city, c.state FROM city_present p,city_state c 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

私は顧客(都市、州、名前、valid_customer)にインデックスを持っています

クエリ時間を短縮する方法を教えてください。

4

2 に答える 2

0

スキーマに何も追加せずに試すことができることがいくつかあります。サブクエリでは、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)
于 2013-05-29T11:27:47.977 に答える