I’ve been telling to people that we need async MySQL client API. The fact that you have to block while database box is thinking (doesn’t happen too often, but still..) is holding back some nice response-time oriented coding practices.
Developers of various proxies (not to be named :) admit that the major inspiration was adding features to proxies was easier for them, than getting into main MySQL (where the way minor enhancements are treated leads to pure stagnation — my opinion).
And now I realized there’s an easy way out, how to make client libraries more async. Simply, don’t use mysql protocol – talk HTTP, thats what dbslayer provides. Then application-level can use cURL, PEAR libraries or other ways to access it – often with way better parallelization or non-blocking capabilities.
Now, add more HTTP capabilities to the mix – stateful connections, pipelining, and benefits of protocol designed to serve the Web, not just talk to single server – start showing up.
Or of course, one could implement pipelining, parallel query and async capabilities to MySQL client libraries… Maybe afterwards MySQL server itself will start supporting query multiplexing in the protocol. But for now it is job for clients. Oh, and proxies.
The new MySQL driver for PHP has built-in support for async queries. If you use, you can use async queries in C/C++ if you wish. It’s just that it’s not marketed as async queries, but replication. Libmysql has the function mysql_send_query(), which does exactly what you want. See the following excerpt from the sources :
/*
Send the query and return so we can do something else.
Needs to be followed by mysql_read_query_result() when we want to
finish processing it.
*/
int STDCALL
mysql_send_query(MYSQL* mysql, const char* query, ulong length)
{
DBUG_ENTER(“mysql_send_query”);
DBUG_RETURN(simple_command(mysql, COM_QUERY, (uchar*) query, length, 1));
}
…
my_bool STDCALL mysql_read_query_result(MYSQL *mysql)
{
return (*mysql->methods->read_query_result)(mysql);
}
Voila. Here we go. You want async queries? They are partly there. There is no poll, but won’t be too hard to implement.
s/if you use/if you use libmysql/
mysqlnd already provides async api to PHP ;)
OK, I see how it is done in mysqlnd – though it asks for multiple connections then, and doesn’t provide polling hooks (ergh, where is the documentation for these features?:)
Oh, and of course – mysql_send_query doesn’t have a manual entry…
MySQL Cluster has a lovely Async API …
Talk to Jim and Georg about it @ Connector/C. Its on their both whishlist, AFAIK. I think Jan has blogged about how it could be hacked.
mysqlnd async “docs” are here http://www.slideshare.net/nixnutz/mysqlnd-async-ipc2008-presentation and I might have a bit more in my blog. Though, I must confess that we have not worked on it since three months.
Judging from the questions Tony asked yesterday, he is done with .NET documentation and back to PHP work. So, there is some hope that he will eventually catch up one fine (soon?) day.
Ulf