|
1 | 1 | # Using a logical "OR" in the "WHERE" section of a query (LogicalOrInTheWhereSectionOfQuery)
|
2 | 2 |
|
3 | 3 | <!-- Блоки выше заполняются автоматически, не трогать -->
|
4 |
| -## Description |
| 4 | +## Описание диагностики |
5 | 5 | <!-- Описание диагностики заполняется вручную. Необходимо понятным языком описать смысл и схему работу -->
|
6 |
| -Do not use `OR` in the `WHERE` section of the query. This can lead to the DBMS being unable to use the indexes of the tables and will perform scans, which will increase the query time and the likelihood of locks occurring. Instead, you should split one query into several and combine the results. |
| 6 | +Не следует использовать `ИЛИ` в секции `ГДЕ` запроса. Это может привести к тому, что СУБД не сможет использовать |
| 7 | +индексы таблиц и будет выполнять сканирование, что увеличит время работы запроса и вероятность возникновения блокировок. |
| 8 | +Вместо этого следует разбить один запрос на несколько и объединить результаты. |
| 9 | + |
| 10 | +## Примеры |
| 11 | +<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию --> |
| 12 | + |
| 13 | +For example, query: |
7 | 14 |
|
8 |
| -For example, the query |
9 | 15 | ```bsl
|
10 |
| -SELECT Item.Name FROM Directory.Products AS Item |
11 |
| -WHERE Article = "001" OR Price = 10 |
| 16 | +SELECT Goods.Description FROM Catalog.Goods AS Goods |
| 17 | +WHERE Code = "001" OR Cost = 10 |
12 | 18 | ```
|
13 | 19 |
|
14 |
| -should be replaced with a query |
| 20 | +should instead of a query: |
15 | 21 |
|
16 | 22 | ```bsl
|
17 |
| -SELECT Product.Name FROM Directory.Products AS Product WHERE Article = "001" |
| 23 | +SELECT Goods.Description FROM Catalog.Goods AS Goods |
| 24 | +WHERE Code = "001" |
| 25 | +
|
18 | 26 | UNION ALL
|
19 |
| -SELECT Product.Name FROM Directory.Products AS Product WHERE Price = 10 |
| 27 | +
|
| 28 | +SELECT Goods.Description FROM Catalog.Goods AS Goods |
| 29 | +WHERE Cost = 10 |
| 30 | +
|
20 | 31 | ```
|
21 |
| -> **Important** - the current diagnostic implementation triggers any `OR` in the `WHERE` section and may give false positives for some conditions. |
22 | 32 |
|
23 |
| -1) In the main condition, the `OR` operator can be used only for the last used or the only index field, when the `OR` operator can be replaced with the `IN` operator. |
| 33 | +>**Important** - the current implementation of the diagnostic triggers on any `OR` in the `WHERE` section and may issue false positives for some conditions. |
24 | 34 |
|
25 |
| -CORRECT: |
| 35 | +1) In the main condition, the `OR` operator can only be used for the last used or the only index field, when the `OR` operator can be replaced by the `IN` operator. |
| 36 | + |
| 37 | +Correct: |
26 | 38 |
|
27 | 39 | ```bsl
|
28 | 40 | WHERE
|
29 |
| - Table.Field = &Value1 |
30 |
| - OR Table.Field = &Value2 |
| 41 | + Table.Filed = &Value1 |
| 42 | + OR Table.Filed = &Value2 |
31 | 43 | ```
|
32 | 44 |
|
33 |
| -since can be rewritten using the `IN` operator (you don't need to rewrite it specifically, you can leave it as it is): |
| 45 | +because can be rewritten using the `IN` operator (you don’t need to specifically rewrite it, you can leave it as it is): |
34 | 46 |
|
35 | 47 | ```bsl
|
36 | 48 | WHERE
|
37 | 49 | Table.Field IN (&Value)
|
38 | 50 | ```
|
39 | 51 |
|
40 |
| -WRONG: |
| 52 | +Incorrect: |
41 | 53 |
|
42 | 54 | ```bsl
|
43 | 55 | WHERE
|
44 |
| - Table.Field1 = &Value1 |
45 |
| - OR Table.Field2 = &Value2 |
| 56 | + Table.Field1 = &Value1 |
| 57 | + OR Table.Field2 = &Value2 |
46 | 58 | ```
|
47 | 59 |
|
48 |
| -cannot be overwritten with `IN`, but can be overwritten with `UNION ALL` (each field Field1 and Field2 must be indexed): |
| 60 | +cannot be rewritten with `IN`, but can be rewritten with `UNION ALL` (each Field1 and Field2 must be indexed): |
49 | 61 |
|
50 | 62 | ```bsl
|
51 | 63 | WHERE
|
52 |
| - Table.Field1 = &Value1 |
| 64 | + Table.Field1 = &Value1 |
53 | 65 |
|
54 |
| -ОБЪЕДИНИТЬ ВСЕ |
| 66 | +UNION ALL |
55 | 67 |
|
56 | 68 | WHERE
|
57 |
| - Table.Field2 = &Value1 |
| 69 | + Table.Field2 = &Value1 |
58 | 70 | ```
|
59 |
| -> Note: replacing `OR` with `UNION ALL` is not always possible, make sure the result is indeed the same as `OR` before use. |
60 | 71 |
|
61 |
| -2) In an additional condition, the OR operator can be used without restrictions. |
| 72 | +>Note: it is not always possible to replace `OR` with `UNION ALL`, make sure the result is really the same as with `OR` before applying. |
62 | 73 |
|
63 |
| -CORRECT 1: |
| 74 | +2) Additionally, the 'OR' operator can be used without restriction. |
| 75 | + |
| 76 | +Correct: |
64 | 77 |
|
65 | 78 | ```bsl
|
66 | 79 | WHERE
|
67 |
| - Table.Field1 = &Value1 // Main condition (uses index) |
68 |
| - AND // Additional condition (you can use OR) |
69 |
| - (Table.Field2 = &Value2 OR Table.Field3 = &Value3) |
| 80 | + Table.Filed1 = &Value1 // Main condition (use index) |
| 81 | + AND // Addition condition (can use OR) |
| 82 | + (Table.Filed2 = &Value2 OR Table.Filed3 = &Value3) |
70 | 83 | ```
|
71 | 84 |
|
72 |
| -CORRECT 2: |
| 85 | +Correct: |
73 | 86 |
|
74 | 87 | ```bsl
|
75 | 88 | WHERE
|
76 |
| - (Table.Field1 = &Value1 OR Table.Field1 = &Value2) |
77 |
| - AND |
78 |
| - (Table.Field2 = &Value3 OR Table.Field2 = &Value4) |
| 89 | + (Table.Filed1 = &Value1 OR Table.Filed1 = &Value2) |
| 90 | + AND |
| 91 | + (Table.Filed2 = &Value3 OR Table.Filed2 = &Value4) |
79 | 92 | ```
|
80 | 93 |
|
81 |
| -since can be rewritten using the IN operator (you don't need to rewrite it specifically, you can leave it as it is): |
| 94 | +because can be rewritten using 'IN' (no special rewriting needed, can be left as is): |
82 | 95 |
|
83 | 96 | ```bsl
|
84 | 97 | WHERE
|
85 |
| - Table.Field1 B (&Values1) // Main condition |
86 |
| - AND Table.Field2 B (&Values2) // Additional condition (or vice versa) |
| 98 | + Table.Field1 IN (&Value1) // Main condition |
| 99 | + AND Table.Field2 IN (&Value2) // Additional condition (or vice versa) |
87 | 100 | ```
|
88 | 101 |
|
89 |
| -## Examples |
90 |
| -<!-- В данном разделе приводятся примеры, на которые диагностика срабатывает, а также можно привести пример, как можно исправить ситуацию --> |
91 |
| - |
92 | 102 | ## Sources
|
93 | 103 | <!-- Необходимо указывать ссылки на все источники, из которых почерпнута информация для создания диагностики -->
|
94 | 104 |
|
|
0 commit comments