Skip to content

Query Performance Assertions: Index Usage, Duplicates & Timing

Choose a tag to compare

@mattiasgeniar mattiasgeniar released this 12 Jan 13:36
· 67 commits to master since this release
7fbf8ba

Query Performance Assertions: Index Usage, Duplicates & Timing

This release adds powerful query performance assertions to catch inefficient queries in your tests.

New Assertions

Index Usage Detection

Detect full table scans and missing indexes by running EXPLAIN on your queries:

$this->assertAllQueriesUseIndexes(function () {
    User::where('email', 'test@example.com')->first();
});

Supports MySQL, MariaDB, and SQLite. Detects full table scans, unused indexes, filesort, temporary tables, and more.

Duplicate Query Detection

Catch repeated identical queries:

$this->assertNoDuplicateQueries(function () {
    User::find(1);
    User::find(1); // Fails - duplicate
});

Query Timing Assertions

Set performance budgets for your queries:

$this->assertMaxQueryTime(100, fn() => ...);    // No single query over 100ms
$this->assertTotalQueryTime(500, fn() => ...);  // Total time under 500ms

Row Count Threshold (MySQL/MariaDB)

Fail when queries examine too many rows:

$this->assertMaxRowsExamined(1000, fn() => User::where('status', 'active')->get());

Combined Efficiency Assertion

Check everything at once—N+1, duplicates, and index usage:

$this->assertQueriesAreEfficient(function () {
    $users = User::with('posts')->get();
});

Or use trackQueriesForEfficiency() in setUp/beforeEach for test-wide tracking.

Custom Query Analysers

Add support for additional databases:

AssertsQueryCounts::registerQueryAnalyser(new PostgresAnalyser());

Other Improvements

  • Issue severity levels (ERROR, WARNING, INFO) with configurable failure thresholds
  • Small table optimization: ignores full scans on tables < 100 rows
  • FK constraint context in SQLite scan warnings
  • PHPStan analysis added to CI

Backwards Compatibility

Fully backwards compatible. All existing methods unchanged.