From 0882c187fc03847c312970d7a23eac7da0ed5311 Mon Sep 17 00:00:00 2001 From: Samy Rabih Date: Sat, 2 Mar 2013 18:21:04 +0100 Subject: [PATCH 1/2] Import time improvment Instead of making as many queries as you ps_feature entries (for each product), it is possible to make it in one query. And we test if the user has the MySQL rights to create temporary tables. --- classes/Feature.php | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/classes/Feature.php b/classes/Feature.php index 6d9c2dc7e..c0c3c0105 100644 --- a/classes/Feature.php +++ b/classes/Feature.php @@ -313,19 +313,30 @@ class FeatureCore extends ObjectModel public static function cleanPositions() { $return = true; - $sql = ' - SELECT `id_feature` - FROM `'._DB_PREFIX_.'feature` - ORDER BY `position`'; - $result = Db::getInstance()->executeS($sql); + CREATE TEMPORARY TABLE `'._DB_PREFIX_.'feature_tmp` ( + `rank` INT NOT NULL AUTO_INCREMENT, + `id_feature` int not null, + `position` int not null, + primary key(rank) + ); + INSERT INTO '._DB_PREFIX_.'feature_tmp(id_feature,position) SELECT id_feature,position FROM '._DB_PREFIX_.'feature ORDER BY position ASC; + UPDATE `'._DB_PREFIX_.'feature` f LEFT JOIN '._DB_PREFIX_.'feature_tmp t USING(id_feature) SET f.position = rank-1'; + $return = Db::getInstance()->executeS($sql); + if (!$return) { + $sql = ' + SELECT `id_feature` + FROM `'._DB_PREFIX_.'feature` + ORDER BY `position`'; + $result = Db::getInstance()->executeS($sql); - $i = 0; - foreach ($result as $value) - $return = Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'feature` - SET `position` = '.(int)$i++.' - WHERE `id_feature` = '.(int)$value['id_feature']); + $i = 0; + foreach ($result as $value) + $return = Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'feature` + SET `position` = '.(int)$i++.' + WHERE `id_feature` = '.(int)$value['id_feature']); + } return $return; } From 56d4c8339a833ff96399d5050fd68b192a5fab2b Mon Sep 17 00:00:00 2001 From: Samy Rabih Date: Sat, 2 Mar 2013 18:33:59 +0100 Subject: [PATCH 2/2] cleanPositions query update We now use an unique query, with no need to use temporary table. Thanks to @sylwit for the tip ! --- classes/Feature.php | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/classes/Feature.php b/classes/Feature.php index c0c3c0105..83b49bb57 100644 --- a/classes/Feature.php +++ b/classes/Feature.php @@ -314,29 +314,13 @@ class FeatureCore extends ObjectModel { $return = true; $sql = ' - CREATE TEMPORARY TABLE `'._DB_PREFIX_.'feature_tmp` ( - `rank` INT NOT NULL AUTO_INCREMENT, - `id_feature` int not null, - `position` int not null, - primary key(rank) - ); - INSERT INTO '._DB_PREFIX_.'feature_tmp(id_feature,position) SELECT id_feature,position FROM '._DB_PREFIX_.'feature ORDER BY position ASC; - UPDATE `'._DB_PREFIX_.'feature` f LEFT JOIN '._DB_PREFIX_.'feature_tmp t USING(id_feature) SET f.position = rank-1'; + UPDATE `'._DB_PREFIX_.'feature` f LEFT JOIN + (SELECT @i := @i +1 AS rank, id_feature, position + FROM `'._DB_PREFIX_.'feature` JOIN (SELECT @i :=1) dummy + ORDER by position) AS f2 + USING(id_feature) + SET f.position = f2.rank-1'; $return = Db::getInstance()->executeS($sql); - if (!$return) { - $sql = ' - SELECT `id_feature` - FROM `'._DB_PREFIX_.'feature` - ORDER BY `position`'; - $result = Db::getInstance()->executeS($sql); - - $i = 0; - foreach ($result as $value) - $return = Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'feature` - SET `position` = '.(int)$i++.' - WHERE `id_feature` = '.(int)$value['id_feature']); - } return $return; }