From 7d6244699410e2186ea7fcf812ed60886017efd7 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Mon, 3 Dec 2018 16:30:21 +0000 Subject: [PATCH 1/5] FIX Filtering the version history by a specific date is now more performant in large datasets --- src/Versioned.php | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Versioned.php b/src/Versioned.php index ac1c9194..62599d81 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -634,6 +634,21 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ $stageCondition = ''; } + // Get base query conditions and add them to the inner join as well, this helps with performance + // in larger data sets + $baseQueryConditions = ''; + $baseQueryParams = []; + foreach ($query->getWhere() as $condition) { + foreach ($condition as $key => $value) { + $baseQueryConditions .= ' AND ' . $key; + // Normalise array values + if (is_array($value) && count($value) === 1) { + $value = reset($value); + } + $baseQueryParams[] = $value; + } + } + // Join on latest version filtered by date $query->addInnerJoin( << Date: Tue, 4 Dec 2018 12:06:02 +0000 Subject: [PATCH 2/5] Move inner join into a where condition --- src/Versioned.php | 49 ++++++++++++----------------------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/src/Versioned.php b/src/Versioned.php index 62599d81..c67b29d3 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -10,6 +10,7 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; +use SilverStripe\Core\Convert; use SilverStripe\Core\Extension; use SilverStripe\Core\Resettable; use SilverStripe\Dev\Deprecation; @@ -634,44 +635,18 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ $stageCondition = ''; } - // Get base query conditions and add them to the inner join as well, this helps with performance - // in larger data sets - $baseQueryConditions = ''; - $baseQueryParams = []; - foreach ($query->getWhere() as $condition) { - foreach ($condition as $key => $value) { - $baseQueryConditions .= ' AND ' . $key; - // Normalise array values - if (is_array($value) && count($value) === 1) { - $value = reset($value); - } - $baseQueryParams[] = $value; - } - } - - // Join on latest version filtered by date - $query->addInnerJoin( - <<addWhere(["\"{$baseTable}_Versions\".\"Version\"" => $latestVersionSql]); } /** From a25830933d5bab095172fe1ab982421ea8071d91 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Tue, 4 Dec 2018 13:58:04 +0000 Subject: [PATCH 3/5] Incorrect table name used in LastEdited filter --- src/Versioned.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Versioned.php b/src/Versioned.php index c67b29d3..18630ccc 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -641,7 +641,7 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ ( SELECT MAX("{$baseTable}_Versions"."Version") FROM "{$baseTable}_Versions" AS "{$baseTable}_Versions_Latest" - WHERE "{$baseTable}_Versions"."LastEdited" <= {$date} + WHERE "{$baseTable}_Versions_Latest"."LastEdited" <= {$date} AND "{$baseTable}_Versions_Latest"."RecordID" = "{$baseTable}_Versions"."RecordID" {$stageCondition} ) From c8ed539e57541ce49278147fb5217d6e3bd4a746 Mon Sep 17 00:00:00 2001 From: Guy Marriott Date: Wed, 5 Dec 2018 10:02:57 +1300 Subject: [PATCH 4/5] Another incorrect table name used in LastEdited filter --- src/Versioned.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Versioned.php b/src/Versioned.php index 18630ccc..86b6a340 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -639,7 +639,7 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ $date = Convert::raw2sql($date); $latestVersionSql = << Date: Thu, 6 Dec 2018 09:58:20 +1300 Subject: [PATCH 5/5] Another incorrect table name used in LastEdited filter --- src/Versioned.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Versioned.php b/src/Versioned.php index 86b6a340..4ae6f237 100644 --- a/src/Versioned.php +++ b/src/Versioned.php @@ -630,7 +630,7 @@ protected function augmentSQLVersionedArchive(SQLSelect $query, DataQuery $dataQ $stageColumn = $stage === static::LIVE ? 'WasPublished' : 'WasDraft'; - $stageCondition = "AND \"{$baseTable}_Versions\".\"{$stageColumn}\" = 1"; + $stageCondition = "AND \"{$baseTable}_Versions_Latest\".\"{$stageColumn}\" = 1"; } else { $stageCondition = ''; }