PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, March 1, 2022

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

 March 01, 2022     apache, lamp, mysql, php     No comments   

Issue

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.


Solution

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.



Answered By - piotrp
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing