Queries
The query language — parameters, grouping, joins, unions, NULL, virtual and temporary tables, dot access and performance.
19 questions
JuniorTheoryVery commonHow does a left join differ from an inner join in a 1C query?
How does a left join differ from an inner join in a 1C query?
An inner join returns only rows with a match in both tables. A left join returns every row of the left table, and where the right one has no match its fields are NULL. So goods without sales stay in a left join and drop out of an inner one.
Common mistakes
- ✗Expecting a left join to fill missing fields with zeros: they are
NULLuntilЕСТЬNULLreplaces them - ✗Putting a condition on the right table into
ГДЕafter a left join: rows withNULLdrop out, as in an inner join
Follow-up questions
- →Where should a condition on the right table go so that a left join keeps its rows?
- →When does a left join return more rows than the left table has?
MiddleTheoryVery commonWhy can getting a field through a dot in a 1C query be expensive?
Why can getting a field through a dot in a 1C query be expensive?
Each dot on a reference field, such as Продажа.Товар.Группа, adds an implicit left join to the referenced table. For a composite-type field the platform joins every table in the type, so a long chain or a composite field slows the query down.
Common mistakes
- ✗Chaining dots through composite-type fields: the platform joins every table in the type
- ✗Thinking a dot in a query reads objects the way code does: it becomes a join, not a read per row
Follow-up questions
- →How does
ВЫРАЗИТЬreduce the joins made for a composite-type field? - →When is an explicit join better than a field taken through a dot?
MiddleTheoryVery commonWhere does NULL come from in a 1C query, and how do you handle it?
Where does NULL come from in a 1C query, and how do you handle it?
NULL appears in a left join's fields where the right table has no match, in a dot on an empty or other-type reference and in an aggregate over no rows. Test it with ЕСТЬ NULL and replace it with ЕСТЬNULL(Поле, 0); = NULL is never true.
Common mistakes
- ✗Comparing with
= NULLinГДЕ: such a comparison is never true, so useЕСТЬ NULL - ✗Adding a
NULLfrom a left join to a number: the sum becomesNULL, so wrap the field inЕСТЬNULL
Follow-up questions
- →Why does a balance from a left join come out empty for some goods?
- →How does
ЕСТЬNULLdiffer fromВЫБОР КОГДА … ЕСТЬ NULL?
MiddleTheoryVery commonHow does a temporary table differ from a virtual table in 1C queries?
How does a temporary table differ from a virtual table in 1C queries?
A virtual table, such as Остатки or СрезПоследних, is computed by the platform from a register for given parameters each time it is queried. A temporary table is a result that a batch query saved with ПОМЕСТИТЬ so later queries can read it.
Common mistakes
- ✗Reading the same virtual table several times in one batch: put it into a temporary table once
- ✗Expecting a temporary table to outlive its batch without a manager: it disappears after the batch runs
Follow-up questions
- →Why index a temporary table with
ИНДЕКСИРОВАТЬ ПО? - →Why do a virtual table's parameters filter better than
ГДЕ?
JuniorTheoryCommonWhat does Запрос.Выполнить() return for this text?
What does Запрос.Выполнить() return for this text?
One row with the number 5: the result of the batch's second query
Open full question →Common mistakes
- ✗Expecting
Выполнить()to return the results of every query in the batch: it returns the last one, andВыполнитьПакет()returns all - ✗Expecting a temporary table to be unreadable in the same batch: the later queries of the batch see it
Follow-up questions
- →How do you read the result of the first query of the batch?
- →Why use a
МенеджерВременныхТаблицwhen a batch already sees its temporary tables?
JuniorTheoryCommonWhich goods does the query return, in order?
Which goods does the query return, in order?
ручка, клей
Open full question →Common mistakes
- ✗Applying
ПЕРВЫЕbeforeРАЗЛИЧНЫЕ: repeats go first, and the two rows are taken from distinct ones - ✗Expecting the repeated row «ручка 300» to stay in the result:
РАЗЛИЧНЫЕremoves identical rows
Follow-up questions
- →How does the result change without
РАЗЛИЧНЫЕ? - →Why does
ПЕРВЫЕwithoutУПОРЯДОЧИТЬ ПОgive an unpredictable set of rows?
JuniorTheoryCommonHow does a join of tables in a query differ from a union?
How does a join of tables in a query differ from a union?
A join adds another table's fields to a row; a union adds another query's rows
Common mistakes
- ✗Confusing a join with a union: a join adds fields at the side, a union adds rows at the bottom
- ✗Expecting a join to remove repeated rows: one row on the left can give several result rows
Follow-up questions
- →When does a report of receipts and sales need a union rather than a join?
- →How many fields must each query of a union have?
JuniorTheoryCommonHow many rows does the query return?
How many rows does the query return?
4
Open full question →Common mistakes
- ✗Expecting a left join to return exactly one row per left row: every match on the right gives a row of its own
- ✗Expecting rows without a match to vanish: a left join keeps them, with
NULLin the right table's fields
Follow-up questions
- →How many rows does the same query return with an inner join?
- →How do you get one row per ordered item with the total received?
JuniorTheoryCommonHow many rows does the query return?
How many rows does the query return?
0
Open full question →Common mistakes
- ✗Expecting
ЕСТЬNULLin the field list to replaceNULLinГДЕas well: the condition checks the original field - ✗Expecting
NULL = 0to be true: a comparison withNULLis never true, such rows are found withЕСТЬ NULL
Follow-up questions
- →How do you rewrite the condition so the query returns the goods with no receipts?
- →How does
NULLin a query differ fromНеопределеноin the built-in language?
JuniorTheoryCommonWhy is a warehouse filter put in the Остатки table's parameters rather than in ГДЕ?
Why is a warehouse filter put in the Остатки table's parameters rather than in ГДЕ?
In the parameters the filter acts before the calculation, so other warehouses are not computed
Common mistakes
- ✗Putting a filter on a dimension into
ГДЕafter a virtual table: the result is the same, but the platform first calculates every warehouse - ✗Expecting
ГДЕafterОстаткиto miss the warehouse filter: the rows are the same, only the amount of calculation differs
Follow-up questions
- →When do a condition in the parameters and the same condition in
ГДЕgive different rows? - →Which parameter besides the condition does the
Остаткиvirtual table take?
JuniorDebuggingCommonWhat in this code breaks the 1C standard and slows it down on a large document?
What in this code breaks the 1C standard and slows it down on a large document?
The query runs for every row; one query with Товар В (&Товары) is needed
Common mistakes
- ✗Moving only
Новый Запросout of the loop: a trip to the infobase per row stays, and that is what is slow - ✗Running a query for every row of a table section: standard #436 asks to collect the keys and run one query
Follow-up questions
- →How do you find the balance for each row after the single query without querying again?
- →When does the 1C standard allow a query inside a loop?
JuniorTheoryCommonWhat does the query return if the Дата parameter is 15.11?
What does the query return if the Дата parameter is 15.11?
ручка 15
Open full question →Common mistakes
- ✗Expecting the condition in the slice parameters to act after the slice, like
ГДЕ: it filters the records before the slice - ✗Expecting the last-values slice to return every matching record: it returns one per key, the latest
Follow-up questions
- →What does the query return if the
Цена > 0condition moves toГДЕ? - →Why do the 1C standards advise putting only dimension conditions in the slice parameters?
JuniorTheoryCommonHow many rows does the query return?
How many rows does the query return?
3
Open full question →Common mistakes
- ✗Expecting
ОБЪЕДИНИТЬto keep every row: that isОБЪЕДИНИТЬ ВСЕ, and withoutВСЕrepeats are removed - ✗Expecting repeats to be removed only between the two queries:
ОБЪЕДИНИТЬremoves them across the whole result
Follow-up questions
- →Why do the 1C standards prefer
ОБЪЕДИНИТЬ ВСЕwhen repeats cannot occur? - →Where does a union take the names of its result fields from?
JuniorTheoryCommonHow many rows does the query return?
How many rows does the query return?
1
Open full question →Common mistakes
- ✗Reading
>=as a strict comparison: the boundary is included, so ручка with the price 15 passes - ✗Reading
ИasИЛИ: a row passes only when both conditions hold
Follow-up questions
- →How many rows does the query return if
<becomes<=? - →How would you write this range with
МЕЖДУ, and which bounds does it include?
JuniorTheoryCommonHow many rows does the query return?
How many rows does the query return?
2
Open full question →Common mistakes
- ✗Expecting
ГДЕto filter the grouped sums: it checks the source rows before grouping - ✗Checking each row on its own in
ИМЕЮЩИЕ: its condition is computed on the sum of the group
Follow-up questions
- →How many rows does the query return without the
ГДЕclause? - →Why is a condition on a field without an aggregate better placed in
ГДЕthan inИМЕЮЩИЕ?
MiddleTheoryCommonWhat do the virtual tables СрезПоследних and СрезПервых return?
What do the virtual tables СрезПоследних and СрезПервых return?
For each set of dimensions of a periodic information register, СрезПоследних returns the latest record on or before the given date, and СрезПервых the earliest on or after it. Without a date, the slice of last ones gives the latest values.
Common mistakes
- ✗Filtering a slice by date in
ГДЕ: the date is a parameter of the virtual table - ✗Expecting a record dated exactly on the given date to be left out: the slice of last ones includes it
Follow-up questions
- →Why does a condition on a resource in the slice's parameters give a different result than in
ГДЕ? - →How do you get a price slice on each document's own date in one query?
MiddleTheoryCommonWhat does the function ВЫРАЗИТЬ do in a 1C query, and when is it needed?
What does the function ВЫРАЗИТЬ do in a 1C query, and when is it needed?
ВЫРАЗИТЬ casts a value to one type. On a composite field, ВЫРАЗИТЬ(Регистратор КАК Документ.Продажа).Склад joins only that document's table, not all of them. It also cuts an unlimited string to a fixed length to compare or group by it.
Common mistakes
- ✗Reading a field of a composite-type reference without
ВЫРАЗИТЬ: the platform joins every table of the type - ✗Grouping by an unlimited string: it must first be cut to a fixed length with
ВЫРАЗИТЬ
Follow-up questions
- →What does
ВЫРАЗИТЬreturn for a row whose value has a different type? - →How does the operator
ССЫЛКАin a condition differ fromВЫРАЗИТЬ?
SeniorTheoryCommonWhich common causes of a slow 1C query do you know, and how do you fix them?
Which common causes of a slow 1C query do you know, and how do you fix them?
A query in a loop: replace it with one query over all rows. Filters in ГДЕ instead of a virtual table's parameters. Long dot chains and composite fields: use ВЫРАЗИТЬ or explicit joins. Joins with subqueries: use indexed temporary tables.
Common mistakes
- ✗Blaming the hardware before reading the query: most slow queries come from the text
- ✗Joining a virtual table and leaving its filter in
ГДЕ: the platform computes the table for all data first
Follow-up questions
- →How do you find which query is slow in a running infobase?
- →Why does joining with a virtual table often slow a query down?
SeniorTheoryCommonHow long do temporary tables live within a batch query and after it?
How long do temporary tables live within a batch query and after it?
Without a manager, a temporary table lives while its batch runs, and later queries of the batch see it. With a МенеджерВременныхТаблиц, tables live until the manager is closed or released, for every query using it. УНИЧТОЖИТЬ drops one early.
Common mistakes
- ✗Expecting a temporary table to reach another query without a manager: it is gone when the batch ends
- ✗Keeping a manager open through a long procedure: its tables hold resources until it is closed
Follow-up questions
- →Why index a temporary table that later queries join with?
- →What does
ВыполнитьПакет()return, compared withВыполнить()?