Add missing index on deliveries.created_at and make date filters sargable - #232
Merged
Merged
Conversation
… sargable
The deliveries table had no index on created_at, which every listing
query sorts by, and the from_date/to_date range filters used
whereDate('created_at', ...) in three separate call sites
(Api\DeliveryController::index, DashboardController::deliveries listing,
and its stat-card aggregate). Wrapping the column in whereDate() prevents
Postgres from using a plain B-tree index even if one existed, turning
every listing and dashboard load into a full table scan on the
highest-volume table in the app.
Add an index on created_at and replace whereDate() with direct
timestamp range comparisons (>= start of day, < start of next day) so
the filters remain index-friendly while preserving identical inclusive
day-range semantics.
Fixes #123
CI's Pint check runs --preset=psr12, which requires parentheses on anonymous class instantiation and the opening brace on its own line. Run vendor/bin/pint --preset=psr12 to match.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was broken
deliveriesis the highest-volume, continuously-growing table in the app, butcreated_at— the column every listing query sorts by — has no index. On top of that, thefrom_date/to_daterange filters usedwhereDate('created_at', ...), which wraps the column in a function and prevents Postgres from using a plain B-tree index even if one were added.This pattern was duplicated across three query call sites:
Api\DeliveryController::index()(the/api/v1/deliverieslisting)DashboardController::deliveries()(the dashboard listing)groupBy('status')count query, cloned from the same filtered query)As delivery volume grows, every one of these becomes a full table scan.
What changed
deliveries.created_at.whereDate('created_at', ...)with direct timestamp range comparisons (>= start of day,< start of next day) in both controllers, so the filters stay sargable and can use the new index, while preserving identical inclusive day-range semantics.tests/Feature/DeliveryDateRangeFilterBoundaryTest.php, covering:deliveries.created_at00:00:00and23:59:59)Testing
vendor/bin/pint --dirty— cleanphp artisan test— full suite passes (272 passed, 1 pre-existing skip)Fixes #123