Skip to content

Commit

Permalink
Optimized slow query SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
m431m committed Dec 9, 2020
1 parent aa774fc commit dbdfaa0
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions ckan/model/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ def get_for_resource(cls, url):

@classmethod
def get_total_download(cls, package_id):
url = 'dataset/%s/resource/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}/download' % package_id
url = '%dataset/{package_id}/resource/%/download%'.format(package_id=package_id)
query = text("""
SELECT SUM(running_total)::int AS total_download FROM (
SELECT DISTINCT ON (url) running_total, tracking_date
FROM tracking_summary WHERE url ~* :url ORDER BY url, tracking_date DESC) AS sub;
FROM tracking_summary WHERE url LIKE :url ORDER BY url, tracking_date DESC) AS sub;
""")

result = meta.Session.execute(query, {'url': url}).fetchone()
Expand Down

0 comments on commit dbdfaa0

Please sign in to comment.