The tables created by django for MtM fields are unordered.  Loading all the 
relationships may result in a number of I/O operations approaching the 
number of relationships:

# select * from basetable;
 id |basetable_id| mtm_id 
----+------------+----------
  6 |          3 |        1
  7 |          2 |        2
 15 |          1 |        1
 16 |          1 |        2
 18 |          3 |        5


Postgres has a feature to 'defragment' such tables packing data that gets 
accessed at the same time into the same disk blocks.  The number of I/O 
operations can be as small as one:

# \pset pager off
# cluster basetable using table_mem_id;
# select * from basetable;

 id |basetable_id| mtm_id 
----+------------+----------
 15 |          1 |        1
 16 |          1 |        2
  7 |          2 |        2
  6 |          3 |        1
 18 |          3 |        5

On a large table this can put hundreds of MtM relationships sequentially on 
disk.  This makes for less I/O operations and less cache space used.

----
This feature is postgres specific.
And it would involve django making a guess about the use pattern of the 
relationship.  Either the forward or reverse is optimized.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to