← Back to team overview

launchpad-dev team mailing list archive

performance tuesday - Reference vs Int columns

 

Todays note about performance is a note about some storm/sqlobject glue.

Code like this:
...
if bugtask.distribution is None:
    ...

is probably wrong: it should be

...
if bugtask.distributionID is None:
    ...

I say this because the former code will trigger late evaluation of the
related distribution object *which we then don't use*. The second form
won't trigger late evaluation, and thus is faster / can run with
narrower DB permissions.

The underlying cause for this is the difference between
ReferenceColumn and IntColumn in storm.

In 'raw' storm, we create *two* attributes on a model table - something like:

distribution_id = IntColumn('distribution', ...)
distribution = Reference(table='distribution', foreign_key=distributribution_id)

But in storm.sqlobject we use some magic, and instead do:
distribution = ForeignKey(dbname=distribution, foreignkey='Distribution')

which creates a ReferenceColumn distribution *and* an IntColumn distributionID.

Accessing .distribution will *always* trigger a DB query against the
Distribution table. Accessing .distributionID will *never* trigger
such a query.

The rule of thumb is : unless you *want* the distribution object
itself, never access the attribute.

I mention this because just last night another case of this confusion
caused a bug (which I think JTV has a fix for landing already). I've
seen several cases of unnecessary late evaluation caused by this
distinction not being honoured.

-Rob



Follow ups