Re: Fast count of recordset in php...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 7 Aug 2005, at 05:04, TalkativeDoggy wrote:

be coz this way is slow and costs more overload.

$sql = "SELECT COUNT(IDVara) cn FROM tbvara WHERE Varunamn LIKE '$checkLev%'";
   $querys = mysql_query($sql);

   //Count products in db
   //
   $dbArray = mysql_fetch_row($querys);
   $nrOfProducts = $dbArray[0];

According to the docs, MySQL has a particular optimisation that means that it should be:

 $sql = "SELECT COUNT(*) FROM tbvara WHERE Varunamn LIKE '$checkLev%'";

Also if you want the full count value when you've done a query that uses a LIMIT clause, instead of doing a separate full count query, you can get it by using the SQL_CALC_FOUND_ROWS keyword, e.g.

$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM tbvara WHERE Varunamn LIKE '$checkLev%' LIMIT 10";

Of course count(*) could only ever return up to 10 in this query - you can find out how many it would have found by then asking:

$sql = "SELECT FOUND_ROWS()";

Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@xxxxxxxxxxxxxxxxxx | http://www.synchromedia.co.uk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux