From 3ecb6fdcb0efcade41b9a6342718800b3293e4bf Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Thu, 2 Apr 2020 06:45:54 +0000 Subject: [PATCH] [IMP] core: optimize `model._auto_init()` on a new model Create the table with all the columns from scratch, with the NOT NULL constraint when required. Also do not call `_check_removed_columns()` on a new table. This saves 0.5% of the total installation time. X-original-commit: 0727cacf5194a143b15ab4cb9893f3035a67be1f --- odoo/models.py | 12 ++++++++++-- odoo/tools/sql.py | 19 ++++++++++++++++--- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/odoo/models.py b/odoo/models.py index 1c0ecbc20ee..3dbffd54d06 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -2486,14 +2486,22 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})): if self._auto: if must_create_table: - tools.create_model_table(cr, self._table, self._description) + def make_type(field): + return field.column_type[1] + (" NOT NULL" if field.required else "") + + tools.create_model_table(cr, self._table, self._description, [ + (name, make_type(field), field.string) + for name, field in self._fields.items() + if name != 'id' and field.store and field.column_type + ]) if self._parent_store: if not tools.column_exists(cr, self._table, 'parent_path'): self._create_parent_columns() parent_path_compute = True - self._check_removed_columns(log=False) + if not must_create_table: + self._check_removed_columns(log=False) # update the database schema for fields columns = tools.table_columns(cr, self._table) diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py index 3bcbd39e139..69968f78de8 100644 --- a/odoo/tools/sql.py +++ b/odoo/tools/sql.py @@ -48,11 +48,24 @@ def table_kind(cr, tablename): cr.execute(query, (tablename,)) return cr.fetchone()[0] if cr.rowcount else None -def create_model_table(cr, tablename, comment=None): +def create_model_table(cr, tablename, comment=None, columns=()): """ Create the table for a model. """ - cr.execute('CREATE TABLE "{}" (id SERIAL NOT NULL, PRIMARY KEY(id))'.format(tablename)) + colspecs = ['id SERIAL NOT NULL'] + [ + '"{}" {}'.format(columnname, columntype) + for columnname, columntype, columncomment in columns + ] + cr.execute('CREATE TABLE "{}" ({}, PRIMARY KEY(id))'.format(tablename, ", ".join(colspecs))) + + queries, params = [], [] if comment: - cr.execute('COMMENT ON TABLE "{}" IS %s'.format(tablename), (comment,)) + queries.append('COMMENT ON TABLE "{}" IS %s'.format(tablename)) + params.append(comment) + for columnname, columntype, columncomment in columns: + queries.append('COMMENT ON COLUMN "{}"."{}" IS %s'.format(tablename, columnname)) + params.append(columncomment) + if queries: + cr.execute("; ".join(queries), params) + _schema.debug("Table %r: created", tablename) def table_columns(cr, tablename):