I'm running a cron task which makes lots of queries to a MySQL
server. The big issue is that the server runs extremely slowly sometimes.
I've got a relatively big query with 4 tables left joined between them, and 4 smaller queries with natural join
s that also attack the first table. After throwing those queries, I then process the results and group them using PHP
.
What I'm planning is to somehow mix those 5 queries into just one big query, and then letting PHP
do some quick sort()
s when I need to do so.
I'm also told MySQL
queries run faster than PHP
in terms of filtering and sorting, but I'm reasonably worried about it when talking about having 7 or 8 left joins. Some more specs about these queries (which I can't copy because of company policies):
- Every fetched row and field will be visited at least once.
- Every query runs based on a single main table, with some "wing" tables.
- Every query uses the same
GROUP BY
rule. - Currently the
PHP
code splits some secondary queries results into multiple arrays. It should, if using a big query, also sort the results by multiple parameters.
So, due to these problems, and maybe as a rule of thumb:
What is faster, a big joined query with more PHP or multiple small selects with less PHP?