r/PHPhelp • u/[deleted] • Sep 27 '22
Solved Is making parallel SQL queries possible with PHP?
I bulk delete Reddit comments using Redact which also supports Twitter, Discord, Instagram, and data brokers.
groovy boat outgoing coordinated aspiring crayon encouraging existence sense nose
6
u/dabenu Sep 28 '22
I like the discussions here, but I think the most important question to ask is not if you can, but if you should.
What exactly do you hope to gain by running multiple queries (on the same dataset) in parallel?
The bottleneck here is, ultimately, the storage medium. You can't read the same data block twice at twice the speed. So even if you fire multiple queries at the same time, your DBMS is going to have to queue them and answer one by one. Sure there's some overhead in connecting to the db etc, but it's generally very little compared to the actual read time.
Whereas when you query an external API over http, you usually have a shitton of overhead that can be parallelized, and only a very tiny bit of the response time is ultimately reading data from a disk.
So in short, for this use-case parallelization doesn't really make sense. It might even make things slower since you need to add complexity to make it work.
And then lastly, you mentioned this is not a very efficient way to query the data in the first place, but something went wrong with the more efficient query. Instead of trying to fix that original problem, you now slap on a new problem. That's an anti-pattern that will soon bite you in the ass.
2
u/antduz Dec 10 '24
With SSD drives the bottleneck is more likely to be CPU, especially when part of the WHERE is not indexed and using some kind of ORM. But then again, you have to share that CPU with business users and you can only parallize it to the number of cores.
1
Sep 27 '22 edited Sep 27 '22
Since we cannot make one giant query we solved this problem by making one query after another, in 15 days intervals.
Did the LIMIT clause not work?
Is there anyway to run multiple "async" SQL queries with PHP?
Not from a web server, no. If you are running on the command line you can use a number of extensions like pcntl, parallel or the now deprecated pthreads. The latter both require ZTS which may or may not be available on your platform.
Databases should be reasonably fast, so why is this taking so long that you want to parallelize it? Where is all the time being spent? If it's on your query maybe you need and index on product.data or whatever you're joining on. If it is in network transport, then parallelization isn't going to help. If the slowness is elsewhere, do share as there maybe a way to speed up that specific process.
2
Sep 27 '22
Did the LIMIT clause not work?
Sorry, totally forgout about LIMIT. We are already using that with 1000 items batch on every query.
Databases should be reasonably fast, so why is this taking so long that you want to parallelize it?
Each query is relatively fast, like 10~20 seconds or so. Problem is the time period span across 3 or more years and we are making at least one inner join and three left joins on each query.
Also, for some reason the app runs slower the longer it stays up. For example, earlier today a full run took one hour, first queries took 10 or so seconds and by the time the run was ending each query was taking 30+ seconds.
2
Sep 27 '22
10~20 seconds
That's pretty slow... are we talking about 10s of millions of records here per result set?
for some reason the app runs slower the longer it stays up.
You are leaking resources somewhere. Are you leaving dangling connections or resources somewhere? You need to do some profiling here and figure out where the bottleneck is.
2
Sep 27 '22
That's pretty slow... are we talking about 10s of millions of records here per result set?
Not really i think. For example we need to send every customer present on the database from 2010 until present day, i did a count(<id>) as total today and got 2.8 million entries. Also, we are not using LIKE or anything like that, the WHERE clause is very simple and SELECT clause brings like 10 or so columns.
3
Sep 27 '22
You have got to be missing indexes or your running this on a Raspberry Pi.
2
Sep 27 '22
Could the fact that the application runs on a Docker container/PHP 7.4 @ Mac Pro be causing the slowness? So far i didn't consider the hardware to be the bottleneck.
The CLI shows the time it takes for PHP to process the data and the time the query takes. The processing consists of only like 10% of total time.
2
Sep 27 '22
That should be plenty of hardware for this. I strongly suspect your missing indexes but I am just guessing. Assuming your using MySQL or MariaDB run
EXPLAINon your query and see why its slow.3
u/BoredOfCanada Sep 28 '22
Just to say, it is indeed possible to run async MySQL queries from web-based PHP using the mysqli extension.
2
3
u/PetahNZ Sep 28 '22
You can run parallel queries with MySQLi:
echo "Starting" . PHP_EOL;
$connection1 = mysqli_connect($host, $user, $password, $database, $port);
$connection2 = mysqli_connect($host, $user, $password, $database, $port);
$connection1->query($query1, MYSQLI_ASYNC);
$connection2->query($query2, MYSQLI_ASYNC);
$poll = 0;
do {
echo "Polling, $poll ready" . PHP_EOL;
$read = $errors = $reject = [$connection1, $connection2];
$poll = mysqli_poll($read, $errors, $reject, 0, 100 * 1000);
} while ($poll < 2);
echo "Done" . PHP_EOL;
$result1 = $connection1->reap_async_query();
while ($row1 = $result1->fetch_object()) {
var_dump($row1);
}
$result2 = $connection2->reap_async_query();
while ($row2 = $result2->fetch_object()) {
var_dump($row2);
}
2
u/allen_jb Sep 28 '22
You mention this is a clients database, so this may not be an option. But the first thing I would look at is whether the query is using optimal indexes.
You can use EXPLAIN to get information on how MySQL is executing a query, including what indexes it's using (I find the FORMAT=JSON output particularly useful as it'll tell you exactly which parts of the query are (not) using indexes and what columns are being used from multi-column indexes).
Rick James' Index Cookbook is a useful guide on how to create better indexes for a given query.
1
u/VariationUpper2009 Sep 27 '22
The Union idea here is interesting! I'd look into that first.
You can also create multiple database connections by instantiating 2 connections and using different queries in each one. Handle each instance separately.
$oConn1 = new PDO($sDSN, $kSettings['username'], $kSettings['password'], $kOpts);
$oConn2 = new PDO($sDSN, $kSettings['username'], $kSettings['password'], $kOpts);
4
u/allen_jb Sep 28 '22
Creating multiple connections won't allow you to perform async queries with PDO. PHP will still wait for the results from one query before continuing your script to execute the next query (regardless of whether it's on the same connection).
As per other posts, mysqli supports async queries.
1
1
u/ishanvyas22 Sep 28 '22
For async PHP, you should checkout ReactPHP or Amphp. Both libraries also provides modules to run processes in parallel.
ReactPHP: https://reactphp.org/
Amphp: https://amphp.org/
2
u/RandyHoward Sep 27 '22
I am wondering why you can't do this all in a single query. Do you know you can use the UNION operator to combine results of multiple queries?