Portal Home > Knowledgebase > Articles Database > mysql_num_rows() - Bad?


mysql_num_rows() - Bad?




Posted by DediPath, 12-08-2007, 02:06 PM
I just got to thinking, does if you have a table with 19000 rows, and you use mysql_num_rows , isn't that going to be a very slow query and slow down loading times? Whats a better way to do this?

Posted by ThatScriptGuy, 12-08-2007, 03:59 PM
mysql_num_rows only returns the number of rows in a result set. It doesn't go through a table row by row and increment a counter.

Posted by Steve_Arm, 12-08-2007, 04:13 PM
Don't worry about that for 2 reason. 1) Fetching rows from tables with a reader that advances forward one record is one of the fastest operations on a database server. 2) You will never have to fetch all the table records at once.

Posted by DediPath, 12-08-2007, 05:27 PM
Your second reason is false, I will have to do that, that is why I'm asking.

Posted by Steve_Arm, 12-08-2007, 05:44 PM
I actually though about it or else you wouldn't be asking. Do you have to do this for every page load? If so there must be a harder but more efficient way to do it.

Posted by DediPath, 12-08-2007, 06:08 PM
Well I'm coding a forum and in the info center at the bottom will be a total number of threads, and that willl load whenever the index is loaded. My idea I had earlier is have a file/ or a table and when a new thread is created just update that number, I think that would deffinately be faster.

Posted by jstanden, 12-08-2007, 07:04 PM
If you're just selecting a count(*) it's going to use the table index. You could store the total number of threads in a table and increment it relatively (SET n=n+1), but there is probably no need. You don't want to "SELECT *" and mysql_num_rows(), you want to "SELECT COUNT(*)". It will always return one row with a number.

Posted by holmesa, 12-10-2007, 11:01 AM
SELECT COUNT(id) (I would not use asterisk at all) is much better then using mysql_num_rows unless you need to perform is on a complex query.

Posted by TonyB, 12-10-2007, 11:20 AM
Bang on right here count will use the index and will be significantly faster. When doing things like pagination it's much better to use count opposed to a complex query multiple times. There are uses for mysql_num_rows but in this case it's not one of them.



Was this answer helpful?

Add to Favourites Add to Favourites    Print this Article Print this Article

Also Read
Good Reseller Hosts (Views: 739)


Language: