[TYPO3-dev] Tip: use SQL function COUNT(*)

Ernesto Baschny [cron IT] ernst at cron-it.de
Fri Jul 18 20:56:35 CEST 2008


ries van Twisk wrote: on 18.07.2008 19:09:
> 
> On Jul 18, 2008, at 10:57 AM, Ernesto Baschny [cron IT] wrote:
> 
>> ries van Twisk wrote: on 18.07.2008 16:35:
>>>
>>> On Jul 18, 2008, at 9:14 AM, Martin Kutschker wrote:
>>> <snip>........
>>>
>>
>> I don't think that mysql_num_rows will be processed "client side". The
>> data sets won't even be retrieved if you just do a select followed by
>> mysql_num_rows. It is probably more expensive than count(*), but I
>> cannot imagine that it is *that* much faster.
>>
>> I recall that mysql has a shortcut in the special case "COUNT(*) FROM
>> <table>" (without a "WHERE"), because it will consult the MYD headers
>> for the amount of rows and not even load indexes to do the dirty work.
>> So this is of course the easiest to do.
>>
>> But as in TYPO3 almost on all queries you will have to include
>> enableFields, I figure that MySQL will have to touch indexes anyway so
>> it shouldn't matter that much.
>>
>> Cheers,
>> Ernesto
>> _______________________________________________
> 
> Hey Ernesto,
> 
>  From reading the docs about mysql_num_rows : "Retrieves the number of 
> rows from a result set."
> 
> That means that the result set was already transfered to PHP's API  and 
> there you lose performance because
> of the data treansfer. You would properly really notice it with one 
> query and a small table.
> But one a buzy database with large record sets it can consume a lot of 
> time.

A result set is not "data" but just a pointer to some structure on the 
server which can be be used to retrieved data with the "fetch" commands. 
The data is not pre-fetched by mysql or the client.

Cheers,
Ernesto




More information about the TYPO3-dev mailing list