Virtual layer update is O(N²) in Python and does not scale with concurrent_tasks
Les mainteneurs répondent en général sous 4 jours
@mday-io y travaille déjà.
Depuis le 14/9/2026.
Évaluation
Cette issue n'a pas encore été évaluée.
Description
cc @cmgoffena13
Environment
- sqlmesh 0.227.1 (code paths below are unchanged on
main, 0.236.1), sqlglot 27.28.1 - Python 3.13, BigQuery engine, ~2,400 promoted snapshots per environment
concurrent_tasks: 16(raising it to 64 changed nothing)
Symptom
sqlmesh plan --include-unmodified <dev_env> spends 40–50 minutes in "Updating virtual layer" for ~2,400 views.
BigQuery INFORMATION_SCHEMA.JOBS shows every job finishing in 1–2 s with no errors, no queueing and long idle stretches, i.e. the warehouse is not the bottleneck. Thread dumps (py-spy) show the promotion workers busy in pure Python. Because dev environments never partially promote (Environment.can_partially_promote requires name == 'prod'), every dev plan pays this for the whole environment.
Root causes (per promoted view)
-
Property rendering re-normalizes the entire view mapping, twice per render.
_promote_snapshotrenderssession_propertiesandvirtual_propertieswithtable_mapping = to_view_mapping(<all env snapshots>)(N entries).ExpressionRenderer.render→_renderresolvesthis_modelunconditionally through_resolve_table, which callsexp.replace_tables(table, {**mapping, **table_mapping}), then_resolve_tablescallsexp.replace_tablesagain on the rendered expression. sqlglot'sreplace_tablesnormalizes every mapping key withnormalize_table_name(a fullparse_one) on every call. Result: ~4N table-name parses per view for two property expressions that contain no table reference at all (label arrays), so a promotion of N views costs O(N²) parses. Profile: 58,580normalize_table_namecalls for 20 renders at N=1,464 (99% of the render time); a static literal property costs the same as a macro call, so this is not macro evaluation.
(sqlmesh/core/renderer.py_render/_resolve_table/_resolve_tables,sqlmesh/core/model/definition.py_render_properties,sqlmesh/core/snapshot/evaluator.py_promote_snapshot) -
to_table_mappingover all environment snapshots, twice per view.
PromotableStrategy.promotepassessnapshots=<all env snapshots>torender_virtual_properties;_resolve_tablesand_resolve_tableeach callto_table_mapping(snapshots.values(), deployability_index), i.e.Snapshot.table_name()(parse + SQL generation) for every snapshot in the environment, per view. Note this only shows up with versioned snapshots (to_table_mappingskipsversion is None), so a profile on a freshly loaded Context misses it.
(sqlmesh/core/snapshot/evaluator.pyPromotableStrategy.promote,sqlmesh/core/snapshot/definition.pyto_table_mapping) -
sqlglot: the lazy dialect loader takes a lock on every import.
sqlglot/dialects/__init__.pyresolvesfrom sqlglot.dialects import Dialectthrough a module__getattr__that acquires an RLock and callsimportlib.import_moduleeach time; the attribute is never cached into the module namespace.Expression.sql()andParser.__init__both do that import, so oneto_table_mappingbuild performs ~16,000 locked calls (N=1,338). With 9–16 promotion threads this becomes a lock convoy: one build takes 85 ms single-threaded and ~2 s per thread with 9 threads on Linux; on macOS the run appeared frozen for many minutes at 92% (all workers sampled inside__getattr__), and Ctrl-C printed "Aborted!" without exiting because the non-daemon workers were mid-call. -
Smaller item on the same path:
_promote_snapshotrebuildssnapshot_by_name = {s.name: s for s in snapshots.values()}for every view (O(N) per view, 70–120 ms here) just forrender_on_virtual_update.
More threads won't help
All of the above is CPU work under the GIL. Measured per-view Python cost: 330 ms at 1 thread, 707 ms at 4 threads (contention makes it slower). The BigQuery I/O it hides is ~3 s per view and would parallelize fine.
Verification of the diagnosis
Two process-local memoizations, applied from a module imported at startup, took the same 2,391-view promotion from ~50 min to 3 min 37 s with byte-identical DDL:
normalize_table_namememoized for str inputs (key: name, dialect string, dialect normalization strategy): property render 248 ms → 4.8 ms per view;to_table_mappingmemoized on (deployability index fields, snapshot ids/versions) andDialectbound as a real attribute ofsqlglot.dialects: mapping build 85 ms (2,016 ms at 9 threads) → 0.5 ms, zero locked calls.
Fingerprints unchanged; renders of self-referencing models identical across evaluating/creating × deployable/dev; identical output across 14 dialect settings.
Reproduction
Any project with ~1,000+ models: sqlmesh plan --include-unmodified <new_dev_env> on a warehouse where DDL is fast, then py-spy dump on the process during "Updating virtual layer" (frames: render_virtual_properties → _resolve_tables → _to_table_mapping → table_name → … → sqlglot/dialects/__init__.py __getattr__) or cProfile model.render_virtual_properties(...) with a full table_mapping and versioned snapshots (replace_tables → normalize_table_name → parse_one dominates).
- Langage dominant
- Python
- Étoiles
- 3.3k
- Forks
- 471
- Merge moyen
- 6 j 7 h
- PR mergées (30 j)
- 35
Préparer son environnement
Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de SQLMesh/sqlmesh
-
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
Les mainteneurs répondent en général sous 4 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Les mainteneurs répondent en général sous 4 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 67/100
SQLMesh/sqlmesh#6008 · 1 commentaire ·
Les mainteneurs répondent en général sous 4 jours
-
VSCode Extension: SQLMesh LSP crashes on startup: ImportError: cannot import name 'WorkspaceDiagnosticRefreshRequest' with lsprotocol 2025.xPeut-être pris @boomesh11 l’a pris il y a 15 jours. OuverteBug
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
Les mainteneurs répondent en général sous 4 jours
-
Improvement
Difficulté 1/5 Moins d'une heure Accessibilité débutants 85/100
SQLMesh/sqlmesh#5696 · 6 commentaires ·
Les mainteneurs répondent en général sous 4 jours
Toutes les issues de SQLMesh/sqlmesh
Issues similaires
-
[BUG][Low–Medium] founder_numbers.py silently reads a SAFE discount or pool target given as a fraction (0.2) as 0.2%Peut-être pris @sumitkhond111-del l’a pris aujourd’hui. Ouvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
bmad-code-org/BMAD-METHOD#3358 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 86/100
open-mmlab/mmcv#3352 ·
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 68/100
OpenGithubs/weekly#263 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
Les mainteneurs répondent en général sous 1 jour
-
comp/cli P3 type/feature
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
NousResearch/hermes-agent#162784 ·
Les mainteneurs répondent en général sous 1 jour