慢:使用子查询获取查询的结果计数

时间:2014-09-18 19:53:00

标签: mysql sql

我想知道的是我的查询结果中有多少条记录,但是MySQL一直在说我失去了联系。查询本身大约运行一秒钟。

SELECT COUNT(*) FROM
(SELECT my208.eid AS contact, name AS the_status, cid208.lastmod AS status_date, boo208.boo_medium
FROM the_emails.my208
LEFT JOIN the_emails.cid208 ON cid208.eid = my208.eid
LEFT JOIN the_emails.boo208 ON boo208.eid = my208.eid
LEFT JOIN the_config.classes ON boo208.class_id = classes.id) foo

为什么这需要这么长时间并有更好的方法?

1 个答案:

答案 0 :(得分:0)

在内部查询中包含count(*)。请参阅下面的修改后的查询

试一试

SELECT my208.eid AS contact, 
name AS the_status, 
cid208.lastmod AS status_date, 
boo208.boo_medium,
COUNT(*) as total_records
FROM the_emails.my208
LEFT JOIN the_emails.cid208 ON cid208.eid = my208.eid
LEFT JOIN the_emails.boo208 ON boo208.eid = my208.eid
LEFT JOIN the_config.classes ON boo208.class_id = classes.id