Jump to content

mysql_fetch_array vs mysql_fetch_assoc


The Little Guy

Recommended Posts

which is faster and/or better?

 

mysql_fetch_array

or

mysql_fetch_assoc

 

what do you recommend using?

 

I would assume that mysql_fetch_assoc is faster because it returns only the column names as keys while mysql_fetch_array returns 2 of each column the name as a key, and a number starting at 0

 

what do you think or know about this?

Link to comment
Share on other sites

mysql_fetch_array() returns essentially two arrays, one with a numeric index and one with an associative based key index. Thus using mysql_fetch_array() without specifying which method you want (either MYSQL_NUM or MYSQL_ASSOC) always returns a double array, which is considerably more inefficient as compared to mysql_fetch_row or mysql_fetch_assoc.

 

I normally use mysql_fetch_assoc() myself.

Link to comment
Share on other sites

They have different uses and I believe since fetch_array returns essentially two array one numeric index and one with an associative based key index, then it makes a bit slower than the mysql_fetch_assoc(). it has more information to process..

 

But I as have used in the pass it will depends on the needs you have.

Link to comment
Share on other sites

I usually use mysql_fetch_array(), but it looks to me that mysql_fetch_assoc() would be a little quicker (probably not much but none the less still quicker).

 

I think I may want to start using mysql_fetch_assoc()...

 

Benchmarking 10000 iterations returns that _assoc() is on average 35% faster when pulling out 100 rows. Atleast on WAMP.

Link to comment
Share on other sites

I dont think there is a noticeable difference at all..

I normally using mysql_fetch_array() with MYSQL_ASSOC or MYSQL_NUM but never both..

I guess it comes down to preference..

 

Or you could actually do a test and see which is faster.

You could test:

mysql_fetch_row, mysql_fetch_object, all 3 flavours of mysql_fetch_array and mysql_fetch_assoc.. Would be interesting to see any noticeable difference.

Link to comment
Share on other sites

One has to be careful when using _array flavor. In one of my scripts I need to send large query results through JSON (and I mean large). The json_encode makes it easy because you don't have to care what is in the array you're encoding, but I was pretty surprised to see that my JSON requests are twice as large as they should be. Sure enough, I was sending results from fetch_array instead of fetch_assoc.

Link to comment
Share on other sites

I usually use mysql_fetch_array(), but it looks to me that mysql_fetch_assoc() would be a little quicker (probably not much but none the less still quicker).

 

I think I may want to start using mysql_fetch_assoc()...

 

Benchmarking 10000 iterations returns that _assoc() is on average 35% faster when pulling out 100 rows. Atleast on WAMP.

 

Who says that 10000 iterations and 100 rows is statistically representative? You cannot always just draw conclusions like that. Many things are circumstantial.

 

For example, for sorting small amounts of data or data that is already nearly sorted, bubble sort works quite fine. However, for larger amounts of data that is highly randomized it's inefficient because its worst-case running time is quadratic.

 

Similarly, consider the two following series: an=n10 and bn=2n-1. Here, a50 > b50, but a1000 < b1000 (way smaller in fact). Indeed all exponential functions will "win" (in terms of getting the highest number) over any polynomial function for sufficiently large input. How is this relevant? Well, the two series could represent running times for two algorithms. In this case, b would then be best for small input, and a would be best for large input.

 

It may be that the numbers you picked aren't good enough and in this case I think these two functions relative efficiency is somewhat a moot point.

 

Moreover, 35% doesn't say very much. Someone who has 3 posts on PHP Freaks has 300% more posts than someone with only 1 post. This is much different than 15000 vs 5000 posts.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.