1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
query_stat.php
См. документацию.
1<?php
2
4{
5 public static function IsBanned($table, $columns)
6 {
7 global $DB;
8 $rs = $DB->Query("
9 select *
10 from b_perf_index_ban
11 where TABLE_NAME = '" . $DB->ForSql($table) . "'
12 AND COLUMN_NAMES = '" . $DB->ForSql($columns) . "'
13 ");
14 return is_array($rs->Fetch());
15 }
16
17 public static function Ban($table, $columns)
18 {
19 global $DB;
20 $DB->Add('b_perf_index_ban', [
21 'BAN_TYPE' => 'A',
22 'TABLE_NAME' => $table,
23 'COLUMN_NAMES' => $columns,
24 ]);
25 }
26
27 public static function GetTableColumns($table)
28 {
29 global $DB;
30 static $cache = [];
31 $table = trim($table, '`');
32
33 if (!array_key_exists($table, $cache))
34 {
35 $strSql = 'SHOW COLUMNS FROM `' . $DB->ForSql($table) . '`';
36 $rs = $DB->Query($strSql);
37
38 $arResult = [];
39 while ($ar = $rs->Fetch())
40 {
41 $arResult[$ar['Field']] = $ar;
42 }
43
44 $cache[$table] = $arResult;
45 }
46 return $cache[$table];
47 }
48
55 public static function GatherExpressStat($table, $columns, $q)
56 {
57 $arColumns = explode(',', $columns);
58 if (count($arColumns) != 1)
59 {
60 return false;
61 }
62
63 $column = trim($arColumns[0], '`');
64 $value = trim($q->find_value($table, $arColumns[0]), "'");
65
66 if ($value === '')
67 {
68 return false;
69 }
70
71 $tab = new CPerfomanceTable;
72 $tab->Init($table);
73 if ($tab->IsExists())
74 {
75 $arTableColumns = CPerfQueryStat::GetTableColumns($table);
76 if (!array_key_exists($column, $arTableColumns))
77 {
78 return false; //May be it is worth to ban
79 }
80
81 if ($arTableColumns[$column]['Type'] === 'char(1)')
82 {
83 if (is_array(CPerfQueryStat::_get_stat($table, $arColumns[0])))
84 {
85 return true;
86 }
87
88 if (CPerfQueryStat::_gather_stat($table, $arColumns[0], $value, 10 * 1024 * 1024))
89 {
90 return true;
91 }
92 }
93
94 return false;
95 }
96 else
97 {
98 return false;
99 }
100 }
101
102 public static function GatherColumnStatByValue($table, $column, $value)
103 {
104 $tab = new CPerfomanceTable;
105 $tab->Init($table);
106 if ($tab->IsExists())
107 {
108 $arStat = CPerfQueryStat::_get_stat($table, $column, $value);
109 if (!is_array($arStat))
110 {
111 CPerfQueryStat::_gather_stat($table, $column, $value, -1);
112 $arStat = CPerfQueryStat::_get_stat($table, $column, $value);
113 }
114
115 return $arStat;
116 }
117 else
118 {
119 return false;
120 }
121 }
122
123 public static function GatherColumnStatOverall($table, $column)
124 {
125 $tab = new CPerfomanceTable;
126 $tab->Init($table);
127 if ($tab->IsExists())
128 {
129 $arStat = CPerfQueryStat::_get_stat($table, $column, null);
130 if (!is_array($arStat))
131 {
132 CPerfQueryStat::_gather_stat($table, $column, null, -1);
133 $arStat = CPerfQueryStat::_get_stat($table, $column, null);
134 }
135
136 return $arStat;
137 }
138 else
139 {
140 return false;
141 }
142 }
143
144 public static function GatherTableStat($table)
145 {
146 global $DB;
147 $table = trim($table, '`');
148
149 $arStat = CPerfQueryStat::_get_stat($table);
150 if (!$arStat)
151 {
152 $rs = $DB->Query("show table status like '" . $DB->ForSql($table) . "'");
153 $arDBStat = $rs->Fetch();
154 $DB->Add('b_perf_tab_stat', $arStat = [
155 'TABLE_NAME' => $table,
156 'TABLE_SIZE' => $arDBStat['Data_length'],
157 'TABLE_ROWS' => $arDBStat['Rows'],
158 ]);
159 }
160 return $arStat;
161 }
162
163 protected static function _gather_stat($table, $column, $value, $max_size = -1)
164 {
165 global $DB;
166 $table = trim($table, '`');
167 $column = trim($column, '`');
168
169 $arStat = CPerfQueryStat::GatherTableStat($table);
170 if ($max_size < 0 || $arStat['TABLE_SIZE'] < $max_size)
171 {
172 $table = preg_replace('/[^A-Za-z0-9%_]+/i', '', $table);
173 $column = preg_replace('/[^A-Za-z0-9%_]+/i', '', $column);
174
175 if (isset($value))
176 {
177 $rs = $DB->Query('
178 select count(1) CNT
179 from ' . $DB->ForSql($table) . '
180 where `' . $DB->ForSql($column) . "` = '" . $DB->ForSql($value) . "'
181 ");
182 }
183 else
184 {
185 $rs = $DB->Query('
186 select count(distinct `' . $DB->ForSql($column) . '`) CNT
187 from ' . $DB->ForSql($table) . '
188 ');
189 }
190
191 if ($ar = $rs->Fetch())
192 {
193 $DB->Add('b_perf_tab_column_stat', [
194 'TABLE_NAME' => $table,
195 'COLUMN_NAME' => $column,
196 'TABLE_ROWS' => $arStat['TABLE_ROWS'],
197 'COLUMN_ROWS' => $ar['CNT'],
198 'VALUE' => $value ?? false,
199 ]);
200 }
201 return true;
202 }
203 else
204 {
205 return false;
206 }
207 }
208
209 protected static function _get_stat($table, $column = '', $value = '')
210 {
211 global $DB;
212 $table = trim($table, '`');
213 $column = trim($column, '`');
214
215 if ($column === '')
216 {
217 $rs = $DB->Query("
218 select *
219 from b_perf_tab_stat
220 where TABLE_NAME = '" . $DB->ForSql($table) . "'
221 ");
222 }
223 else
224 {
225 if (isset($value))
226 {
227 $where = ($value === '' ? '' : "AND VALUE = '" . $DB->ForSql($value, 100) . "'");
228 }
229 else
230 {
231 $where = 'AND VALUE IS NULL';
232 }
233
234 $rs = $DB->Query("
235 select *
236 from b_perf_tab_column_stat
237 where TABLE_NAME = '" . $DB->ForSql($table) . "'
238 AND COLUMN_NAME = '" . $DB->ForSql($column) . "'
239 " . $where . '
240 ');
241 }
242
243 return $rs->Fetch();
244 }
245
246 public static function IsSelective($table, $columns)
247 {
248 global $DB;
249
250 $arColumns = explode(',', $columns);
251 if (count($arColumns) != 1)
252 {
253 return false;
254 }
255
256 $arColumns = array_map([$DB, 'ForSQL'], $arColumns);
257 $rs = $DB->Query("
258 select max(TABLE_ROWS) TABLE_ROWS, max(COLUMN_ROWS) COLUMN_ROWS
259 from b_perf_tab_column_stat
260 where TABLE_NAME = '" . $DB->ForSql($table) . "'
261 AND COLUMN_NAME in ('" . implode("','", $arColumns) . "')
262 ");
263 $ar = $rs->Fetch();
264 if ($ar && $ar['TABLE_ROWS'] > 0)
265 {
266 return $ar['COLUMN_ROWS'] / $ar['TABLE_ROWS'] > 0.05;
267 }
268 else
269 {
270 return false;
271 }
272 }
273}
$arResult
Определения generate_coupon.php:16
Определения query_stat.php:4
static IsSelective($table, $columns)
Определения query_stat.php:246
static GatherColumnStatByValue($table, $column, $value)
Определения query_stat.php:102
static GatherTableStat($table)
Определения query_stat.php:144
static _gather_stat($table, $column, $value, $max_size=-1)
Определения query_stat.php:163
static IsBanned($table, $columns)
Определения query_stat.php:5
static GatherColumnStatOverall($table, $column)
Определения query_stat.php:123
static GatherExpressStat($table, $columns, $q)
Определения query_stat.php:55
static GetTableColumns($table)
Определения query_stat.php:27
static Ban($table, $columns)
Определения query_stat.php:17
static _get_stat($table, $column='', $value='')
Определения query_stat.php:209
Определения table.php:16
Init($TABLE_NAME, $connection=null)
Определения table.php:21
global $DB
Определения cron_frame.php:29
$ar
Определения options.php:199
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$rs
Определения action.php:82