Fix MySQL DELETE using CONCAT_WS preventing index usage#5756
Open
hashwnath wants to merge 1 commit intoSQLMesh:mainfrom
Open
Fix MySQL DELETE using CONCAT_WS preventing index usage#5756hashwnath wants to merge 1 commit intoSQLMesh:mainfrom
hashwnath wants to merge 1 commit intoSQLMesh:mainfrom
Conversation
Override _replace_by_key in MySQLEngineAdapter to use a JOIN-based
DELETE for composite keys instead of CONCAT_WS('__SQLMESH_DELIM__', ...).
MySQL/MariaDB cannot use indexes on CONCAT_WS expressions, causing full
table scans on every incremental run. The JOIN-based approach allows the
engine to use existing composite indexes, reducing query times from hours
to seconds on large tables.
Single-key DELETEs continue to use the efficient IN-based approach.
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.
Summary
Fixes #5711
_replace_by_keyinMySQLEngineAdapterto use a JOIN-based DELETE for composite keys instead ofCONCAT_WS('__SQLMESH_DELIM__', ...)which prevents MySQL/MariaDB from using indexesIN-based approach since indexes work fine for single columnsProblem
When using
INCREMENTAL_BY_UNIQUE_KEYwith MySQL/MariaDB and composite keys, SQLMesh generates:MySQL cannot use indexes on
CONCAT_WS()expressions, causing full table scans that lead to multi-hour query times and connection timeouts on larger tables.Fix
For composite keys, generate a JOIN-based DELETE that allows MySQL to use existing indexes:
This changes the EXPLAIN plan from
type=ALL(full scan) totype=ref(index lookup), reducing query times from hours to seconds.Test plan
test_replace_by_key_composite_uses_join_deleteverifies JOIN-based DELETE for composite keystest_replace_by_key_single_key_uses_inverifies single-key DELETEs still use the IN-based approach