|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace staabm\PHPStanDba\Analyzer; |
| 6 | + |
| 7 | +use mysqli; |
| 8 | +use PDO; |
| 9 | +use PHPStan\ShouldNotHappenException; |
| 10 | +use staabm\PHPStanDba\QueryReflection\QueryReflection; |
| 11 | + |
| 12 | +final class QueryPlanAnalyzerMysql |
| 13 | +{ |
| 14 | + /** |
| 15 | + * number of unindexed reads allowed before a query is considered inefficient. |
| 16 | + */ |
| 17 | + public const DEFAULT_UNINDEXED_READS_THRESHOLD = 100000; |
| 18 | + /** |
| 19 | + * max number of rows in a table, for which we don't report errors, because using a index/table-scan wouldn't improve performance. |
| 20 | + */ |
| 21 | + public const DEFAULT_SMALL_TABLE_THRESHOLD = 1000; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var PDO|mysqli |
| 25 | + */ |
| 26 | + private $connection; |
| 27 | + |
| 28 | + /** |
| 29 | + * @param PDO|mysqli $connection |
| 30 | + */ |
| 31 | + public function __construct($connection) |
| 32 | + { |
| 33 | + $this->connection = $connection; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param non-empty-string $query |
| 38 | + */ |
| 39 | + public function analyze(string $query): QueryPlanResult |
| 40 | + { |
| 41 | + if ($this->connection instanceof PDO) { |
| 42 | + $stmt = $this->connection->query('EXPLAIN '.$query); |
| 43 | + |
| 44 | + // @phpstan-ignore-next-line |
| 45 | + return $this->buildResult($stmt); |
| 46 | + } else { |
| 47 | + $result = $this->connection->query('EXPLAIN '.$query); |
| 48 | + if ($result instanceof \mysqli_result) { |
| 49 | + return $this->buildResult($result); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + throw new ShouldNotHappenException(); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @param \IteratorAggregate<array-key, array{select_type: string, key: string|null, type: string|null, rows: positive-int, table: ?string}> $it |
| 58 | + */ |
| 59 | + private function buildResult($it): QueryPlanResult |
| 60 | + { |
| 61 | + $result = new QueryPlanResult(); |
| 62 | + |
| 63 | + $allowedUnindexedReads = QueryReflection::getRuntimeConfiguration()->getNumberOfAllowedUnindexedReads(); |
| 64 | + if (false === $allowedUnindexedReads) { |
| 65 | + throw new ShouldNotHappenException(); |
| 66 | + } |
| 67 | + |
| 68 | + $allowedRowsNotRequiringIndex = QueryReflection::getRuntimeConfiguration()->getNumberOfRowsNotRequiringIndex(); |
| 69 | + if (false === $allowedRowsNotRequiringIndex) { |
| 70 | + throw new ShouldNotHappenException(); |
| 71 | + } |
| 72 | + |
| 73 | + foreach ($it as $row) { |
| 74 | + // we cannot analyse tables without rows -> mysql will just return 'no matching row in const table' |
| 75 | + if (null === $row['table']) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + if (null === $row['key'] && $row['rows'] > $allowedRowsNotRequiringIndex) { |
| 80 | + // derived table aka. a expression that generates a table within the scope of a query FROM clause |
| 81 | + // is a temporary table, which indexes cannot be created for. |
| 82 | + if ('derived' === strtolower($row['select_type'])) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $result->addRow($row['table'], QueryPlanResult::NO_INDEX); |
| 87 | + } else { |
| 88 | + if (null !== $row['type'] && 'all' === strtolower($row['type']) && $row['rows'] > $allowedRowsNotRequiringIndex) { |
| 89 | + $result->addRow($row['table'], QueryPlanResult::TABLE_SCAN); |
| 90 | + } elseif (true === $allowedUnindexedReads && $row['rows'] > self::DEFAULT_UNINDEXED_READS_THRESHOLD) { |
| 91 | + $result->addRow($row['table'], QueryPlanResult::UNINDEXED_READS); |
| 92 | + } elseif (\is_int($allowedUnindexedReads) && $row['rows'] > $allowedUnindexedReads) { |
| 93 | + $result->addRow($row['table'], QueryPlanResult::UNINDEXED_READS); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return $result; |
| 99 | + } |
| 100 | +} |
0 commit comments