在webhost上SQL select查询速度太慢,在localhost上运行正常

时间:2013-12-16 01:49:09

标签: sql performance select memory

SELECT c.customers_lastname, 
cg.customers_group_name, 
dctc.coupons_id AS coupId, 
dcto.coupons_id AS coupIdUsed, 
dc.coupons_date_start AS coupStart, 
count(DISTINCT o.orders_id) AS totalorders, 
sum(op.products_quantity * op.final_price) AS ordersum 
from
customers c LEFT JOIN customers_groups cg ON cg.customers_group_id = c.customers_group_id 
LEFT JOIN (discount_coupons_to_customers dctc 
    LEFT JOIN discount_coupons dc ON dc.coupons_id = dctc.coupons_id 
    LEFT JOIN discount_coupons_to_orders dcto ON dcto.coupons_id = dctc.coupons_id 
) ON c.customers_id = dctc.customers_id, orders_products op, orders o 
WHERE c.customers_id = o.customers_id 
AND c.customers_promotions = '0' 
AND o.orders_id = op.orders_id 
GROUP BY c.customers_id
ORDER BY ordersum DESC LIMIT 0, 10 

以上查询返回所有在我们的网上商店购买任何东西的客户(以及一些额外的数据),按总订单金额排序。它在localhost上运行正常(几秒钟),但在远程服务器上需要一分钟。更糟糕的是,可以通过表单修改查询,以在GROUP BY子句中包含额外的位,如:

HAVING (sum(op.products_quantity * op.final_price) >= 1000
AND/OR count(DISTINCT o.orders_id) > 2)

这并没有完全加快速度。目前大约有5000个客户和3000个订单。我添加了一个时间限制WHERE命令不超过一年但事后没有加快速度。

我比较了我的本地服务器和在线服务器。

  • localhost linux内核是3.2,在线2.6,
  • localhost php 5.4.4,在线5.3.26,
  • localhost mysql 5.5,在线5.1,
  • localhost php内存限制128M,在线126M。

有明显的瓶颈吗?我发了一封电子邮件给我的虚拟主机,但没有收到回复。如果我需要交换主机我会,但想知道要注意什么。欢呼声,

编辑 使用说明:(不知道如何格式化,不知道它意味着什么)返回

`+----+-------------+-------+--------+-----------------------------+--------------+---------+---------------------+-------+----------------------------------------------+
| id | select_type | table | type   | possible_keys               | key          | key_len | ref                 | rows  | Extra                                        |
+----+-------------+-------+--------+-----------------------------+--------------+---------+---------------------+-------+----------------------------------------------+
|  1 | SIMPLE      | c     | ALL    | PRIMARY                     | NULL         | NULL    | NULL                |  5541 | Using where; Using temporary; Using filesort |
|  1 | SIMPLE      | cp    | eq_ref | PRIMARY                     | PRIMARY      | 4       | rpc.c.customers_id  |     1 |                                              |
|  1 | SIMPLE      | dctc  | eq_ref | customers_id,customers_id_2 | customers_id | 4       | rpc.c.customers_id  |     1 |                                              |
|  1 | SIMPLE      | dcto  | ref    | PRIMARY                     | PRIMARY      | 34      | rpc.dctc.coupons_id |     0 | Using index                                  |
|  1 | SIMPLE      | dc    | ALL    | PRIMARY                     | NULL         | NULL    | NULL                |     1 |                                              |
|  1 | SIMPLE      | cg    | ALL    | PRIMARY                     | NULL         | NULL    | NULL                |     5 | Using where; Using join buffer               |
|  1 | SIMPLE      | o     | ALL    | PRIMARY                     | NULL         | NULL    | NULL                |  5010 | Using where; Using join buffer               |
|  1 | SIMPLE      | op    | ALL    | NULL                        | NULL         | NULL    | NULL                | 10675 | Using where; Using join buffer               |
+----+-------------+-------+--------+-----------------------------+--------------+---------+---------------------+-------+----------------------------------------------+`

0 个答案:

没有答案