-
Improvement
-
Resolution: Fixed
-
Minor
-
Future Dev
-
MOODLE_405_STABLE
-
MDL-78030-main-improvement -
-
6
-
Team Hedgehog 2023 Sprint 4.3, Team Hedgehog 2023 Review 4, Team Hedgehog 2024 Sprint 1.1, Team Hedgehog 2024 Sprint 1.2, Team Hedgehog 2024 Sprint 1.3, Team Hedgehog 2024 Sprint 1.4, Team Hedgehog 2024 Review 1, Team Hedgehog 2024 Sprint 2.1, Team Hedgehog 2024 Sprint 2.2, Team Hedgehog 2024 Sprint 2.3, Team Hedgehog 2024 Review 2
If you have any sufficiently large data set the query behind anything in tablelib and several places where reports are used, operations will get slower. When viewing the report it grabs a page of data, and it also grabs the total count of records without the pagination. Both of these raw queries take effectively the same amount of time to run which doubles the report time and db load.
Using window functions both of these can be combined into a single query for an easy 2x performance win across the board in all reports with no changes to any of the reports which is a pretty rare win
So for a query like this:
SELECT username
|
FROM {user}
|
WHERE username LIKE '%gmail%'
|
we can auto wrap it like this:
SELECT results.*, COUNT(*) OVER() AS full_count FROM (
|
SELECT username
|
FROM {user}
|
WHERE username LIKE '%gmail%'
|
) results
|
This takes no appreciable difference from the original query.
A more detailed example with some edge cases to cover is here:
Window function support should be fairly good across all supported DB's and we can fall back to existing count where it isn't.