Jump to content
  • Advertisement
Sign in to follow this  
jeff8j

MySQL num rows

This topic is 2342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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". Edited by taby

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!