
Blog · 📚 1С: практика разработчика
1C in practice: how to speed up queries — 10 rules and how to find a slow query
🌐 Machine-translated from Russian. Show original
In this part:
First, find what is holding you back#
Do not optimize blindly. Tools:
- Performance measurement (Configurator → Debugging → Performance measurement) — which lines of code consume time.
- Technological log with
DBPOSTGRS/DBMSSQLevents and duration threshold — which DBMS requests are long. - Query plan in DBMS (EXPLAIN in PostgreSQL, execution plan in MS SQL) — why the query is slow.
10 rules for quick queries#
- Do not run a query in a loop. Collect all keys and get data with a single query.
- Do not access details through a dot in the code (
Документ.Контрагент.ИНН) in the loop — each request can be a separate query. Get the required fields in the query. - Use virtual table parameters: the selection condition is in the
Остатки(&Дата, Номенклатура В (&Список))parameters, notГДЕafter the table. - Temporary tables with an index instead of subqueries in joins:
ПОМЕСТИТЬ ВТТовары ... ИНДЕКСИРОВАТЬ ПО Номенклатура. - Do not connect to subqueries — the DBMS optimizer often builds a poor plan for such designs.
- Avoid OR in connection conditions and in WHERE by different fields — break it down into COMBINE ALL.
- Conditions for indexed fields: selection by attribute that is not indexed — full table view. Add an index to the attribute if the selection is frequent.
- EXPRESS for composite types: accessing a composite type field without EXPRESSING generates joins with all possible tables.
- Select only the required fields and limit sampling (
ПЕРВЫЕ N) where possible. - ALLOWED and RLS: record-level access restrictions complicate queries. Test performance under the user with restrictions, not just under the administrator.
Example: a query in a loop → a single query#
Was — query for each line:
Для Каждого Строка Из Товары Цикл
Запрос = Новый Запрос("ВЫБРАТЬ Цена ИЗ РегистрСведений.Цены.СрезПоследних(, Номенклатура = &Н)");
Запрос.УстановитьПараметр("Н", Строка.Номенклатура);
// ...
КонецЦикла;
It became — one query for all lines:
Запрос = Новый Запрос(
"ВЫБРАТЬ
| Цены.Номенклатура,
| Цены.Цена
|ИЗ
| РегистрСведений.Цены.СрезПоследних(, Номенклатура В (&Список)) КАК Цены");
Запрос.УстановитьПараметр("Список", Товары.ВыгрузитьКолонку("Номенклатура"));
Цены = Запрос.Выполнить().Выгрузить();
Цены.Индексы.Добавить("Номенклатура");
When it's not about the query#
- Locks: long transactions hold locks, others wait. Look for events
TLOCK,TTIMEOUT,TDEADLOCKin the TJ. - DBMS statistics are outdated — the query plan is becoming poor. Regular statistics updates are part of the regulations.
- Server disk and memory — see the section about maintenance in the "1C Server" series.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.