Skip to content

Commit

Permalink
Prevent a subquery and full table scan on User.number_of_edits, reduc…
Browse files Browse the repository at this point in the history
…ing average time from 16s to <=4ms.
  • Loading branch information
TkTech committed Feb 3, 2017
1 parent fa9fe3d commit 452acfb
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions ckan/model/user.py
Expand Up @@ -7,7 +7,7 @@
from passlib.hash import pbkdf2_sha512
from sqlalchemy.sql.expression import or_
from sqlalchemy.orm import synonym
from sqlalchemy import types, Column, Table
from sqlalchemy import types, Column, Table, func
import vdm.sqlalchemy

import meta
Expand Down Expand Up @@ -192,9 +192,20 @@ def as_dict(self):
def number_of_edits(self):
# have to import here to avoid circular imports
import ckan.model as model
revisions_q = meta.Session.query(model.Revision)
revisions_q = revisions_q.filter_by(author=self.name)
return revisions_q.count()

# Get count efficiently without spawning the SQLAlchemy subquery
# wrapper. Reset the VDM-forced order_by on timestamp.
return meta.Session.execute(
meta.Session.query(
model.Revision
).filter_by(
author=self.name
).statement.with_only_columns(
[func.count()]
).order_by(
None
)
).scalar()

def number_created_packages(self, include_private_and_draft=False):
# have to import here to avoid circular imports
Expand Down

0 comments on commit 452acfb

Please sign in to comment.