From 50d2e5a15d83b4759cdbbcce363a83f6fe12c6bb Mon Sep 17 00:00:00 2001 From: John Bodley <4567245+john-bodley@users.noreply.github.com> Date: Thu, 28 Jul 2022 22:56:39 -0700 Subject: [PATCH] fix: Address performance regression introduced in #11785 (#20893) --- .pylintrc | 4 +++- superset/connectors/sqla/models.py | 11 +++++------ superset/views/core.py | 15 +++++++++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.pylintrc b/.pylintrc index dbc0eabaefcec..848767fe5dcb1 100644 --- a/.pylintrc +++ b/.pylintrc @@ -134,7 +134,9 @@ include-naming-hint=no # List of decorators that produce properties, such as abc.abstractproperty. Add # to this list to register other decorators that produce valid properties. -property-classes=abc.abstractproperty +property-classes= + abc.abstractproperty, + sqlalchemy.ext.hybrid.hybrid_property # Regular expression matching correct argument names argument-rgx=[a-z_][a-z0-9_]{2,30}$ diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py index 237a71ab7b76a..cb1673c8287a7 100644 --- a/superset/connectors/sqla/models.py +++ b/superset/connectors/sqla/models.py @@ -66,6 +66,7 @@ update, ) from sqlalchemy.engine.base import Connection +from sqlalchemy.ext.hybrid import hybrid_property from sqlalchemy.orm import backref, Query, relationship, RelationshipProperty, Session from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.mapper import Mapper @@ -741,7 +742,7 @@ class SqlaTable(Model, BaseDatasource): # pylint: disable=too-many-public-metho "MAX": sa.func.MAX, } - def __repr__(self) -> str: + def __repr__(self) -> str: # pylint: disable=invalid-repr-returned return self.name @staticmethod @@ -835,11 +836,9 @@ def get_perm(self) -> str: raise DatasetInvalidPermissionEvaluationException() return f"[{self.database}].[{self.table_name}](id:{self.id})" - @property - def name(self) -> str: - if not self.schema: - return self.table_name - return "{}.{}".format(self.schema, self.table_name) + @hybrid_property + def name(self) -> str: # pylint: disable=invalid-overridden-method + return self.schema + "." + self.table_name if self.schema else self.table_name @property def full_name(self) -> str: diff --git a/superset/views/core.py b/superset/views/core.py index 8363cde1911e7..f3835dd83f717 100755 --- a/superset/views/core.py +++ b/superset/views/core.py @@ -1207,7 +1207,16 @@ def is_match(src: str, target: utils.DatasourceName) -> bool: max_tables = max_items * len(tables) // total_items max_views = max_items * len(views) // total_items - dataset_tables = {table.name: table for table in database.tables} + extra_dict_by_name = { + table.name: table.extra_dict + for table in ( + db.session.query(SqlaTable).filter( + SqlaTable.name.in_( # # pylint: disable=no-member + f"{table.schema}.{table.table}" for table in tables + ) + ) + ).all() + } table_options = [ { @@ -1216,9 +1225,7 @@ def is_match(src: str, target: utils.DatasourceName) -> bool: "label": get_datasource_label(tn), "title": get_datasource_label(tn), "type": "table", - "extra": dataset_tables[f"{tn.schema}.{tn.table}"].extra_dict - if (f"{tn.schema}.{tn.table}" in dataset_tables) - else None, + "extra": extra_dict_by_name.get(f"{tn.schema}.{tn.table}", None), } for tn in tables[:max_tables] ]