views:

53

answers:

1

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 http://stackoverflow.com/questions/1306112/ but I just want to avoid nested queries

+1  A: 
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?

Quassnoi