From 993a9f3b23436cddb195740f518c38cd14b9cdd0 Mon Sep 17 00:00:00 2001 From: Gareth James Date: Thu, 18 Jul 2024 23:18:55 +0100 Subject: [PATCH] Reduce impact of scan for a db large tables uses an query to generate a query to run an RLIKE on all fields in the table, so that you only get rows return with encrypted values (and not every row), thus reducing the memory impact --- update-encryption.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/update-encryption.php b/update-encryption.php index 324c655..d0ebafe 100644 --- a/update-encryption.php +++ b/update-encryption.php @@ -113,7 +113,16 @@ if ($skipTable) continue; - $data = $db->query("SELECT * FROM $table")->fetchAll(PDO::FETCH_ASSOC); + $sqlBuilder = "SELECT + CONCAT('SELECT * FROM $table WHERE ', GROUP_CONCAT('`', COLUMN_NAME, '`' SEPARATOR ' RLIKE ''^[0-9]{1}:[0-9]{1}:'' OR '), ' LIKE ''^[0-9]{1}:[0-9]{1}:''') as query + FROM INFORMATION_SCHEMA.COLUMNS + WHERE + TABLE_NAME = '$table' + AND + TABLE_SCHEMA = '{$config['dbname']}'"; + + $builtSql = $db->query($sqlBuilder)->fetch(PDO::FETCH_ASSOC); + $data = $db->query($builtSql['query'])->fetchAll(PDO::FETCH_ASSOC); if (!$data) continue; foreach ($data as $row) {