mysql查询花费了很长时间达到9s到12s

时间:2018-12-18 10:57:25

标签: mysqli

SELECT *
FROM (`orders`)
JOIN `pricelist` ON `orders`.`pricelist_id` = `pricelist`.`id`
JOIN `users` ON `users`.`id` = `orders`.`user_id`
WHERE `pricelist`.`vendor` > 0
AND `pricelist`.`services` > 0
AND `orders`.`price` != 0
and (orders.status='Approved' || orders.status='Duplicate') AND (orders.allow_setting=0 || orders.allow_setting=2)

LIMIT 450;

  

我不确定如何编制索引,

此查询需要0.457秒 但是当我运行

SELECT `orders`.`service_name_order`, `orders`.`price`, `orders`.`allow_setting`, `users`.`status` as ustatus, `orders`.`free`, `orders`.`return`, `orders`.`scrapt_counter`, `orders`.`return2`, `pricelist`.`minorder`, `orders`.`service_code`, `pricelist`.`vendor_list_id2`, `pricelist`.`vendor_list_id`, `pricelist`.`increment`, `orders`.`error_status`, `orders`.`services_order`, `orders`.`url`, `pricelist`.`requireaction`, `pricelist`.`checker`, `orders`.`requireviews`, `orders`.`youtubeviews`, `orders`.`id`, `orders`.`start_api_date`
FROM (`orders`)
JOIN `pricelist` ON `orders`.`pricelist_id` = `pricelist`.`id`
JOIN `users` ON `users`.`id` = `orders`.`user_id`
WHERE `pricelist`.`vendor` > 0
AND `pricelist`.`services` > 0
AND `orders`.`price` != 0
AND (orders.free <> 1 OR users.status = 'active') and (orders.status='Approved' || orders.status='Duplicate') AND (orders.allow_setting=0 || orders.allow_setting=2)
ORDER BY `orders`.`created_update` Asc
LIMIT 450;
  

如果我将单个字段标记为索引或进行复合索引。它们在单个索引或复合索引上有什么区别吗?

花了12秒。

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  users   index   PRIMARY,status,id_status    status  1   NULL    477068  Using index; Using temporary; Using filesort
1   SIMPLE  orders  ref free,pricelist_id,user_id,price,free_status_allow_setting_price_created_update,free_pricelist_id_user_id_price_created_update   user_id 4   njxkfnygnp.users.id 2   Using where
1   SIMPLE  pricelist   eq_ref  PRIMARY,vendor,services,id_vendor_services  PRIMARY 4   njxkfnygnp.orders.pricelist_id  1   Using where

0 个答案:

没有答案