Skip to content

Commit 9a75151

Browse files
authored
refactor(database): optimize _processForeignKeys() string allocations and loop invariants (#10351)
* perf: optimize _processForeignKeys() Refactors _processForeignKeys() to: - calculate invariant SQL fragments outside the loop - cache escaped table identifier - build SQL string locally before concatenating to the array, saving intermediate string allocations * refactor: apply code review fixes for _processForeignKeys optimization - Restore fast return without foreign keys\n- Remove unrelated formatting changes\n- Remove non-English comment
1 parent d9ec94b commit 9a75151

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

‎system/Database/Forge.php‎

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,10 @@ protected function _processIndexes(string $table, bool $asQuery = false): array
12091209
*/
12101210
protected function _processForeignKeys(string $table, bool $asQuery = false): array
12111211
{
1212+
if ($this->foreignKeys === []) {
1213+
return [''];
1214+
}
1215+
12121216
$errorNames = [];
12131217

12141218
foreach ($this->foreignKeys as $fkeyInfo) {
@@ -1225,40 +1229,43 @@ protected function _processForeignKeys(string $table, bool $asQuery = false): ar
12251229
throw new DatabaseException(lang('Database.fieldNotExists', $errorNames));
12261230
}
12271231

1228-
$sqls = [''];
1229-
1230-
foreach ($this->foreignKeys as $index => $fkey) {
1231-
if ($asQuery === false) {
1232-
$index = 0;
1233-
} else {
1234-
$sqls[$index] = '';
1235-
}
1232+
$sqls = [''];
1233+
$isOci8 = $this->db->DBDriver === 'OCI8';
1234+
$dbPrefix = $this->db->DBPrefix;
1235+
$fkSuffix = $isOci8 ? '_fk' : '_foreign';
12361236

1237-
$nameIndex = $fkey['fkName'] !== '' ?
1238-
$fkey['fkName'] :
1239-
$table . '_' . implode('_', $fkey['field']) . ($this->db->DBDriver === 'OCI8' ? '_fk' : '_foreign');
1237+
$prefixSql = $asQuery
1238+
? 'ALTER TABLE ' . $this->db->escapeIdentifiers($dbPrefix . $table) . ' ADD '
1239+
: ",\n\t";
12401240

1241-
$nameIndexFilled = $this->db->escapeIdentifiers($nameIndex);
1242-
$foreignKeyFilled = implode(', ', $this->db->escapeIdentifiers($fkey['field']));
1243-
$referenceTableFilled = $this->db->escapeIdentifiers($this->db->DBPrefix . $fkey['referenceTable']);
1244-
$referenceFieldFilled = implode(', ', $this->db->escapeIdentifiers($fkey['referenceField']));
1241+
foreach ($this->foreignKeys as $index => $fkey) {
1242+
$idx = $asQuery ? $index : 0;
12451243

12461244
if ($asQuery) {
1247-
$sqls[$index] .= 'ALTER TABLE ' . $this->db->escapeIdentifiers($this->db->DBPrefix . $table) . ' ADD ';
1248-
} else {
1249-
$sqls[$index] .= ",\n\t";
1245+
$sqls[$idx] = '';
12501246
}
12511247

1252-
$formatSql = 'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)';
1253-
$sqls[$index] .= sprintf($formatSql, $nameIndexFilled, $foreignKeyFilled, $referenceTableFilled, $referenceFieldFilled);
1248+
$nameIndex = $fkey['fkName'] !== ''
1249+
? $fkey['fkName']
1250+
: $table . '_' . implode('_', $fkey['field']) . $fkSuffix;
1251+
1252+
$sql = $prefixSql . sprintf(
1253+
'CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s(%s)',
1254+
$this->db->escapeIdentifiers($nameIndex),
1255+
implode(', ', $this->db->escapeIdentifiers($fkey['field'])),
1256+
$this->db->escapeIdentifiers($dbPrefix . $fkey['referenceTable']),
1257+
implode(', ', $this->db->escapeIdentifiers($fkey['referenceField'])),
1258+
);
12541259

12551260
if ($fkey['onDelete'] !== false && in_array($fkey['onDelete'], $this->fkAllowActions, true)) {
1256-
$sqls[$index] .= ' ON DELETE ' . $fkey['onDelete'];
1261+
$sql .= ' ON DELETE ' . $fkey['onDelete'];
12571262
}
12581263

1259-
if ($this->db->DBDriver !== 'OCI8' && $fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $this->fkAllowActions, true)) {
1260-
$sqls[$index] .= ' ON UPDATE ' . $fkey['onUpdate'];
1264+
if (! $isOci8 && $fkey['onUpdate'] !== false && in_array($fkey['onUpdate'], $this->fkAllowActions, true)) {
1265+
$sql .= ' ON UPDATE ' . $fkey['onUpdate'];
12611266
}
1267+
1268+
$sqls[$idx] .= $sql;
12621269
}
12631270

12641271
return $sqls;

0 commit comments

Comments
 (0)