-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathQueryPlanResult.php
More file actions
115 lines (96 loc) · 3.05 KB
/
Copy pathQueryPlanResult.php
File metadata and controls
115 lines (96 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
declare(strict_types=1);
namespace staabm\PHPStanDba\Analyzer;
final class QueryPlanResult
{
public const ROW_NO_INDEX = 'no-index';
public const ROW_TABLE_SCAN = 'table-scan';
public const ROW_UNINDEXED_READS = 'unindexed-reads';
/**
* @see https://www.postgresql.org/docs/current/sql-explain.html
* @see https://www.postgresql.org/docs/current/using-explain.html
*/
public const ROW_AGGREGATE = 'aggregate';
public const ROW_BITMAP_HEAP_SCAN = 'bitmap-heap-scan';
public const ROW_BITMAP_INDEX_SCAN = 'bitmap-index-scan';
public const ROW_HASH_AGGREGATE = 'hash-aggregate';
public const ROW_INDEX_SCAN = 'index-scan';
public const ROW_SEQ_SCAN = 'seq-scan';
private const MYSQL_TIPS = [
self::ROW_NO_INDEX => 'see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html',
self::ROW_TABLE_SCAN => 'see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/table-scan-avoidance.html',
self::ROW_UNINDEXED_READS => 'see Mysql Docs https://dev.mysql.com/doc/refman/8.0/en/select-optimization.html',
];
public const PGSQL_TIP = 'see PostgreSQL Docs https://www.postgresql.org/docs/8.1/performance-tips.html';
/**
* @var array<string, self::ROW_*>
*/
private $result = [];
/**
* @var string
*/
private $simulatedQuery;
public function __construct(string $simulatedQuery)
{
$this->simulatedQuery = $simulatedQuery;
}
public function getSimulatedQuery(): string
{
return $this->simulatedQuery;
}
/**
* @param self::ROW_* $result
*
* @return void
*/
public function addRow(string $table, string $result)
{
$this->result[$table] = $result;
}
/**
* @return string[]
*/
public function getTablesNotUsingIndex(): array
{
$tables = [];
foreach ($this->result as $table => $result) {
if (self::ROW_NO_INDEX === $result || self::ROW_INDEX_SCAN !== $result) {
$tables[] = $table;
}
}
return $tables;
}
/**
* @return string[]
*/
public function getTablesDoingTableScan(): array
{
$tables = [];
foreach ($this->result as $table => $result) {
if (self::ROW_TABLE_SCAN === $result || self::ROW_SEQ_SCAN === $result) {
$tables[] = $table;
}
}
return $tables;
}
/**
* @return string[]
*/
public function getTablesDoingUnindexedReads(): array
{
$tables = [];
foreach ($this->result as $table => $result) {
if (self::ROW_UNINDEXED_READS === $result || self::ROW_INDEX_SCAN !== $result) {
$tables[] = $table;
}
}
return $tables;
}
public function getTipForTable(string $table): string
{
$result = $this->result[$table];
return \in_array($result, array_keys(self::MYSQL_TIPS), true)
? self::MYSQL_TIPS[$this->result[$table]]
: self::PGSQL_TIP;
}
}