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

improve memory usage for vacuum on large tables #170

Merged
merged 1 commit into from
Sep 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions sqlalchemy_continuum/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def versioned_relationships(obj, versioned_column_keys):
yield prop


def vacuum(session, model):
def vacuum(session, model, yield_per=1000):
"""
When making structural changes to version tables (for example dropping
columns) there are sometimes situations where some old version records
Expand All @@ -236,22 +236,26 @@ def vacuum(session, model):

:param session: SQLAlchemy session object
:param model: SQLAlchemy declarative model class
:param yield_per: how many rows to process at a time
"""
version_cls = version_class(model)
versions = defaultdict(list)

query = (
session.query(version_cls)
.order_by(option(version_cls, 'transaction_column_name'))
)
).yield_per(yield_per)

primary_key_col = sa.inspection.inspect(model).primary_key[0].name

for version in query:
if versions[version.id]:
prev_version = versions[version.id][-1]
version_id = getattr(version, primary_key_col)
if versions[version_id]:
prev_version = versions[version_id][-1]
if naturally_equivalent(prev_version, version):
session.delete(version)
else:
versions[version.id].append(version)
versions[version_id].append(version)


def is_internal_column(model, column_name):
Expand Down