3

我有一个大约有 300 万行的表。当我使用这个查询来选择时:

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 个一样快。

谢谢你们!

更新:这些是表的索引

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
4

1 回答 1

2

使用最后两个的 UNION 怎么样?in/or 可能会强制进行表扫描。

于 2013-03-27T04:34:12.780 回答