1

I have a PHP script on webserver 1 located in country A. I have a DB server located in country B.

The php script queries a large table from the DB server, groups the results, and inserts them back into the DB server (to another table). This is done by a single query (INSERT INTO SELECT...)

My question here is, does the data actually transfer between the web/db server? E.g. is this using GB's of bandwidth on both servers?

1 Answer 1

0

If you never deal with any retrieved data from DB server, then the query won't send any data to web server 1. Basically, if you just run the query, the only data that's sent is the text of the query (e.g. INSERT INTO SELECT...), which is probably just a few bytes, and then the response, which is just a success/fail value. That's it.

To say it another way, the data from the SELECT part of your INSERT INTO SELECT query is all dealt with on the DB server, it's never sent to webserver 1.

Even if you did run a SELECT query on a remote databse, you wouldn't get all of the results back. You actually get a resource. This resource is a reference to a set of data still on the remote database. When you do something like fetch_row on that resource, it fetches the next row. At that point, the data is transferred.

You can test this by monitoring the memory usage of your PHP script at various points in it's execution using memory_get_usage. Try:

echo "Memory before query:  " . memory_get_usage() . "\n";

$result = $mysqli->query("your select query");

echo "Memory after query:  " . memory_get_usage() . "\n";

$data = array();

$i=1;
while ($row = $result->fetch_row()) {
    $data[] = $row;
    echo "Memory after reading row " . $i++ . ": " . memory_get_usage() . "\n";
}

You should see a very small increase of used memory after your SELECT, and then a steady increase as your iterate over the results.

Sign up to request clarification or add additional context in comments.

7 Comments

That's what I hoped for. However, if I run a SHOW PROCESSLIST; on DB server it shows a State of Sending data for quite a while, hence the question raised.
I'm not sure what your SHOW PROCESSLIST output is (can't understand your comment), but SHOW PROCESSLIST doesn't show you information about data that is being transferred.
See this: stackoverflow.com/questions/10347193/… 'Sending data' doesn't mean 'transferring data'
Thanks. I suppose there is nothing to worry about then :-)
@TjeerdKramer I added some info. on how you could test this for yourself. I know it's nice when you have concrete data--it's comforting :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.