# NPlusOneDetector

> Detects repeated read queries emitted from one captured call site.

[API reference](/docs/api/index.md) › [Debug Core](/docs/extensions/debug-core/reference/index.md) › [Panel](/docs/extensions/debug-core/reference/panel/index.md) › [Db](/docs/extensions/debug-core/reference/panel/db/index.md) › NPlusOneDetector

| final class | `PHPForge\Debug\Panel\Db\NPlusOneDetector` |
| --- | --- |
| Source | [`src/Panel/Db/NPlusOneDetector.php`](https://github.com/php-forge/debug-core/blob/main/src/Panel/Db/NPlusOneDetector.php#L17) |

Detects repeated read queries emitted from one captured call site.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`bySequence()`](#method-bySequence) static | Indexes findings by every query sequence they cover, so a grid row can look up its own group. | `NPlusOneDetector` |
| [`detect()`](#method-detect) static | Groups repeated `SELECT` statements by captured call site and keeps the groups above the threshold. | `NPlusOneDetector` |

## Method details

### bySequence() <span id="method-bySequence"></span>

```php
public static function bySequence(list<PHPForge\Debug\Panel\Db\NPlusOneFinding> $findings): array<int, PHPForge\Debug\Panel\Db\NPlusOneFinding>
```

Indexes findings by every query sequence they cover, so a grid row can look up its own group.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$findings` | `list<`[`NPlusOneFinding`](/docs/extensions/debug-core/reference/panel/db/nplusonefinding/index.md)`>` | Findings to index. |

**Returns**

`array<int,` [`NPlusOneFinding`](/docs/extensions/debug-core/reference/panel/db/nplusonefinding/index.md)`>`: Findings keyed by covered query sequence.

### detect() <span id="method-detect"></span>

```php
public static function detect(list<PHPForge\Debug\Panel\Db\QueryRow> $rows, int $threshold = 3): list<PHPForge\Debug\Panel\Db\NPlusOneFinding>
```

Groups repeated `SELECT` statements by captured call site and keeps the groups above the threshold.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rows` | `list<`[`QueryRow`](/docs/extensions/debug-core/reference/panel/db/queryrow/index.md)`>` | Captured query rows in capture order. |
| `$threshold` | `int` | Minimum occurrences a call site needs to be reported; must be at least `2`. |

**Returns**

`list<`[`NPlusOneFinding`](/docs/extensions/debug-core/reference/panel/db/nplusonefinding/index.md)`>`: Findings ordered by count, then total duration, then first sequence.

**Throws**

- [`InvalidArgumentException`](https://www.php.net/manual/en/class.invalidargumentexception.php): When the threshold is lower than `2`.