Massive slow down with more than one leftjoin
Massive slow down with more than one leftjoin
Link to test case:
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
Possibly similar to https://datatables.net/forums/discussion/comment/191112
I'm using the Editor PHP library and trying to add multiple LEFT JOINS. With one, it seems to work fine. But if I add a second, then almost every time I try to load the page, it times out with a 504 error. It's worked a few times, I think without me changing anything.
Code that works:
->leftJoin(
'mc_revs_language_id',
'mc_revs_language_id.row_index = main_collection.main_index AND mc_revs_language_id.timestamp = (SELECT MAX(LIZ.timestamp) FROM mc_revs_language_id LIZ WHERE LIZ.row_index = main_collection.main_index)'
)
Code that times out:
->leftJoin(
'mc_revs_language_id',
'mc_revs_language_id.row_index = main_collection.main_index AND mc_revs_language_id.timestamp = (SELECT MAX(LIZ.timestamp) FROM mc_revs_language_id LIZ WHERE LIZ.row_index = main_collection.main_index)'
)
->leftJoin(
'mc_revs_level',
'mc_revs_level.row_index = main_collection.main_index AND mc_revs_level.timestamp = (SELECT MAX(LZ.timestamp) FROM mc_revs_level LZ WHERE LZ.row_index = main_collection.main_index)'
)
This question has an accepted answers - jump to answer
Answers
It varies too. Now it seems that two leftjoin()'s works fine, but four of them times out. (I need six total.)
Here's the full code of the server-side script, with only one leftjoin active, the rest commented out:
Test case: https://comprehensibleinputwiki.com/ciwlibrary/test_case.php
Currently working since it's only one left join, but if I uncommented the rest, it would just timeout and error trying to get the serverside script.
Is there some sort of verbose log for the PHP library that might help me narrow down the issue?
Ok, so it might actually be really slow SQL. I turned on debug (and it started actually retrieving the page for some reason, but now a new deprecated warning appears, so the JSON is broken:
Deprecated: strcmp(): Passing null to parameter #1 ($string1) of type string is deprecated in /var/www/comprehensibleinputwiki.com/html/editor-php/lib/Editor/SearchPaneOptions.php on line 449
Anyway, I looked at the SQL in the debug file, and I tried running just one of the five or so queries directly in mysql, when I have two left joins. It took about 4.5 seconds.
But if I run SQL that I had written previously that does almost the same thing, except has all six desired joins, it takes 0.030 seconds:
It looks like
DataTable->sql()
might let me use my own SQL to speed this up, so I'll try that.Ooh ok, so I just had to create a VIEW using my faster SQL, then reference that from Datatables. The data seems to be returning quickly.
The only issue now is that the server is returning multiple
Deprecated
messages before the JSON, as I quoted in a previous comment, so the JSON can't be used for the table. So if anyone could tell me how to fix the issue or turn off the message, that'd be great, thanks.Hi,
Good to hear you found a VIEW to make things afters. One thing about the left joins - the additional left joins weren't selecting anything from the database - i.e.
mc_revs_enabled
didn't have any data pulled from it. That might well be what was hurting performance.Thanks for letting me know about that. We've been testing with PHP 8.2 which should have caught this, but we don't have null data in the SearchPanes example - so never saw it! I've committed the fix here.
You can either update your SearchPaneOptions.php file with that change, or add
->order('channelTitle')
to yourSearchPaneOptions
instance to get the SQL server to do the sorting.Allan
I don't think that's it, because I was trying many combinations of SQL, including using data from every table that was joined. I think it's an issue with my database structure, which I'm investigating.
Updated and works, thank you!
Looks like the issue was not having an INDEX on the row_index columns of the tables I was joining. It seems to work fast with the old code now.
Ah - yes, no index on the joined column would do it.
Thanks for letting me know it is working now.
Allan