Skip to content

Commit bec52f4

Browse files
staabmclxmstaab
andauthored
docs (#387)
* docs * allow numberOfRowsNotRequiringIndex=0 * cs Co-authored-by: Markus Staab <[email protected]>
1 parent cb766c5 commit bec52f4

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

docs/query-plan-analysis.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ When enabled, `phpstan-dba` will error when queries are not using indices or que
55

66
The analyzer is reporting problems related to queries not using index, full-table-scans and too many unindexed reads.
77

8+
## Signature
9+
10+
`analyzeQueryPlans($numberOfAllowedUnindexedReads = true, $numberOfRowsNotRequiringIndex = QueryPlanAnalyzerMysql::DEFAULT_SMALL_TABLE_THRESHOLD)`
11+
12+
## Examples
13+
814
Passing `true` will enable the feature:
915

1016
```php
@@ -27,6 +33,14 @@ $config = new RuntimeConfiguration();
2733
$config->analyzeQueryPlans(0);
2834
```
2935

36+
When running in environments in which only the database schema, but no data is available pass `$numberOfRowsNotRequiringIndex=0`.
37+
38+
```php
39+
$config = new RuntimeConfiguration();
40+
$config->analyzeQueryPlans(true, 0);
41+
```
42+
43+
3044
**Note:** For a meaningful performance analysis it is vital to utilize a database, which containts data and schema as similar as possible to the production database.
3145

3246
**Note:** "Query Plan Analysis" requires an active database connection.

src/Analyzer/QueryPlanAnalyzerMysql.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function buildResult($it): QueryPlanResult
7676
continue;
7777
}
7878

79-
if (null === $row['key'] && $row['rows'] > $allowedRowsNotRequiringIndex) {
79+
if (null === $row['key'] && $row['rows'] >= $allowedRowsNotRequiringIndex) {
8080
// derived table aka. a expression that generates a table within the scope of a query FROM clause
8181
// is a temporary table, which indexes cannot be created for.
8282
if ('derived' === strtolower($row['select_type'])) {
@@ -85,11 +85,11 @@ private function buildResult($it): QueryPlanResult
8585

8686
$result->addRow($row['table'], QueryPlanResult::NO_INDEX);
8787
} else {
88-
if (null !== $row['type'] && 'all' === strtolower($row['type']) && $row['rows'] > $allowedRowsNotRequiringIndex) {
88+
if (null !== $row['type'] && 'all' === strtolower($row['type']) && $row['rows'] >= $allowedRowsNotRequiringIndex) {
8989
$result->addRow($row['table'], QueryPlanResult::TABLE_SCAN);
90-
} elseif (true === $allowedUnindexedReads && $row['rows'] > self::DEFAULT_UNINDEXED_READS_THRESHOLD) {
90+
} elseif (true === $allowedUnindexedReads && $row['rows'] >= self::DEFAULT_UNINDEXED_READS_THRESHOLD) {
9191
$result->addRow($row['table'], QueryPlanResult::UNINDEXED_READS);
92-
} elseif (\is_int($allowedUnindexedReads) && $row['rows'] > $allowedUnindexedReads) {
92+
} elseif (\is_int($allowedUnindexedReads) && $row['rows'] >= $allowedUnindexedReads) {
9393
$result->addRow($row['table'], QueryPlanResult::UNINDEXED_READS);
9494
}
9595
}

src/QueryReflection/RuntimeConfiguration.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class RuntimeConfiguration
4545
*/
4646
private $numberOfAllowedUnindexedReads = false;
4747
/**
48-
* @var false|positive-int
48+
* @var false|0|positive-int
4949
*/
5050
private $numberOfRowsNotRequiringIndex = false;
5151

@@ -112,7 +112,7 @@ public function stringifyTypes(bool $stringify): self
112112
* @param bool|0|positive-int $numberOfAllowedUnindexedReads `true` to enable analysis with QueryPlanAnalyzerMysql::DEFAULT_UNINDEXED_READS_THRESHOLD. `false` to disable analysis.
113113
* Otherwise the number of reads a query is allowed to execute, before it is considered inefficient.
114114
* `0` disables the efficiency checks but still scans for queries not using an index.
115-
* @param positive-int $numberOfRowsNotRequiringIndex number of reads a query is allowed to execute, without requiring a index
115+
* @param 0|positive-int $numberOfRowsNotRequiringIndex number of reads a query is allowed to execute, without requiring a index
116116
*
117117
* @return $this
118118
*/
@@ -133,7 +133,7 @@ public function getNumberOfAllowedUnindexedReads()
133133
}
134134

135135
/**
136-
* @return false|positive-int
136+
* @return false|0|positive-int
137137
*/
138138
public function getNumberOfRowsNotRequiringIndex()
139139
{

0 commit comments

Comments
 (0)