[*] FO : lots of performance improvements (removed or merged useless SQL queries)

This commit is contained in:
Damien Metzger
2013-07-19 10:58:51 +02:00
parent 219ec6e8be
commit bd4ece095b
4 changed files with 74 additions and 48 deletions
+7
View File
@@ -70,6 +70,13 @@ class GroupCore extends ObjectModel
protected $webserviceParameters = array(); protected $webserviceParameters = array();
public function __construct($id = null, $id_lang = null, $id_shop = null)
{
parent::__construct($id, $id_lang, $id_shop);
if ($this->id && !isset(Group::$group_price_display_method[$this->id]))
self::$group_price_display_method[$this->id] = $this->price_display_method;
}
public static function getGroups($id_lang, $id_shop = false) public static function getGroups($id_lang, $id_shop = false)
{ {
$shop_criteria = ''; $shop_criteria = '';
+16 -9
View File
@@ -113,19 +113,26 @@ class HookCore extends ObjectModel
if (!Validate::isHookName($hook_name)) if (!Validate::isHookName($hook_name))
return false; return false;
$cache_id = 'hook_idbyname_'.$hook_name; $cache_id = 'hook_idsbyname';
if (!Cache::isStored($cache_id)) if (!Cache::isStored($cache_id))
{ {
$retro_hook_name = Hook::getRetroHookName($hook_name); // Get all hook ID by name and alias
Cache::store($cache_id, Db::getInstance()->getValue(' $hook_ids = array();
SELECT `id_hook` $result = Db::getInstance()->ExecuteS('
FROM `'._DB_PREFIX_.'hook` SELECT `id_hook`, `name`
WHERE `name` = \''.pSQL($hook_name).'\' FROM `'._DB_PREFIX_.'hook`
OR `name` = \''.pSQL($retro_hook_name).'\' UNION
')); SELECT `id_hook`, ha.`alias` as name
FROM `ps_hook_alias` ha
INNER JOIN `ps_hook` h ON ha.name = h.name');
foreach ($result as $row)
$hook_ids[$row['name']] = $row['id_hook'];
Cache::store($cache_id, $hook_ids);
} }
else
$hook_ids = Cache::retrieve($cache_id);
return Cache::retrieve($cache_id); return (isset($hook_ids[$hook_name]) ? $hook_ids[$hook_name] : false);
} }
/** /**
+17 -10
View File
@@ -242,16 +242,6 @@ abstract class ObjectModelCore
$this->{$key} = $value; $this->{$key} = $value;
} }
} }
if (!is_array(self::$fieldsRequiredDatabase))
{
$fields = $this->getfieldsRequiredDatabase(true);
if ($fields)
foreach ($fields as $row)
self::$fieldsRequiredDatabase[$row['object_name']][(int)$row['id_required_field']] = pSQL($row['field_name']);
else
self::$fieldsRequiredDatabase = array();
}
} }
/** /**
@@ -901,6 +891,7 @@ abstract class ObjectModelCore
*/ */
public function validateField($field, $value, $id_lang = null) public function validateField($field, $value, $id_lang = null)
{ {
$this->cacheFieldsRequiredDatabase();
$data = $this->def['fields'][$field]; $data = $this->def['fields'][$field];
// Check if field is required // Check if field is required
@@ -983,6 +974,7 @@ abstract class ObjectModelCore
public function validateController($htmlentities = true) public function validateController($htmlentities = true)
{ {
$this->cacheFieldsRequiredDatabase();
$errors = array(); $errors = array();
$required_fields_database = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array(); $required_fields_database = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array();
foreach ($this->def['fields'] as $field => $data) foreach ($this->def['fields'] as $field => $data)
@@ -1029,6 +1021,7 @@ abstract class ObjectModelCore
public function getWebserviceParameters($ws_params_attribute_name = null) public function getWebserviceParameters($ws_params_attribute_name = null)
{ {
$this->cacheFieldsRequiredDatabase();
$default_resource_parameters = array( $default_resource_parameters = array(
'objectSqlId' => $this->def['primary'], 'objectSqlId' => $this->def['primary'],
'retrieveData' => array( 'retrieveData' => array(
@@ -1139,6 +1132,7 @@ abstract class ObjectModelCore
public function validateFieldsRequiredDatabase($htmlentities = true) public function validateFieldsRequiredDatabase($htmlentities = true)
{ {
$this->cacheFieldsRequiredDatabase();
$errors = array(); $errors = array();
$required_fields = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array(); $required_fields = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array();
@@ -1166,6 +1160,19 @@ abstract class ObjectModelCore
FROM '._DB_PREFIX_.'required_field FROM '._DB_PREFIX_.'required_field
'.(!$all ? 'WHERE object_name = \''.pSQL(get_class($this)).'\'' : '')); '.(!$all ? 'WHERE object_name = \''.pSQL(get_class($this)).'\'' : ''));
} }
public function cacheFieldsRequiredDatabase()
{
if (!is_array(self::$fieldsRequiredDatabase))
{
$fields = $this->getfieldsRequiredDatabase(true);
if ($fields)
foreach ($fields as $row)
self::$fieldsRequiredDatabase[$row['object_name']][(int)$row['id_required_field']] = pSQL($row['field_name']);
else
self::$fieldsRequiredDatabase = array();
}
}
public function addFieldsRequiredDatabase($fields) public function addFieldsRequiredDatabase($fields)
{ {
+34 -29
View File
@@ -153,18 +153,22 @@ abstract class ModuleCore
// If cache is not generated, we generate it // If cache is not generated, we generate it
if (self::$modules_cache == null && !is_array(self::$modules_cache)) if (self::$modules_cache == null && !is_array(self::$modules_cache))
{ {
// Join clause is done to check if the module is activated in current shop context $id_shop = (Validate::isLoadedObject($this->context->shop) ? $this->context->shop->id : 1);
$sql_limit_shop = 'SELECT COUNT(*) FROM `'._DB_PREFIX_.'module_shop` ms WHERE m.`id_module` = ms.`id_module` AND ms.`id_shop` = '.((is_object(Context::getContext()->shop) && $id = (int)Context::getContext()->shop->id) ? $id : 1);
$sql = 'SELECT m.`id_module`, m.`name`, ('.$sql_limit_shop.') as total FROM `'._DB_PREFIX_.'module` m';
// Result is cached
self::$modules_cache = array(); self::$modules_cache = array();
$result = Db::getInstance()->executeS($sql); // Join clause is done to check if the module is activated in current shop context
$result = Db::getInstance()->executeS('
SELECT m.`id_module`, m.`name`, (
SELECT id_module
FROM `'._DB_PREFIX_.'module_shop` ms
WHERE m.`id_module` = ms.`id_module`
AND ms.`id_shop` = '.(int)$id_shop.'
LIMIT 1
) as mshop
FROM `'._DB_PREFIX_.'module` m');
foreach ($result as $row) foreach ($result as $row)
{ {
self::$modules_cache[$row['name']] = $row; self::$modules_cache[$row['name']] = $row;
self::$modules_cache[$row['name']]['active'] = ($row['total'] > 0) ? 1 : 0; self::$modules_cache[$row['name']]['active'] = ($row['mshop'] > 0) ? 1 : 0;
} }
} }
@@ -1511,12 +1515,12 @@ abstract class ModuleCore
* @param int $id_hook Hook ID * @param int $id_hook Hook ID
* @return array Exceptions * @return array Exceptions
*/ */
protected static $exceptionsCache = null; public function getExceptions($id_hook, $dispatch = false)
public function getExceptions($hookID, $dispatch = false)
{ {
if (self::$exceptionsCache === null) $cache_id = 'exceptionsCache';
if (!Cache::isStored($cache_id))
{ {
self::$exceptionsCache = array(); $exceptionsCache = array();
$sql = 'SELECT * FROM `'._DB_PREFIX_.'hook_module_exceptions` $sql = 'SELECT * FROM `'._DB_PREFIX_.'hook_module_exceptions`
WHERE `id_shop` IN ('.implode(', ', Shop::getContextListShopID()).')'; WHERE `id_shop` IN ('.implode(', ', Shop::getContextListShopID()).')';
$result = Db::getInstance()->executeS($sql); $result = Db::getInstance()->executeS($sql);
@@ -1525,33 +1529,34 @@ abstract class ModuleCore
if (!$row['file_name']) if (!$row['file_name'])
continue; continue;
$key = $row['id_hook'].'-'.$row['id_module']; $key = $row['id_hook'].'-'.$row['id_module'];
if (!isset(self::$exceptionsCache[$key])) if (!isset($exceptionsCache[$key]))
self::$exceptionsCache[$key] = array(); $exceptionsCache[$key] = array();
if (!isset(self::$exceptionsCache[$key][$row['id_shop']])) if (!isset($exceptionsCache[$key][$row['id_shop']]))
self::$exceptionsCache[$key][$row['id_shop']] = array(); $exceptionsCache[$key][$row['id_shop']] = array();
self::$exceptionsCache[$key][$row['id_shop']][] = $row['file_name']; $exceptionsCache[$key][$row['id_shop']][] = $row['file_name'];
} }
Cache::store($cache_id, $exceptionsCache);
} }
else
$exceptionsCache = !Cache::retrieve($cache_id);
$key = $hookID.'-'.$this->id; $key = $id_hook.'-'.$this->id;
if (!$dispatch) $array_return = array();
if ($dispatch)
{ {
$files = array();
foreach (Shop::getContextListShopID() as $shop_id) foreach (Shop::getContextListShopID() as $shop_id)
if (isset(self::$exceptionsCache[$key], self::$exceptionsCache[$key][$shop_id])) if (isset($exceptionsCache[$key], $exceptionsCache[$key][$shop_id]))
foreach (self::$exceptionsCache[$key][$shop_id] as $file) $array_return[$shop_id] = $exceptionsCache[$key][$shop_id];
if (!in_array($file, $files))
$files[] = $file;
return $files;
} }
else else
{ {
$list = array();
foreach (Shop::getContextListShopID() as $shop_id) foreach (Shop::getContextListShopID() as $shop_id)
if (isset(self::$exceptionsCache[$key], self::$exceptionsCache[$key][$shop_id])) if (isset($exceptionsCache[$key], $exceptionsCache[$key][$shop_id]))
$list[$shop_id] = self::$exceptionsCache[$key][$shop_id]; foreach ($exceptionsCache[$key][$shop_id] as $file)
return $list; if (!in_array($file, $array_return))
$array_return[] = $file;
} }
return $array_return;
} }
public static function isInstalled($module_name) public static function isInstalled($module_name)