【发布时间】:2015-05-29 10:03:30
【问题描述】:
我有一个相当简单的查询,当我在 phpMyAdmin 中测试它时运行正常:
SELECT
c.customers_id,
c.customers_cid,
c.customers_gender,
c.customers_firstname,
c.customers_lastname,
c.customers_email_address,
c.customers_telephone,
c.customers_date_added,
ab.entry_company,
ab.entry_street_address,
ab.entry_postcode,
ab.entry_city,
COUNT(o.customers_id) AS orders_number,
SUM(ot.value) AS totalvalue,
mb.bonus_points
FROM
orders AS o,
orders_total AS ot,
customers AS c,
address_book AS ab,
module_bonus AS mb
WHERE
c.customers_id = o.customers_id
AND c.customers_default_address_id = ab.address_book_id
AND c.customers_id = mb.customers_id
AND o.orders_id = ot.orders_id
AND ot.class = 'ot_subtotal'
** AND c.customers_gender = 'm' AND c.customers_lastname LIKE 'Famlex'
GROUP BY o.customers_id
标有**的行会根据进行查询的应用程序的过滤设置而变化。
现在,当我在 phpMyAdmin 中对此进行测试时,查询需要几秒钟才能运行(这很好,因为有数千个条目,而且据我所知,使用 COUNT 和 SUM 索引时没有帮助) 并且结果很完美,但是当我在 PHP 中运行完全相同的查询时(在运行前回显),MySQL 线程将内核加载到 100% 并且在我杀死它之前不会停止。
如果我去掉多余的东西来计算 COUNT 和 SUM,则查询完成,但结果对我来说毫无用处。
解释:
1 SIMPLE mb ALL NULL NULL NULL NULL 48713 Using temporary; Using filesort
1 SIMPLE ot ALL idx_orders_total_orders_id NULL NULL NULL 811725 Using where
1 SIMPLE o eq_ref PRIMARY PRIMARY 4 db.ot.orders_id 1 Using where
1 SIMPLE c eq_ref PRIMARY PRIMARY 4 db.o.customers_id 1 Using where
1 SIMPLE ab eq_ref PRIMARY PRIMARY 4 db.c.customers_default_address_id 1
应用索引和使用连接后的解释:
1 SIMPLE c ref PRIMARY,search_str_idx search_str_idx 98 const 1 Using where; Using temporary; Using filesort
1 SIMPLE mb ALL NULL NULL NULL NULL 48713 Using where
1 SIMPLE ab eq_ref PRIMARY PRIMARY 4 db.c.customers_default_address_id 1
1 SIMPLE ot ref idx_orders_total_orders_id,class class 98 const 157004 Using where
1 SIMPLE o eq_ref PRIMARY PRIMARY 4 db.ot.orders_id 1 Using where
【问题讨论】:
-
explain select...对上述查询说了什么,更新问题并在其中添加这些详细信息。 -
添加了解释,虽然我不知道这意味着什么。
-
看起来我们同时进行了相同的编辑,@t.niese。
-
将在 50 分钟后返回......
标签: php mysql phpmyadmin cpu-usage freeze