Skip to content

Conversation

@xRahul
Copy link
Owner

@xRahul xRahul commented Jan 27, 2026

This PR optimizes the random question retrieval in quiz_system_git/quiz.php.

What

Replaced the following query pattern:

SELECT * FROM questions WHERE quiz_id=:quizID ORDER BY rand() LIMIT :limit

With a multi-step approach:

  1. Fetch all IDs for the quiz (SELECT id ...).
  2. Shuffle and slice IDs in PHP.
  3. Fetch full question data for only the selected IDs (WHERE id IN (...)).
  4. Re-order the results in PHP to match the shuffled order.

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

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>
@google-labs-jules
Copy link
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants