yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 857 forks source link

feat: Optimize simple queries #3135

Closed Arkhas closed 7 months ago

Arkhas commented 7 months ago

The issues was for simple queries with complexe wheres, a sub query was used resulting in the following query :

select count(*)
from (select "users".*
      from "users"
      where (select "title"
             from "posts"
             where "posts"."user_id" = "users"."id"
             order by "created_at" asc
    ) = ?
) count_row_table

resulting into an uneccessary temporary table, the query we would like to have would be this one :

select count(*)
from "users"
where (select "title"
       from "posts"
       where "posts"."user_id" = "users"."id"
       order by "created_at" asc
) = ?

On our app, the gains were massive, one of our datables would just timeout and with this change it load now in less than 5 seconds

sonarcloud[bot] commented 7 months ago

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

yajra commented 7 months ago

I guess this PR also fixes #3133. @abdulrhmansouda would you be able to review? Thanks!

abdulrhmansouda commented 7 months ago

I fixed this problem when I changed the isComplexQuery function #3133. and it's ready. you can review it.

yajra commented 7 months ago

Please run composer pr on your local machine to fix the linting. I haven't figured out yet how to automate in PR. Thanks!

yajra commented 7 months ago

Released on v11.1.0, 🚀 thanks!