Skip to content

Potential performance issues with requests to DB #3

@volodymyrdovhan

Description

@volodymyrdovhan

Hello,
Plugin contains nested database queries within loops, causing N+1 query problems that can severely impact performance on production sites with large datasets.

  • How to fix:
    • Move DB calls outside loops by preloading required records in bulk.
    • Use get_records_sql() with grouped queries and get_in_or_equal() where applicable.
    • Iterate over in-memory results instead of querying $DB per loop iteration.

Documentation:

Code excerpts:

  • File: coursereport.php
239:     // Verdict severity ordering for roll-up "worst" verdict.
240:     $verdictseverity = [
241:         'LIKELY_HUMAN' => 0,
242:         'LOW_SUSPICION' => 1,
243:         'SUSPICIOUS' => 2,
244:         'PROBABLE_AGENT' => 3,
245:         'HIGH_CONFIDENCE_AGENT' => 4,
246:     ];
248:     foreach ($sessions as $session) {
249:         $totalsessions++;
251:         // Load the highest-scoring combined signal record for this session.
252:         $signalrecord = $DB->get_record_sql(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions