⚡ Optimize random question selection to avoid ORDER BY rand()#12
⚡ Optimize random question selection to avoid ORDER BY rand()#12
Conversation
Replaces the inefficient `ORDER BY rand()` with a strategy that fetches IDs, shuffles them in PHP, and then fetches the specific rows. This avoids full table scans and sorts on the database side for large tables. Benchmark results (SQLite simulation with 100k rows, large payload): Original: 7.37s Optimized: 5.51s Improvement: ~25% Co-authored-by: xRahul <1639945+xRahul@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
This PR optimizes the random question retrieval in
quiz_system_git/quiz.php.What
Replaced the following query pattern:
With a multi-step approach:
SELECT id ...).WHERE id IN (...)).Why
ORDER BY rand()is known to be inefficient on large tables because it typically forces the database to generate a random number for every row, create a temporary table, and perform a file sort (O(N log N)).By moving the shuffle logic to the application layer (which operates on lightweight integers) and only fetching the heavy row data for the displayed questions, we significantly reduce database load.
Verification
A benchmark script (using SQLite in-memory with 100,000 rows containing large text fields) showed a ~25% performance improvement (7.37s -> 5.51s). The improvement is expected to be even more significant on a production MySQL server where I/O and sorting overheads are higher.
The logic preserves the random display order of the questions.
PR created automatically by Jules for task 14001401200639589268 started by @xRahul