Mysqli fetch_assoc() loop vs. Mysqli fetch_all() database load? - php

Strictly from MySQL's point of view (database performance, not PHP performance) what's the difference between a Mysqli fetch_assoc() loop vs. Mysqli fetch_all() when retrieving query results?
Let's say for $result = $qdb->query("SELECT name, id FROM cats");
In other words, does each additional fetch_assoc() or fetch_array(MYSQLI_NUM) iteration result in more MySQL communication or is the entire query result already pulled from MySQL at one time?
In other words, can Mysqli fetch_all() make life easier for MySQL?
To emphasize, I'm only concerned with what MySQL hears and responds with, if there's any difference. This is not a question about PHP performance, why one way is better than the other, etc. Also, this is not a PDO question http://php.net/manual/en/mysqli-result.fetch-all.php

From reading the code, mysqli_fetch_assoc() fetches one row.
Whereas mysqli_fetch_all() calls mysqlnd_fetch_all(), which use a loop to fetch one row at a time until all rows have been fetched, then it breaks out of the loop.
Here's the relevant function in mysqlnd, edited for length:
MYSQLND_METHOD(mysqlnd_res, fetch_all)(MYSQLND_RES * result, unsigned int flags, zval *return_value TSRMLS_DC ZEND_FILE_LINE_DC)
{
...
do {
MAKE_STD_ZVAL(row);
mysqlnd_fetch_into(result, flags, row, MYSQLND_MYSQLI);
if (Z_TYPE_P(row) != IS_ARRAY) {
zval_ptr_dtor(&row);
break;
}
add_index_zval(return_value, i++, row);
} while (1);
...
}
So the answer is: from the point of view of the MySQL server, there is no such thing as "fetch all." The PHP extensions either fetch one row, or else fetch one row at a time until all the rows in the result set have been fetched.

Strictly from MySQL's point of view (database performance, not PHP performance) neither Mysqli fetch_assoc() nor Mysqli fetch_all() has any significance.
Strictly from general performance point of view, there is not a slightest difference. You can use anything that suits you more from application design, sensibility and readability point of view.

Related

Mysqli dynamic query bind_results

I wrote one databasse class which I use often and I used mysqli.I wanted to write it with PDO but it was slow (It is not about ip connection :) ),and my website is really huge and this pdo little slowness will be really big problem,that's why I choosed difficult way --Mysqli--.I wrote some dynamic class which bind params dynamicly and easily usage like this:
DB::getInstance()->query(sql,'ss',array($a,$b));
This was really useful untill today.I wanted to get result and also count values but I discover reaally big problem that when I use num_rows mysqli get_result will not work,When I use get_result num rows will never work,also when I use get_result and if I want to use it again for same query second one will not work
.Also get_result is not good function because it support only mysqlid.Then I have tried bind result which is useless because every select query I should write bind_result(params) which is not good for other developers on company also.What Should I do?Pdo is slow and for my website it is really slow,mysqli is not for developers it increase development time.How can I bind results dinamicly for query?I want something like I will write sql statement and bind result should get column names dinamicly bind them aoutomaticly and then I will write fetch() and I will write column names and I will get result.How can I do that?
When I use get_result num rows will never work
this is not true
besides, you never need num rows anyway
when I use get_result and if I want to use it again for same query
you don't want it
get_result is not good function because it support only mysqlid
this is true
however, if your site is so big and distinct, there is no problem to install a required module or two.
How can I bind results dinamicly for query?
use get_result.
To reuse a result get all the rows into array using fetch_all() and then use this array anywhere you wish
Instead of num_rows just fetch the data and see whether anything was fetched or not.

which mysqli functions do a client server round trip

I want to understand how many client sever calls are made for a typical mysqli query?
Step(1) $result = mysqli_query($link, $query);
Depending on the type of query, we use other mysqli function after this like
mysqli_fetch_fields, mysqli_affected_rows, mysqli_insert_id, mysqli_fetch_row
etc. then we close the result object.
Now, is all data retrieved and stored in php memory after step (1)?
Or mysqli_fetch_fields, mysqli_insert_id etc makes another call to mysql server?
Reason for asking: Trying to understand how mysqli calls work. But can not find such explanation anywhere for beginners like me.
PHP MySQLi API is built on MySQL C API. So it would be better if you have knowlegdes of it.
Basically, SELECT query could generate large ResultSet and this ResultSet is transfered from Server to Client when you call PHP's mysqli_store_result() (In C API, mysql_store_result()).
C API mysql_fetch_row() just returns a pointer to MYSQL_RES* (which is already stored in PHP right after mysql_store_result(). But 'mysqli_fetch_row()` would require some memories to make PHP's array.
mysqli_insert_id() (which is last_insert_id() of C API) just returns insert id of MYSQL connection data stucture which means there is no extra memory for insert id.
If you want to know how MySQLi works, I would recommand to learn MySQL C API and see PHP source codes.
mysqli_query runs the query on the server and returns false is the query failed, true is the query was successful but did not return anything (UPDATE query for example) or a mysqli_result otherwise. That mysqli_result is a class that extends Traversable interface, so yes, it's in memory. All other functions mysqli_fetch_fields, mysqli_affected_rows etc. are just methods in that class so those just read what's already in memory.
For more details, read this: php documentation
The documentation tells you everything you need to know about mysqli.
mysqli_query execute the query and returns a query object, this object has some methods and amongst them there are:
mysqli_fetch_fields:
Returns an array of objects representing the fields in a result set
mysqli_affected_rows:
Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query.
mysqli_insert_id:
Returns the auto generated id used in the last query
mysqli_fetch_row:
Get a result row as an enumerated array
Being all method of an object they don't execute sql requests, they simply access object values and gives you back different results depending on the method.

Add element to array in the conditional statement of a loop in PHP

I have this piece of code:
while ($i = $res->fetchArray(SQLITE3_ASSOC))
{
$items[] = $i;
}
I tried neatening it to this:
while ($items[] = $res->fetchArray(SQLITE3_ASSOC));
It looks very satisfying but I now get an extra element at the end of the array (when the call to fetchArray() returns false. Is there a way of writing this statement without getting the extra element at the end?
If you're using PDO as your database library, you should use the fetchAll() method (documentation)
$items = $stmt->fetchAll(PDO::FETCH_ASSOC)
This will provide you a bidimensional associative array. Its index goes from 0 to n-1 where n is the fetched rows count and every row contains an array with column names as indexes. For example:
$items[3]['id']
will contain the value stored in the id column of the 4th fetched row.
if you're using mysqli_* instead, there is mysqli_fetch_all() but it's discouraged because it's more expensive rather a loop of mysqli_fetch_array(), or so the documentation says.
If you're using a third party library, consult the provided documentation. If there is none provided or there is no fetchAll equivalent it's a sign of poor quality. Drop it and use the native PDO instead.
Since you're using SQLITE3 driver, i suggest you to look at this page: SQLITE (PDO) which explains how to use PDO with SQLITE3. Believe me, it's worth it. Most probably you won't stick to SQLITE for long and when you'll migrate to MySQL or PostgreSQL you'll thank me for this read.
PDO's main advantage is that's (usually) transparent to the user regarding which DB is below. Therefore, it shouldn't break your application if you change database, just change the PDO connection string and it'll be enough.
Try
while(($item=$res->fetchArray(SQLITE3_ASSOC))&&$items[]=$item);

Fetch all SQL query into an array at a time?

Well, the php code below successfull adds up all the rows in the url field.
$sql = "SELECT * FROM table WHERE url <> ''";
$result = mysql_query($sql,$con);
$sql_num = mysql_num_rows($result);
while($sql_row=mysql_fetch_array($result))
{
urls[] = $sql_row["url"];
}
The problem is that if the list of url are in millions, then it takes a lot of time (especially in localhost). So, I'd like to know anothe way of getting the sql query result directly into an array without using a loop. Is it possible?
The problem is not the loop, it's that you are transferring millions of pieces of data (possibly large) from your database into memory. Whichever way you're doing that, it'll take time. And somebody needs to loop somewhere anyway.
In short: reduce the amount of data you get from the database.
You should consider using mysqli for that purpose. The fetch_all() method would allow to do that.
http://php.net/manual/en/mysqli-result.fetch-all.php
UPDATE
As per comments, I tried both methods. I tried using mysql_fetch_array in a loop, and using mysqli::fetch_all() method, on a large table we have in production. mysqli::fetch_all() did use less memory and ran faster than the mysql_fetch_array loop.
The table has about 500000 rows. mysqli::fetch_all() finished loading the data in an array in 2.50 seconds, and didn't hit the 1G memory limit set in the script. mysql_fetch_array() failed from memory exhaustion after 3.45 seconds.
mysql is deprecated, and the functionality you want is found in mysqli and PDO. It's the perfect excuse to switch to the newer MySQL extensions. For both mysqli and PDO, the method is fetchAll (note that the mysqli::fetch_all requires the mysqlnd driver to run).
there is no option for it in mysql. though you can use pdo's fetchall()
http://php.net/pdostatement.fetchall

Get Number of Rows from a Select Statement Efficiently

Until recently I've been using mysql_real_escape_string() to fix most of my variables before making SQL queries to my database. A friend said that I should be using PDO's prepared statements instead, so after reading a bit about them I'm now switching over to them.
I've only encountered one problem so far in switching over, and that's counting the rows to returned by a SELECT statement. On occasion in my code, I'd run an SQL query and then count the number of rows returned from the SELECT statement. Depending on whether a result set returned, I would take different actions. Sometimes I do need to use the result set from it. MySQL let me go straight to mysql_fetch_assoc() after mysql_num_rows() with no problem. However, PDO doesn't seem to have anything like mysql_num_rows().
I've been reading some responses on SO that gave me a solution, to either use COUNT() in the SQL statement or to use the PHP function count() on the result set. COUNT() would work fine in the SQL statement if I didn't need the result set in some places, however, several people have mentioned that using count() on the result set is fairly inefficient.
So my question is, how should I be doing this if I need to count the number of rows selected (if any), then run a script with the result set? Is using count() on the result set the only way in this case, or is there a more efficient way to do things?
Below is a short example of something similar to my previous SQL code:
$query=mysql_query('SELECT ID FROM Table WHERE Name='Paul' LIMIT 1);
if(mysql_num_rows($query)>0)
{
print_r(mysql_fetch_assoc($query));
}
else
{
//Other code.
}
Thanks.
EDIT
I do know that you use fetchAll() on the statement before counting the result set (which gives me what I need), but I'm just trying to figure out the most efficient way to do things.
$stmt->rowCount();
http://php.net/manual/en/pdostatement.rowcount.php
the rows must be fetched(buffered into memory, or iterated) for it to work. It's not uncommon for your pdo driver to be configured to do this automatically.
You will have to use Count(). You can run two queries like
SELECT COUNT(ID) FROM Table WHERE Name='Paul'
one you have get the count, then run the query with select clause
SELECT ID FROM Table WHERE Name='Paul' LIMIT 1
Count() function is not inefficient at all if you are using it like COUNT(ID), because most probably id is primary key and have an index. MYSQL wont even have to access the table.

Categories