If I have a query that may return a lot of records and I want to know how many.
How much the COUNT(*) will cost in memory ?
Is it always possible to use COUNT ?
Thanks in advance,
Regards, David
If I have a query that may return a lot of records and I want to know how many.
How much the COUNT(*) will cost in memory ?
Is it always possible to use COUNT ?
Thanks in advance,
Regards, David
Using MySQL withCOUNT(*)
takes into account column indexes it will be the best result as long as there are no WHERE
clauses etc
I think I read somewhere the MyISAM engine actually stores row count so it doesn't count the rows every time (instead it basis it on the primary key) -> I think I read that on another stack overflow question so try searching for it.
If you are grouping result sets do not use COUNT(*)
but rather SQL_CALC_FOUND_ROWS
after SELECT:
SELECT SQL_CALC_FOUND_ROWS FROM table LIMIT....
Maybe read this blog for more information on COUNT(*) vs COUNT(col)
as well:
I've no experience of prestashop but maybe it helps, try testing the queries yourself and monitor the behaviour, speed etc.
EDIT: Here we are, this supports some of my statements above: is SELECT COUNT(*) expensive?
EDIT: When including a WHERE expression ensure your indexes are correct, consider adding a multi index (See how to create an index). Before this you can check your queries by using the EXPLAIN{query goes here} to see which index is used (EXPLAIN explained). If more than one, it can be optimised by moving the smallest index to the first position in the index - can't remember how to do this but Google will (How MyQSL uses indexes).
I'm trying not to go off topic here but it may also be worth considering adding triggers to create a summary table as is discussed here: Speeding up row counting in MySQL