MySQL在SELECT WITH" OR"在多列上

时间:2013-03-27 04:25:07

标签: mysql optimization

我有一张大约有3万行的表。虽然我使用此查询来选择:

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;

它会立即返回 之类的结果。 我怎样才能优化第一个查询,使其运行速度与其他查询一样快。

谢谢大家!

更新: 这些是表的索引

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

1 个答案:

答案 0 :(得分:2)

如何使用最后两个的UNION? in /或可能正在强制进行表扫描。