diff --git a/core/ClassInfo.php b/core/ClassInfo.php index 9651754e3..42d80275a 100755 --- a/core/ClassInfo.php +++ b/core/ClassInfo.php @@ -24,13 +24,24 @@ class ClassInfo { return isset($_ALL_CLASSES['exists'][$class]) ? $_ALL_CLASSES['exists'][$class] : null; } + /** + * Cache for {@link hasTable()} + */ + private static $_cache_all_tables = null; + /** * @todo Move this to Database or DB */ static function hasTable($class) { if(DB::isActive()) { - $SQL_table = Convert::raw2sql($class); - return (bool)(DB::query("SHOW TABLES LIKE '$SQL_table'")->value()); + // Cache the list of all table names to reduce on DB traffic + if(self::$_cache_all_tables === null) { + self::$_cache_all_tables = array(); + $tables = DB::query("SHOW TABLES")->column(); + foreach($tables as $table) self::$_cache_all_tables[strtolower($table)] = true; + } + return isset(self::$_cache_all_tables[strtolower($class)]); + } else { return false; } diff --git a/core/model/MySQLDatabase.php b/core/model/MySQLDatabase.php index 2249fcd5f..ae3556599 100644 --- a/core/model/MySQLDatabase.php +++ b/core/model/MySQLDatabase.php @@ -269,6 +269,8 @@ class MySQLDatabase extends Database { } } + private static $_cache_collation_info = array(); + public function fieldList($table) { $fields = DB::query("SHOW FULL FIELDS IN `$table`"); foreach($fields as $field) { @@ -278,7 +280,11 @@ class MySQLDatabase extends Database { } if($field['Collation'] && $field['Collation'] != 'NULL') { - $collInfo = DB::query("SHOW COLLATION LIKE '$field[Collation]'")->record(); + // Cache collation info to cut down on database traffic + if(!isset(self::$_cache_collation_info[$field['Collation']])) { + self::$_cache_collation_info[$field['Collation']] = DB::query("SHOW COLLATION LIKE '$field[Collation]'")->record(); + } + $collInfo = self::$_cache_collation_info[$field['Collation']]; $fieldSpec .= " character set $collInfo[Charset] collate $field[Collation]"; }