Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Minor optimization for updating DatasetDAO columns and metrics #20473

Merged

Conversation

john-bodley
Copy link
Member

@john-bodley john-bodley commented Jun 22, 2022

SUMMARY

This PR makes the minimal updates as possible to the DatasetDAO update logic to improve performance as the request was timing out when we (Airbnb) were trying to update a very very large dataset. If this is not suffice, the next and more aggressive update would be to add bulk updating.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

CI.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@john-bodley john-bodley marked this pull request as ready for review June 22, 2022 23:12
properties["columns"] = cls.update_columns(
model, properties.get("columns", []), commit=commit
)
properties["columns"] = cls.update_columns(model, properties["columns"])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get(...) is not needed here per the check on line #165.

for column in property_columns:
column_id = column.get("id")

if column_id:
column_obj = db.session.query(TableColumn).get(column_id)
Copy link
Member Author

@john-bodley john-bodley Jun 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leverage the columns defined by the model rather than re-querying for each column individually.

column_obj = DatasetDAO.update_column(column_obj, column, commit=commit)
column_obj = DatasetDAO.update_column(
column_by_id.get(column_id),
commit=False,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to commit when updating the column. It can all be committed when the dataset is updated.

else:
column_obj = DatasetDAO.create_column(column, commit=commit)
column_obj = DatasetDAO.create_column(column, commit=False)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to commit when creating the column. Also the TableColumn object has no table_id at this time and thus the record needs to be updated when the entire dataset is committed. Also autoflush=True by default so the new column should have a primary key associated with it.

column_obj = db.session.query(TableColumn).get(column_id)
column_obj = DatasetDAO.update_column(column_obj, column, commit=commit)
column_obj = DatasetDAO.update_column(
column_by_id.get(column_id),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same behavior as before, i.e., the input column_obj is None if for some reason it doesn't exist—which shouldn't happen.

@codecov
Copy link

codecov bot commented Jun 22, 2022

Codecov Report

Merging #20473 (a242b25) into master (a169b60) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master   #20473      +/-   ##
==========================================
- Coverage   66.75%   66.75%   -0.01%     
==========================================
  Files        1740     1740              
  Lines       65149    65143       -6     
  Branches     6898     6898              
==========================================
- Hits        43492    43486       -6     
  Misses      19908    19908              
  Partials     1749     1749              
Flag Coverage Δ
hive 53.72% <9.09%> (+0.01%) ⬆️
mysql 82.33% <100.00%> (-0.01%) ⬇️
postgres 82.40% <100.00%> (-0.01%) ⬇️
presto 53.58% <9.09%> (+0.01%) ⬆️
python 82.83% <100.00%> (-0.01%) ⬇️
sqlite 82.18% <100.00%> (-0.01%) ⬇️
unit 50.56% <9.09%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
superset/datasets/dao.py 93.57% <100.00%> (-0.27%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a169b60...a242b25. Read the comment docs.

@john-bodley john-bodley force-pushed the john-bodley-dataset-dao-update-commit branch from b74e5e6 to cc1533a Compare June 22, 2022 23:40
@john-bodley john-bodley force-pushed the john-bodley-dataset-dao-update-commit branch from cc1533a to a242b25 Compare June 23, 2022 02:44
@pull-request-size pull-request-size bot added size/L and removed size/M labels Jun 23, 2022
# Note for new columns the primary key is undefined sans a commit/flush.
columns.append(DatasetDAO.create_column(properties, commit=False))

for id_ in {obj.id for obj in model.columns} - {obj.id for obj in columns}:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimized looping.

DatasetDAO.update_column(
column_by_id[properties["id"]],
properties,
commit=False,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commit once for the dataset.

model: TableColumn,
properties: Dict[str, Any],
commit: bool = True,
) -> TableColumn:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return is not Optional[...] per the superset/dao/base.py methods.

@@ -287,24 +296,27 @@ def find_dataset_metric(
return db.session.query(SqlMetric).get(metric_id)

@classmethod
def delete_metric(
cls, model: SqlMetric, commit: bool = True
) -> Optional[TableColumn]:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should have been SqlMetric and not TableColumn.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the command still return the metric when it is deleted?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, prob not needed

metrics.append(DatasetDAO.create_metric(properties, commit=False))

for id_ in {obj.id for obj in model.metrics} - {obj.id for obj in metrics}:
DatasetDAO.delete_column(metric_by_id[id_], commit=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't SQLA automatically delete orphaned metrics once they are not associated with a dataset?

Copy link
Member

@ktmud ktmud left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a good idea to always skip auto-commit.

@john-bodley john-bodley merged commit f29cde2 into apache:master Jun 25, 2022
@john-bodley john-bodley deleted the john-bodley-dataset-dao-update-commit branch June 25, 2022 21:31
akshatsri pushed a commit to charan1314/superset that referenced this pull request Jul 19, 2022
…ics (apache#20473)

Co-authored-by: John Bodley <john.bodley@airbnb.com>
john-bodley added a commit to john-bodley/superset that referenced this pull request Jul 21, 2022
john-bodley added a commit that referenced this pull request Jul 27, 2022
* refactor: Improve performance regression introduced in #20473

* Update dao.py
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 2.1.0 and removed 🚢 2.1.3 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L 🚢 2.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants