Eric DayThoughts, code, and other oddments. |
Dark | Light |
|
|
|
< Will Sun’s job cuts hit MySQL? || OSS Business Model? > Asynchronous I/O – How It Could Speed Up Your AppMay 6th, 2008In a previous post I wrote about how I have started implementing asynchronous I/O into the MySQL client library. I plan on contacting and working with other client API maintainers (PHP, Python, Ruby, …) to make sure this functionality gets pushed out to those places too. Any comments or suggestions on how the interfaces should behave are of course welcome, and I’ll get patches posted somewhere for testing once I have the basics working. This is also my first project going through the MySQL Community Contributions Program so it can be included as part of a later release. I sent out the first contact e-mail to MySQL a week ago, but have not received any response yet. Anyone at MySQL listening? :) For those of you new to the idea of asynchronous clients, check out the Wikipedia pagefor an introduction. The basic idea is to be able to issue a query, have the function return immediately, do some other work (while the server processes the query), and then check for the query response and process the result (as normal). This may not be much of a gain (if any) for fast queries, but for those potentially sluggish queries that take a few hundred milliseconds, this could be significant (assuming you have something else to process during that time). This is especially so if you need to issue multiple queries that take a little time since they could all run in parallel connections. Let me demonstrate with a simple use case, ignoring error checking for brevity:
...
$mysql = mysql_connect("localhost", "myuser", "mypass");
$result = mysql_query("...query that takes 500ms...");
...process result...
$result = mysql_query("...query that takes 500ms...");
...process result...
...
Imagine this is part of a PHP script for our new webpage, and it needs to issue two queries to the MySQL server. Each of these queries takes approximately 500ms, resulting in a total processing time of ~1 second. Now if the code uses asynchronous I/O:
...
$mysql[0] = mysql_connect("localhost", "myuser", "mypass", 1, MYSQL_CLIENT_ASIO);
$mysql[1] = mysql_connect("localhost", "myuser", "mypass", 1, MYSQL_CLIENT_ASIO);
mysql_query_start("...query that takes 500ms...", $mysql[0]);
mysql_query_start("...query that takes 500ms...", $mysql[1]);
$result = mysql_wait($mysql[0]);
...process result...
$result = mysql_wait($mysql[1]);
...process result...
...
(Note, this is psuedo code, it will just break if you try it currently!) In this example, the “mysql_query_start” function calls will return immediately (send the packet out on the socket and return). This allows the server to process both of them in parallel, resulting in a total processing time closer to ~500ms. We just cut our page load time in half! (…well, ignoring network latency, but you get the idea) You can of course expand on this by doing some other time consuming processing before waiting, say image manipulation. You can also issue more than two SQL queries (possibly to different servers) and then collect all the responses in the end. I also plan on adding a query pool so that you can add multiple pending queries to it and wait for the first one that returns. This way you would not need to order your “mysql_wait” calls in how you *think* they will return, you will always get them in the order in which they finish. Again, this is still in it’s infancy, so please let me know if you have any comments or suggestions! Posted in MySQL24 Responses to "Asynchronous I/O – How It Could Speed Up Your App"
Leave a Reply< Will Sun’s job cuts hit MySQL? || OSS Business Model? > |
Blog Wiki About Resume RSS Comments Launchpad identi.ca OpenStack Scale Stack Gearman NW Veg Veg Food & Fit |
|
Copyright (C) Eric Day - eday@oddments.org All content licensed under the Creative Commons Attribution 3.0 License. Hosted by Rackspace Cloud |
|
Hi,
we have already code for ext/mysqli that does ASYNC queries but due to lack of time for extensive test writing (QA) we haven’t merged the code into PHP CVS. It’s implemented with mysqlnd, the new driver for PHP. Implementing with libmysql won’t be any harder after checking out the currently implemented ext/mysqli + mysqlnd code.
The sources are in our public svn :
http://svn.mysql.com/svnpublic/php-mysqlnd/trunk/
Look in php5/ext/mysqli/mysqli_nonapi.c for mysqli_poll().
mysqli_query() takes a thid parameter – how to store the data. Currently it’s either MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT. In the patched sources the valud is a bitmap -> MYSQLI_USE_RESULT & MYSQLI_ASYNC , or MYSQLI_USE_RESULT & MYSQLI_ASYNC
We have a small test file, which will show you how to use the ASYNC queries :
http://svn.mysql.com/svnpublic/php-mysqlnd/trunk/tests/ext/mysqli/mysqli_async_query.phpt