2013-03-27 53 views
3

我有一個大約有3mil行的表。雖然我用這個查詢來選擇: SELECT WITH WITH在多列上的速度很慢

SELECT order_code, store_debit, total_price 
FROM orders 
WHERE 4624603 IN (id, pid) AND `status` = -6; 

或此查詢(使用OR

SELECT order_code, store_debit, total_price 
FROM orders 
WHERE (id = 4624603 OR pid = 4624603) AND `status` = -6; 

和一個帶> 17secs。 但是當我把它分成2個查詢:

SELECT order_code, store_debit, total_price 
FROM orders 
WHERE id = 4624603 AND `status` = -6; 

SELECT order_code, store_debit, total_price 
FROM orders 
WHERE pid = 4624603 AND `status` = -6; 

返回像瞬間結果。 我怎麼能優化第一個查詢,使其運行速度與其他2.

謝謝大家!

UPDATE: 這些表

cop_type payment_type, cod_type, cash_transfer, pid, status, created Normal BTREE 
all_type payment_type, cash_transfer, pid, status, created Normal BTREE 
theodoi user_id, pid, payment_type, cash_transfer, status, report_atm, pay_status, created Normal BTREE 
item_lib item_id, status, pay_status, payment_type, created Normal BTREE 
search_phone phone Normal BTREE 
search_order_code order_code Normal BTREE 
search_order_email email Normal BTREE 
order_work cod_id, status, district, payment_type, pay_status, cash_transfer, cod_type, free_ship Normal BTREE 
select_hot province, status, type, created, payment_type, pay_status, item_id Normal BTREE 
coupon_after_buy id, pid, status, free_ship Normal BTREE 
search_ofice office, created, status, ship_status Normal BTREE 
search_item item_id, office, created, status, ship_status Normal BTREE 
idx_ward_id ward_id Normal BTREE 
idx_street street_id Normal BTREE 
idx_group_code group_code, pid Normal BTREE 
idx_paytime payment_time Normal BTREE 
search_all pid, status, created Normal BTREE 
idx_country_type country_type Normal BTREE 
idx_book_time book_time Normal BTREE 
+0

索引'id'和'pid' – 2013-03-27 04:27:03

+0

對不起,我用** OR **和** IN **更新了這個問題,他們都很慢 – CasperPas 2013-03-27 04:28:48

+0

@HankyPankyㇱ:我更新了我的問題。我應該只索引id和pid在一個新的索引鍵嗎?我已經在** coupon_after_buy **索引 – CasperPas 2013-03-27 04:32:38

回答

2

的指標如何使用的最後兩個的工會嗎?在/或可能迫使表掃描。

+0

中有這些字段謝謝,那是行得通的:D我完全忘記了** UNION **。 – CasperPas 2013-03-27 06:51:27