MySQL 在多列上选择 WITH "OR"时速度慢

标签 mysql optimization

我有一个大约有 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;

那一个花了 >17 秒。 但是当我把它分成两个查询时:

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

最佳答案

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

关于MySQL 在多列上选择 WITH "OR"时速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15651888/

相关文章:

python - while循环,永远运行或倒计时

python - 如何使用 numpy 有效地计算一个系列?

mysql - 一页上有很多查询,执行时间太长...似乎无法优化它们

mysql - 可串行化冲突以及为什么 2 次读取不冲突?

mysql - 每个用户喜欢的帖子的单独表格 - 实用与否?

mysql - Sequelize 原始查询返回 TextRows 数组

c - 评估 sizeof 导致性能改进 C

php - Codeigniter MySQL 连接复杂

php - 使用 pdo 时数据未插入数据库

c# - 比反复试验更有效地优化多个变量的算法