Home >Backend Development >PHP Tutorial >mysql查询时间太长,请诸位大神看看

mysql查询时间太长,请诸位大神看看

WBOY
WBOYOriginal
2016-06-13 12:22:421245browse

mysql查询时间太长,请各位大神看看
SELECT a.`sku`,a.`stock_id`,a.`currency`,a.`account`,a.`charge`,a.`sale_country`,b.`stock_description`,c.`img_name`,d.`price`,d.`purchase_price`,d.`price`,d.`order_id`
 FROM `Sku_Comparison` a
 LEFT JOIN `Item_Codes` b ON a.`stock_id`=b.`stock_id` 
 LEFT JOIN (SELECT * FROM `Product_Img` GROUP BY `item_code`) as c ON a.`stock_id`=c.`item_code`
  LEFT JOIN  (SELECT * FROM (SELECT * FROM `Sale_Order_de` order by `id` desc ) t GROUP BY t.`sku`) d ON d.`sku`=a.`sku`
  ORDER BY a.`id` DESC 

查询时间要10秒左右,一个表数据最多的才几万条而以

字段类型除了数字类的,就是varchar
------解决思路----------------------
你应该优化你的 sql 语句,减少子查询(你用了3个临时表),而且索引很少用到,连接查询中 ON a.`stock_id`=c.`item_code`和ON d.`sku`=a.`sku`这个几个字段尽量添加索引

你试试这个:我也不知道返回的是不是你想要的数据

<br />SELECT a.`sku`,a.`stock_id`,a.`currency`,a.`account`,a.`charge`,a.`sale_country`,b.`stock_description`,c.`img_name`,d.`price`,d.`purchase_price`,d.`price`,d.`order_id`<br /> FROM `Sku_Comparison` a<br /> LEFT JOIN `Item_Codes` b ON a.`stock_id`=b.`stock_id` <br /> LEFT JOIN `Product_Img` as c ON a.`stock_id`=c.`item_code`<br />  LEFT JOIN  `Sale_Order_de` as d ON d.`sku`=a.`sku`<br />  ORDER BY a.`id` DESC <br />

------解决思路----------------------
没有表结构吗?
1.索引要加对。
2.尽量减少临时表。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn