Winamp & Shoutcast Forums

Winamp & Shoutcast Forums (http://forums.winamp.com/index.php)
-   General Discussions (http://forums.winamp.com/forumdisplay.php?f=1)
-   -   PHP/SQL Question. (http://forums.winamp.com/showthread.php?t=284950)

ryan 14th January 2008 06:40

PHP/SQL Question.
 
Is there any advantage to using "SELECT count('foo') FROM bar" over mysql_num_rows()?

Kaboon 14th January 2008 06:53

Quote:

PHP.net
It is faster to run second query "select count(...) from ... ", than adding SQL_CALC_FOUND_ROWS to your first query, and then using select FOUND_ROWS() + mysql_num_rows().
Quote:

PHP.net
The reason it's just as slow is that to count that way as it is to fetch, minus the data transfer.

Even when executing a limit query, when you ask it to fetch the number of total rows, it must scan the whole table every time to calculate the count.

Quote:

PHP.net
The fastest way to get the number of rows in a table is doing this:

$total = mysql_result(mysql_query("SELECT COUNT(id) FROM yourtable"),0);

As long as there are no NULL ids (shouldnt be), it will return the correct rows extremely fast. If you already used yourtable though, it is faster to use mysql_num_rows() on the result of it.


ryan 14th January 2008 06:55

Thanks.

Kaboon 14th January 2008 06:56

You're welcome! :D

ryan 14th January 2008 07:54

http://pastebin.com/m4b262ac5

A lot nicer than my old query() function :D


All times are GMT. The time now is 09:06.

Copyright © 1999 - 2010 Nullsoft. All Rights Reserved.