Skip to content

Commit 0b8596f

Browse files
rosalieperoutdooracorndeer-wmde
authored
Reduce number of queries executed in the WikiUserEmailChecker service (#1043)
Reduce number of queries executed in the `WikiUserEmailChecker` service in order to improve performance. Bug: T415017 --------- Co-authored-by: Ollie <43674967+outdooracorn@users.noreply.github.com> Co-authored-by: Ollie <oliver.hyde@wikimedia.de> Co-authored-by: dena <91744937+deer-wmde@users.noreply.github.com>
1 parent de75abb commit 0b8596f

3 files changed

Lines changed: 15 additions & 19 deletions

File tree

app/Services/WikiUserEmailChecker.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@ public function findEmail(string $email): array {
1212
$this->db->purge('mw');
1313
$pdo = $this->db->connection('mw')->getPdo();
1414

15-
$mwDatabases = $pdo
16-
->query("SHOW DATABASES LIKE 'mwdb_%'")
17-
->fetchAll(PDO::FETCH_COLUMN);
18-
1915
$foundIn = [];
2016

21-
foreach ($mwDatabases as $dbName) {
22-
$userTable = $this->findUserTable($pdo, $dbName);
23-
24-
if (!$userTable) {
25-
continue;
26-
}
17+
$userTables = $this->getAllMediaWikiUserTables($pdo);
2718

19+
foreach ($userTables as $dbName => $userTable) {
2820
if ($this->emailExists($pdo, $dbName, $userTable, $email)) {
2921
$foundIn[] = "{$dbName}.{$userTable}";
3022
}
@@ -33,18 +25,21 @@ public function findEmail(string $email): array {
3325
return $foundIn;
3426
}
3527

36-
private function findUserTable(PDO $pdo, string $dbName): ?string {
37-
$stmt = $pdo->prepare("
38-
SELECT TABLE_NAME
28+
private function getAllMediaWikiUserTables(PDO $pdo): array {
29+
$stmt = $pdo->query("
30+
SELECT TABLE_SCHEMA, TABLE_NAME
3931
FROM INFORMATION_SCHEMA.TABLES
40-
WHERE TABLE_SCHEMA = :db
41-
AND TABLE_NAME LIKE '%\_user'
42-
LIMIT 1
32+
WHERE TABLE_SCHEMA LIKE 'mwdb_%'
33+
AND TABLE_NAME LIKE '%_user'
4334
");
4435

45-
$stmt->execute(['db' => $dbName]);
36+
$tablesByDb = [];
37+
38+
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
39+
$tablesByDb[$row['TABLE_SCHEMA']] = $row['TABLE_NAME'];
40+
}
4641

47-
return $stmt->fetchColumn() ?: null;
42+
return $tablesByDb;
4843
}
4944

5045
private function emailExists(PDO $pdo, string $dbName, string $table, string $email): bool {

tests/Commands/User/CheckUserEmailExistTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function testCaseInsensitive() {
6060
User::factory()->create([
6161
'email' => 'Test@Example.com',
6262
]);
63+
6364
$exists = User::whereEmailInsensitive('tEsT@eXaMpLe.CoM')->exists();
6465

6566
$this->assertTrue($exists);

tests/Services/WikiUserEmailCheckerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testCorrectDatabaseFound(): void {
5858

5959
public function testEmailFoundInMultipleDatabases(): void {
6060
$checker = new WikiUserEmailChecker($this->db);
61-
$this->assertEquals(
61+
$this->assertEqualsCanonicalizing(
6262
['mwdb_1.db_1_user', 'mwdb_2.db_2_user'],
6363
$checker->findEmail('user1@email.localhost')
6464
);

0 commit comments

Comments
 (0)