->leftJoin Slow down the script
->leftJoin Slow down the script
Hello.
I have two tables customers and customer address (approximately 3500 records).
If I only get records from the client table, the generation takes 0.0573 seconds
$start = microtime(true);
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
$db->sql("SET names utf8");
Editor::inst($db, 'client')
->fields(
Field::inst('client.id','id'),
Field::inst('client.phone','phone'),
Field::inst('client.fio','fio'),
Field::inst('client.mail','fio'),
Field::inst('client.discount','discount'),
Field::inst('client.card','card'),
Field::inst('client.black_list', 'bl'))
->debug(true)
->process($_POST)
->json();
But if I do one leftJoin with one column then the execution time becomes 2.1287 seconds
$start = microtime(true);
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Options,
DataTables\Editor\Upload,
DataTables\Editor\Validate,
DataTables\Editor\ValidateOptions;
$db->sql("SET names utf8");
Editor::inst($db, 'client')
->fields(
Field::inst('client.id','id'),
Field::inst('client.phone','phone'),
Field::inst('client.fio','fio'),
Field::inst('client.mail','fio'),
Field::inst('client.discount','discount'),
Field::inst('client.card','card'),
Field::inst('client.black_list', 'bl'),
Field::inst( 'client_delivery.city' )
)->leftJoin( 'client_delivery', 'client_delivery.id_client', '=', 'client.id')
->debug(true)
->process($_POST)
->json();
If this query is executed at phpmyadmin then the time in the first case is 0.0012, and in the second with leftJoin 0.017
please tell me, it should be so that with such a small number of records, the time changes so significantly from 0.05 to 2 seconds due to one leftJoin
But if you replace leftJoin
->join(
Mjoin::inst( 'client_delivery' )
->link( 'client.id', 'client_delivery.id_client' )
->fields(
Field::inst( 'city' )
)
)
then the execution time is 10 times less than 0.1537 seconds
It turns out that you should avoid using leftJoin in editor server script
Replies
Sounds odd - I've not encountered such a dramatic slow down myself when using left join before. Indeed, it should always be faster than an Mjoin, since it is just one query rather than the two used for an Mjoin.
Can you show me the JSON response from the server when you have the left join enabled please? I'm wondering if you might be best adding a index to
client_delivery.id_client
if it doesn't have one already.Allan
Sorry for delay.I added an index, but it did not give a noticeable increase in the speed of generating the response.
answer with one left join
lead time 2.1446
This is the query being run:
if you run that on your SQL server directly (MySQL by the looks of it, so you might use phpMyAdmin, MySQL Workbench of something else), how long does it take to run?
Also you can run the query with
EXPLAIN
at the start and the database will give details about the timing of the query.Allan
Showing lines 0 - 24 (total 3488, Query executed in 0.0214 seconds.)
Are you able to give me a link to the page so I can investigate further please?
Thanks,
Allan
Sent you a private message
I'm not seeing it I'm afraid. If you click my forum user name above and then "Send message" that will come to me.
Thanks,
Allan
That's exactly what I did. Here's a screenshot.
I also duplicated it additionally.
Got it this time - thanks! I've replied there.
Allan
Posted in private messages