开发者

Multiple count functions in a SELECT query

I have a select query like this

select count(distinct id)*100/totalcount as freq, count (distinct id) from 
<few joins, conditions, gorup by here> .....

Will this result in 2 calculations of count under MySql 5.0? I can calculate the frequency in my appliation as well if this is a problem. I am aware of the solutions presented in Adding percentages to multiple counts in one SQL SELECT Que开发者_StackOverflow中文版ry but I just want to avoid nested queries


select count(distinct id)*100/totalcount as freq, count (distinct id) from 
<few joins, conditions, gorup by here> .....

Yes, it will result in several evaluations.

Each recordset on DISTINCT id will be built separately for each function

Note that if not for DISTINCT, MySQL would use each record only once (though in multiple function calls).

Since COUNT is very cheap, function calls add almost nothing to overall query time.

You can benefit from rewriting your query as this:

SELECT  COUNT(id) * 100 / totalcount AS freq,
        COUNT(id)
FROM    (
        SELECT  DISTINCT id
        FROM    original_query
        ) q

BTW, why do you need both GROUP BY and DISTINCT in one query? Could you please post your original query as it is?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜