镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Optimize deleting large calendars #50

Open

Description

@thomasst

def _delete_calendar(db_session, calendar):
"""
Delete the calendar after deleting its events in batches.
Note we deliberately do not rely on the configured delete cascade -- doing
so for a calendar with many events can result in the session post-flush
processing (Transaction record creation) blocking the event loop.
"""
count = 0
for e in calendar.events:
db_session.delete(e)
count += 1
if count % 100 == 0:
# Issue a DELETE for every 100 events.
# This will ensure that when the DELETE for the calendar is issued,
# the number of objects in the session and for which to create
# Transaction records is small.
db_session.commit()
db_session.commit()
# Delete the calendar
db_session.delete(calendar)
db_session.commit()
is fetching all the calendar data, which is inefficient and causes timeouts.

We should use DELETE with a LIMIT until there are no events (~1s for a batch size of 1K, ~4s for a batch size of 10K).

DELETE FROM event WHERE calendar_id=XXXX LIMIT 1000;

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions