Jump to content

  • Log In with Google      Sign In   
  • Create Account

MySQL num rows


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 jeff8j   Members   -  Reputation: 779

Like
0Likes
Like

Posted 18 July 2012 - 07:07 AM

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

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


Sponsor:

#2 taby   Members   -  Reputation: 336

Like
0Likes
Like

Posted 18 July 2012 - 09:20 AM

Just a stab in the dark... would the COUNT function help you?

#3 WavyVirus   Members   -  Reputation: 735

Like
1Likes
Like

Posted 18 July 2012 - 11:10 AM

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.

#4 jeff8j   Members   -  Reputation: 779

Like
0Likes
Like

Posted 19 July 2012 - 07:56 AM

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.

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#5 jeff8j   Members   -  Reputation: 779

Like
0Likes
Like

Posted 19 July 2012 - 07:58 AM

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.

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#6 taby   Members   -  Reputation: 336

Like
0Likes
Like

Posted 19 July 2012 - 01:15 PM

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, 19 July 2012 - 01:17 PM.


#7 jeff8j   Members   -  Reputation: 779

Like
0Likes
Like

Posted 19 July 2012 - 01:17 PM

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

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#8 WavyVirus   Members   -  Reputation: 735

Like
0Likes
Like

Posted 19 July 2012 - 04:23 PM

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.

#9 hplus0603   Moderators   -  Reputation: 5716

Like
0Likes
Like

Posted 20 July 2012 - 04:04 AM

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 };




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS