From 23e190ead50baa2a6337fe53c249b305dabbd941 Mon Sep 17 00:00:00 2001 From: Matt Clarkson Date: Wed, 6 Dec 2017 10:41:01 +1300 Subject: [PATCH] MDL-60976 dml: Optimise replace_all_text() Avoid updating fields that do not match the search string. --- lib/dml/moodle_database.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 1b72c00955c..a16b9336399 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -2402,21 +2402,25 @@ abstract class moodle_database { // Enclose the column name by the proper quotes if it's a reserved word. $columnname = $this->get_manager()->generator->getEncQuoted($column->name); + + $searchsql = $this->sql_like($columnname, '?'); + $searchparam = '%'.$this->sql_like_escape($search).'%'; + $sql = "UPDATE {".$table."} SET $columnname = REPLACE($columnname, ?, ?) - WHERE $columnname IS NOT NULL"; + WHERE $searchsql"; if ($column->meta_type === 'X') { - $this->execute($sql, array($search, $replace)); + $this->execute($sql, array($search, $replace, $searchparam)); } else if ($column->meta_type === 'C') { if (core_text::strlen($search) < core_text::strlen($replace)) { $colsize = $column->max_length; $sql = "UPDATE {".$table."} SET $columnname = " . $this->sql_substr("REPLACE(" . $columnname . ", ?, ?)", 1, $colsize) . " - WHERE $columnname IS NOT NULL"; + WHERE $searchsql"; } - $this->execute($sql, array($search, $replace)); + $this->execute($sql, array($search, $replace, $searchparam)); } }