MediaWiki master
TableStatsProvider.php
Go to the documentation of this file.
1<?php
2
4
6
13 private ?int $minId;
14 private ?int $maxId;
15 private bool $loaded = false;
16
17 public function __construct(
18 private IConnectionProvider $connectionProvider,
19 private string $tableName,
20 private string $idField,
21 ) {
22 }
23
30 public function getIdDelta() {
31 $this->loadStats();
32 return $this->maxId === null ? 0 : $this->maxId - $this->minId;
33 }
34
40 public function getMinId() {
41 $this->loadStats();
42 return $this->minId;
43 }
44
50 public function getMaxId() {
51 $this->loadStats();
52 return $this->maxId;
53 }
54
55 private function loadStats() {
56 if ( $this->loaded ) {
57 return;
58 }
59 $this->loaded = true;
60
61 $info = $this->connectionProvider
62 ->getReplicaDatabase()
63 ->newSelectQueryBuilder()
64 ->select( [
65 'min_id' => "MIN({$this->idField})",
66 'max_id' => "MAX({$this->idField})",
67 ] )
68 ->from( $this->tableName )
69 ->caller( __METHOD__ )
70 ->fetchRow();
71 if ( $info ) {
72 $this->minId = (int)$info->min_id;
73 $this->maxId = (int)$info->max_id;
74 }
75 }
76}
Cache and provide min/max ID and "size" (ID delta) of a table.
__construct(private IConnectionProvider $connectionProvider, private string $tableName, private string $idField,)
getIdDelta()
Estimate the number of rows in the table, using the difference of maximum and minimum primary keys.
Provide primary and replica IDatabase connections.