Skip to content

Commit

Permalink
Using SQL to reduce overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
coleifer committed Jul 7, 2011
1 parent 6b069ac commit 83b57ff
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions djutils/dashboard/commands.py
@@ -1,6 +1,7 @@
import datetime

from django.conf import settings
from django.db import connection

from djutils import dashboard
from djutils.dashboard.models import Panel, PanelData, PanelDataSet, PANEL_AGGREGATE_MINUTE
Expand Down Expand Up @@ -34,14 +35,29 @@ def remove_old_panel_data():
if EXPIRATION_DAYS:
cutoff = datetime.datetime.now() - datetime.timedelta(days=EXPIRATION_DAYS)

# try deleting these first so as not to load up the subsequent
# delete while collecting sub-objects
PanelDataSet.objects.filter(
panel_data__created_date__lte=cutoff,
panel_data__aggregate_type=PANEL_AGGREGATE_MINUTE,
).delete()

PanelData.objects.filter(
created_date__lte=cutoff,
aggregate_type=PANEL_AGGREGATE_MINUTE,
).delete()
cursor = connection.cursor()

data_set_table = PanelDataSet._meta.db_table
data_table = PanelData._meta.db_table

query = '''
DELETE FROM %s WHERE id IN (
SELECT dpd.id FROM %s AS dpd
INNER JOIN %s AS dp
ON dpd.panel_data_id = dp.id
WHERE (
dp.aggregate_type = %%s AND
dp.created_date <= %%s
)
);
''' % (data_set_table, data_set_table, data_table)

cursor.execute(query, [PANEL_AGGREGATE_MINUTE, cutoff])

query = '''
DELETE FROM %s WHERE
aggregate_type = %%s AND
created_date <= %%s;
''' % (data_table)

cursor.execute(query, [PANEL_AGGREGATE_MINUTE, cutoff])

0 comments on commit 83b57ff

Please sign in to comment.