MySQL num rows

Started by
7 comments, last by hplus0603 11 years, 9 months ago
I created my own mysql client connection following their protocol
http://forge.mysql.com/wiki/MySQL_Internals_ClientServer_Protocol

I can query just fine now and its working great but I am trying to find a way to implement a mysql_num_rows and cannot find a way todo it without first getting all the rows. What I would like is to get the number first and I dont want to have to store several thousand rows before I know how much I will be dealing with.

I have read and reread the protocol but cant find anything could someone help point me in a direction or tell me that the only way would be to get the rows first? Thanks
Advertisement
Just a stab in the dark... would the COUNT function help you?
Generally the database does not know how may rows will be returned unless you do a count(*) or actually fetch all of the rows. In many cases, the DB does not need to build the whole result set before it can start outputting rows to the client, and so it will output rows as you fetch them in more of a streaming fashion. This avoids doing any unnecessary work if you cancel the query before fetching all available results, and also avoids having to keep results in a temporary area until you fetch them.

Certain queries which involve sorting/grouping etc may require the whole result set to be built before it can output anything, in which case fetching the first batch of rows will be slow, and fetching subsequent batches will be faster as all of the heavy lifting has already been done.
Thank you WavyVirus. That makes sense and I guess you wouldnt want it to get the result set first so I can complain lol ill just have to store everything and count it thats fine.
Oh and to taby The Count function would either a have to perform 2 queries or b modify the query to have a count in it then strip it from the result set both could be done but wouldnt be pretty.

Oh and to taby The Count function would either a have to perform 2 queries or b modify the query to have a count in it then strip it from the result set both could be done but wouldnt be pretty.


Yep, that's the point of count -- "find a way to implement a mysql_num_rows [snip] without first getting all the rows".
I think ill just store the result set in memory rather than do 2 queries or try to modify the queries and result fields on the fly. Thank You
That seems sensible (assuming you really need to know the count at all). You cannot guarantee that the count will be the same if you issue two queries (unless you use something like "serializable" isolation level, but I think that has major locking implications in mysql). Fudging the query to somehow return a count would be really ugly as well.
My opinion: The fact that you want a count means that you're not doing proper stream processing of your data.
Perhaps a more efficient way is to find a way to write the application layer without count, instead.

For select count() to return the number of rows that a later select will return, your transaction isolation level needs to be "repeatable-read" or better. However, any isolation level below "serializable" is not fully transaction compliant. If you're using MySQL for an online transaction database, you should be using "serializable." Also, when benchmarking against other databases, you should probably be using this isolation level.
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement