1---2name: query-optimize3description: Оптимизация запросов 1С и СКД. Используй когда нужно написать, проверить или ускорить запрос, СКД query, временные таблицы, виртуальные таблицы, отборы, соединения или проблемный SQL/DBMS trace.4---56# Query Optimize78## MCP routing910- Preferred path: use MCP `unica` tools `unica.search`, `unica.view`, `unica.check`, `unica.view` on the schema node, `unica.view` on the object node, `unica.docs`, and `unica.run`.11- Runtime идёт через `unica.run`: вызов без `op` отдаёт словарь операций и12контракт каждой — `argsSchema`, `execution`, `previewRequired`,13`ifRevRequiredOnApply`. Контракт вызова бери оттуда, а не из этого текста;14выбирай только операцию с `implemented: true` и не выдумывай аргументов15записи с `argsSchema: null`; превью исполнением не является. Не обходи16контракт прямым runner-ом.17- Use `unica.view {}` if the source-set or format is unclear.18- Do not call internal analyzer, standards, runtime, or package adapters directly. They are hidden behind MCP `unica`.1920## Workflow21221. Extract the exact query text with `unica.search` or `unica.view` on the schema node.232. Inspect the execution context with `unica.view` on the module node (its `Method` branch lists the methods): module, exported entry point, region, temporary table chain, and caller loop.243. Find callers with `unica.search` by the method name when the query is inside reusable API, background jobs, event handlers, or suspected query-in-loop flow; a call graph is not on the v0.13 surface.254. Run `unica.check {at}` on the containing module when analyzer diagnostics can reveal unreachable code, unresolved calls, or type issues around the query. Do not pass a DCS `TemplatePath` as a diagnostic target; locate the BSL module that executes the query.265. Inspect `unica.view` on the object node for both related modules, subscriptions, roles, functional options and the local registers, dimensions, resources, реквизиты, tabular sections, and indexes implied by the platform object type.276. Inspect DCS with `unica.view` on the schema node when the query lives in a data composition schema.287. Search `unica.docs` with `source: "development-standard"` only for `development-standard` query rules. Exact platform query semantics require `unica.docs` with `source: "platform-help"` before a platform-dependent rewrite.298. Read `../../references/platform/db-performance.md` when performance depends on DBMS behavior, locks, indexes, temp storage, WAL, TEMPDB, or large table statistics.309. Optimize one cause at a time: filters before joins, virtual table parameters, temporary table materialization, repeated queries in loops, dot dereference expansion, unbounded selections, and unnecessary totals.3110. Check syntax with `unica.check`; require real trace/log evidence when performance depends on data volume.3233## DB-aware diagnostics3435- Keep platform query text, generated SQL/DBMS evidence, table sizes, index usage, locks, deadlocks, and transaction boundaries together.36- Treat PostgreSQL, MS SQL Server, and file mode as different evidence models. Do not generalize a СУБД-specific conclusion without naming it.37- Do not recommend a new index without tying it to a predicate, join, sort, grouping, and write-cost tradeoff.38- For virtual tables, prefer precise parameters over broad reads followed by post-filtering.39- For блокировки, connect lock holder, waiter, transaction, module path, and user/API scenario before proposing a rewrite.4041## Review checklist4243- Virtual tables receive parameters instead of broad post-filtering.44- Temporary tables have the minimal fields needed by later stages.45- Repeated subqueries and query-in-loop patterns are removed or justified.46- Joins do not multiply rows silently; totals and grouping match business meaning.47- Date and organization filters are applied as early as the platform query allows.48- Query changes preserve rights semantics and do not replace `РАЗРЕШЕННЫЕ` blindly.4950## MCP examples5152```jsonc53{54 "jsonrpc": "2.0",55 "method": "tools/call",56 "params": {57 "name": "unica.view",58 "arguments": {59 "cwd": "<workspace>",60 "at": "main:Report.Продажи.Template.ОсновнаяСхемаКомпоновкиДанных.DataSet"61 }62 }63}64```6566```jsonc67{68 "jsonrpc": "2.0",69 "method": "tools/call",70 "params": {71 "name": "unica.docs",72 "arguments": {73 "query": "оптимизация запросов 1С виртуальные таблицы",74 "source": "development-standard"75 }76 }77}78```