Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PHP issue multi processed mysql queries in an Apache environment?

I have a single database server with 4 cores and a web server running PHP. I want one PHP script to be able to issue queries to the database server such that they execute in parallel on the database server, one on each mysqld process. Typically in PHP you do this:

$sql = new mysqli( [insert connection parameters] );
$sql->query( "SELECT 'Complex Query A'" );
$sql->query( "SELECT 'Complex Query B'" );
$sql->query( "SELECT 'Complex Query C'" );
$sql->query( "SELECT 'Complex Query D'" );

But these run serially and only utilize one mysqld process. In this application, each query (A through D) is processing a different part of the data, but working on the same set of InnoDB tables.

One possible solution is to make AJAX calls to apache to break it down into sub-scripts that might run in parallel, but I'm guessing that Apache will process those ajax calls sequentially with one httpd process per client.

Is there a way to achieve this? Does anyone have experience with the mysqlnd MYSQLI_ASYNC features? Can they work in parallel with a single database server and mysqli connection?

Purpose: we run real-time analytic tools that generate graphs and I'd like to take advantage of the processing power on our database to speed up the queries which take time.

like image 635
Stu Avatar asked Aug 25 '11 10:08

Stu


People also ask

How run multiple MySQL queries in PHP?

Multiple statements or multi queries must be executed with mysqli::multi_query(). The individual statements of the statement string are separated by semicolon. Then, all result sets returned by the executed statements must be fetched.

Does MySQL work with Apache?

Remember to start the apache and mysql servers before you run any script to access a table in MySQL using your Apache Web server. In Windows open Computer Management, Services and start each service in turn. In Linux using the KDE desktop open services applet and start both httpd and mysqld services.

Which of the following methods connect MySQL database using PHP?

There are two popular ways to connect to a MySQL database using PHP: With PHP's MySQLi Extension. With PHP Data Objects (PDO)

Is PHP faster than MySQL?

MySQL is faster in scope of SQL query. PHP is faster in PHP code. If you make SQL query to find out SQRT() it should be definitely slower (unless PHP is broken) because MySQL parser and networking overhead.


1 Answers

What you need is asynchronous query execution. It is possible starting with PHP 5.3 when you have the new mysqlnd driver - then you are able to pass MYSQLI_ASYNC flag to query() method. Script execution will continue without waiting for your query to finish. There is a nice usage example available in PHP manual in comments for the poll() method.

You will need a separate MySQL connection for each query that you want to execute in parallel.

like image 151
piotrp Avatar answered Oct 17 '22 05:10

piotrp