Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Advanced Django ORM techniques
Upcoming SlideShare
Loading in...5
×

Advanced Django ORM techniques

27,889

Published on

Django's ORM is extremely powerful, allowing you to manage your data without ever going near a line of SQL and hiding a multitude of complexities. But its power can sometimes be a curse rather than a blessing, multiplying queries without your knowledge and bringing your database to its knees.

In this session I explain what's going on behind the scenes and present some techniques to make your ORM use more efficient, showing how to monitor what's going on and how to better deal with relationships, indexes and more.

This talk was presented at Europython 2010 in Birmingham.

Published in: Technology
2 Comments
34 Likes
Statistics
Notes
No Downloads
Views
Total Views
27,889
On Slideshare
0
From Embeds
0
Number of Embeds
21
Actions
Shares
0
Downloads
306
Comments
2
Likes
34
Embeds 0
No embeds

No notes for slide

  • (background: montage of Limmud, rosemanblog, Capital, Classic, Heart, GlassesDirect)

  • Some of same ideas in Guido's Appstats talk this morning




  • It's a model, in a field, geddit?
  • For more, see Marty Alchin, Pro Django (Apress)
  • descriptors used especially in related objects - see later
  • Very useful for introspection and working out what's going on
  • explain identity: multiple instances relating to same model row aren't the same object, changes made to one don't reflect the other; even saving one with new values won't be reflected in others.
  • Update, Aggregates, Q, F


  • Find repeated queries with my branch of the django-debug-toolbar, or SimonW's original query debug middleware



  • Actually in 1.2 there's an extra _state object in __dict__, which is used for the multiple DB support (which I'm not covering here).


  • Lack of model identity means that accessing the related item on one instance does not cause cache to be created on other instances that might reference the same db row


  • Note: backwards cache does work on OneToOne as of 1.2












  • +----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
    | 1 | PRIMARY | sandy_bar | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
    | 2 | DEPENDENT SUBQUERY | U0 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |
    +----+-----------+----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
    | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
    +----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
    | 1 | PRIMARY | sandy_bar | ALL | NULL | NULL | NULL | NULL | 100 | Using where |
    | 2 | DEPENDENT SUBQUERY | U0 | unique_subquery | PRIMARY | PRIMARY | 4 | func | 1 | Using where |
    +----+--------------------+-----------+-----------------+---------------+---------+---------+------+------+-------------+

    --------+-----------+-----------------+---------------+---------+---------+------+------+-------------+
























    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×