feat: add index on actions(time) to improve time-range query performance#369
Open
netcrafts wants to merge 1 commit intoQuiltServerTools:masterfrom
Open
feat: add index on actions(time) to improve time-range query performance#369netcrafts wants to merge 1 commit intoQuiltServerTools:masterfrom
netcrafts wants to merge 1 commit intoQuiltServerTools:masterfrom
Conversation
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.
Problem
The
actionstable has indexes onaction_id,object_id,old_object_id,source,player_id, and a composite(x, y, z, world_id)— but no index onthe
timecolumn. This means every time-range query (/ledger lookup before:... after:...) and every auto-purge (deleteWhere { timestamp lessEq ... }) performsa full table scan on the largest column in the database.
Changes
1. New databases —
Tables.ktThe
timestampcolumn declaration inActionsnow includes.index("actions_time"):This follows the same pattern used by every other indexed column in the table.
Exposed's
SchemaUtils.create()will include the index when creating the tablefor the first time.
2. Existing databases —
DatabaseManager.ktensureTables()now runs an idempotentCREATE INDEX IF NOT EXISTSafterSchemaUtils.create(), which creates the index on databases that alreadyexisted before this change:
The
try/catchonSQLExceptionhandles the edge case whereCREATE INDEX IF NOT EXISTSis not supported — this syntax was added in MySQL 8.0.12 (it issupported in all versions of SQLite, MariaDB, and PostgreSQL). On an incompatible
MySQL version the server logs a warning and continues normally rather than
crashing on startup. MySQL 5.7 is EOL so this is an unlikely scenario in practice.
Testing
Tested on a live MariaDB 10.11 server:
timeexisted (SHOW INDEX FROM actions WHERE Column_name = 'time'returned empty)actions_timeBTREE index was present and correct