Skip to content

Commit

Permalink
Added some pseudocode indicating a possible performance improvement. …
Browse files Browse the repository at this point in the history
…Removed use_for_related_fields = True from the manager, which I think is a vestige from old code that doesn't actually make sense anymore.
  • Loading branch information
debrouwere committed Mar 5, 2011
1 parent d52da90 commit b256b91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions docs/index.rst
Expand Up @@ -31,6 +31,11 @@ The models and API work with both single-table models and `joined tables <http:/

See :doc:`models` for more detail.

Specifying your own primary key
-------------------------------

* working with UUIDs; comparators -- e.g. a "created" date (but beware of programmatical creation, resulting in multiple objects with the same created date and thus no canonical "latest" revision)

Admin integration
-----------------

Expand Down
30 changes: 30 additions & 0 deletions performance.txt
@@ -0,0 +1,30 @@
# query

where = '{comparator_table}.{comparator} = (SELECT MAX({comparator}) FROM {table} as sub WHERE {table}.cid = sub.cid)'

# optimized query

where = '{comparator_table}.{pk} IN {comparator_table}_latest_revisions'

# in the code

# optimize by default
if not getattr(settings, 'OPTIMIZE_REVISIONS', True):
where
else:
optimized_where

# post-syncdb

for model in versioned_models:
base = model.get_base_model()
table = base._meta.db_table + "_latest_revisions"
pk = model().pk_name
latest = str(model.latest.only(pk).query)
sql = "CREATE OR REPLACE VIEW {table} AS {selection}".format(
table=table,
selection=latest
)

# NOTE TO SELF: create a script that enters ~20k records into a DB and test performance before committing this
# If this still isn't fast enough, we could actually go for a real, indexed table, but then we have to take care of keeping the _latest_revisions table in sync with the real table, which isn't rocket science but which I'd rather avoid.
5 changes: 1 addition & 4 deletions revisions/managers.py
Expand Up @@ -21,10 +21,7 @@ def count(self):

class LatestManager(models.Manager):
""" A manager that returns the latest revision of each bundle of content. """
# use_for_related_fields makes sure this manager works
# seamlessly with inline formsets
use_for_related_fields = True


@property
def current(self):
qs = LatestQuerySet(self.model, using=self._db)
Expand Down

0 comments on commit b256b91

Please sign in to comment.