2

I have a somewhat complex database in django. It makes extensive use of partitioned tables. I have had some problems before with django and partitioned tables but so far I have found satisfactory solutions to all of my problems.

My newest Problem concerns a foreign key relationship between two partitioned tables. I have a model called Event with a submodel called MonitorEvent that houses some additional information for this type of events. The, for my question, important fields of the models are:

class Event(models.Model):
    id PK, (automatically added)
    ts timestamp

class MonitorEvent(Event):
    event_ptr_id FK -> Event (automatically added)
    ts_copy timestamp

Both tables are partitioned by month using the timestamp. The timestamp, however is not part of the PK, since django does not allow multi-field PKs. (I have tried using joined PK fields without success Stackoverflow Question) Therefore I need a copy of the timestamp in the subclass to allow for a partitioned table of the subclass.

Now to my problem: I would usually select all Events and MonitorEvents by executing:

Event.objects.all().select_related('monitorevent')

(I would, of course, add some filters as to not select all available events.) To my understanding this would result in a SQL Query like:

SELECT * FROM event LEFT JOIN monitorevent ON event.id = monitorevent.event_ptr_id;

which should join all event table partitions with all monitorevent table partitions. I would actually want something like:

SELECT * FROM event LEFT JOIN monitorevent ON event.id = monitorevent.event_ptr_id AND event.ts = monitorevent ts_copy;

which should allow the query planner to make use of the partition scheme to speed up the query.

Is there a possibility in django to write a custom Manager that modifies the default select_related to include an additional field in the ON condition of the query?

Another possibility for me would be to write a custom raw query that does the select_related manually, however according to this question Django select related in raw request this does not seem to be possible except from the manual proposition made in the question.

Of course I could also add the additional fields to the original event model and delete the submodel which would eliminate the problem altogether, but that would be my least favorite solution.

I am using Django 1.5 in combination with a postgres DB.

Thank you for your suggestions.

Community
  • 1
  • 1
Tim
  • 1,272
  • 11
  • 28

0 Answers0