[TYPO3-dev] Tip: use SQL function COUNT(*)
ries van Twisk
typo3 at rvt.dds.nl
Fri Jul 18 21:39:36 CEST 2008
On Jul 18, 2008, at 1:56 PM, Ernesto Baschny [cron IT] wrote:
>> <snip>......
>> 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
If you look at the PHP's code that directly calls libmysql.
Then libmysql get's the number of records from it's internal dataset
here:
http://leithal.cool-tools.co.uk/sourcedoc/mysql509/html/mysql_8h-source.html#l00278
see : st_mysql_res
From this function : http://leithal.cool-tools.co.uk/sourcedoc/mysql509/html/mysql_8h.html#a90
mysql_query(..) calls mysql_real_query(...) http://leithal.cool-tools.co.uk/sourcedoc/mysql509/html/mysql_8h.html#a113
then read_query_result is called to fetch all data into memory from
the mysql server
see here : http://leithal.cool-tools.co.uk/sourcedoc/mysql509/html/mysql_8h.html#a115
So that means it's not a cursor or anything but the whole dataset is
wetched into lib_mysql and mysql_num_rows
simply reads a value from it's internal array and doesn't call the
server, nor mysql_fetch_row calls the server
to get data from the server. It's all memmory based in libmysql.
Ries
More information about the TYPO3-dev
mailing list