Skip to content

Commit b3976ac

Browse files
author
epriestley
committedFeb 19, 2021
Improve performance of "phabricator:20210215.changeset.02.phid-populate.php"
Summary: Ref T13613. Improve the performance of this migration by using a temporary table and an "UPDATE x JOIN y ..." pattern. Test Plan: - Ran on `secure`, got exit after a few seconds since the migration is idempotent and changesets already had PHIDs. - Ran on `secure` with the `continue;` commented out, got valid new PHIDs in 53s (from 153s). - Tried a larger page size (16K), didn't see any improvement. - From "--trace", client PHID generation seems to be the limiting factor. Subscribers: PHID-OPKG-gm6ozazyms6q6i22gyam Maniphest Tasks: T13613 Differential Revision: https://secure.phabricator.com/D21570
1 parent 7c44657 commit b3976ac

File tree

1 file changed

+53
-8
lines changed

1 file changed

+53
-8
lines changed
 

‎resources/sql/autopatches/20210215.changeset.02.phid-populate.php

+53-8
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,65 @@
77
$conn = $changeset_table->establishConnection('w');
88
$table_name = $changeset_table->getTableName();
99

10-
$iterator = new LiskRawMigrationIterator($conn, $table_name);
11-
foreach ($iterator as $changeset_row) {
12-
$phid = $changeset_row['phid'];
10+
$chunk_size = 4096;
1311

14-
if (strlen($phid)) {
12+
$temporary_table = 'tmp_20210215_changeset_id_map';
13+
14+
queryfx(
15+
$conn,
16+
'CREATE TEMPORARY TABLE %T (
17+
changeset_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
18+
changeset_phid VARBINARY(64) NOT NULL)',
19+
$temporary_table);
20+
21+
$table_iterator = id(new LiskRawMigrationIterator($conn, $table_name))
22+
->setPageSize($chunk_size);
23+
24+
$chunk_iterator = new PhutilChunkedIterator($table_iterator, $chunk_size);
25+
foreach ($chunk_iterator as $chunk) {
26+
27+
$map = array();
28+
foreach ($chunk as $changeset_row) {
29+
$phid = $changeset_row['phid'];
30+
31+
if (strlen($phid)) {
32+
continue;
33+
}
34+
35+
$phid = PhabricatorPHID::generateNewPHID($phid_type);
36+
$id = $changeset_row['id'];
37+
38+
$map[(int)$id] = $phid;
39+
}
40+
41+
if (!$map) {
1542
continue;
1643
}
1744

18-
$phid = PhabricatorPHID::generateNewPHID($phid_type);
45+
$sql = array();
46+
foreach ($map as $changeset_id => $changeset_phid) {
47+
$sql[] = qsprintf(
48+
$conn,
49+
'(%d, %s)',
50+
$changeset_id,
51+
$changeset_phid);
52+
}
53+
54+
queryfx(
55+
$conn,
56+
'TRUNCATE TABLE %T',
57+
$temporary_table);
58+
59+
queryfx(
60+
$conn,
61+
'INSERT INTO %T (changeset_id, changeset_phid) VALUES %LQ',
62+
$temporary_table,
63+
$sql);
1964

2065
queryfx(
2166
$conn,
22-
'UPDATE %T SET phid = %s WHERE id = %d',
67+
'UPDATE %T c JOIN %T x ON c.id = x.changeset_id
68+
SET c.phid = x.changeset_phid',
2369
$table_name,
24-
$phid,
25-
$changeset_row['id']);
70+
$temporary_table);
2671
}

0 commit comments

Comments
 (0)
Failed to load comments.