SQL查询长执行(嵌套选择中的计数)

时间:2015-03-24 13:18:37

标签: mysql phpmyadmin

我有这样的疑问:

SELECT DISTINCT type, (SELECT count(*) FROM ads WHERE ad_type = description_table.type)  
as count FROM description_table;

执行大约需要5分钟。这可能是什么问题?

编辑:将表名从'desc'更改为'description_table'以避免复杂化。

1 个答案:

答案 0 :(得分:0)

您需要将表格description_table与广告表相关联。试试这个:

SELECT DISTINCT type, (SELECT count(ads.type) FROM ads join description_table on ads.type = description_table.type)  
as count FROM `description_table`;

而不是计算*,尝试计算某些列,如id或类型

修改

根据您的评论,您可以尝试此查询:

SELECT a.type, count(d.type) as count 
FROM description_table d left join ads a on d.type = a.type
group by d.type;