Django orm-tips
Upcoming SlideShare
Loading in...5
×
 

Django orm-tips

on

  • 3,144 views

 

Statistics

Views

Total Views
3,144
Views on SlideShare
2,330
Embed Views
814

Actions

Likes
2
Downloads
39
Comments
0

7 Embeds 814

http://www.codexn.com 601
http://birdhouse.org 147
http://codexn.com 53
http://tarequeh.com 9
http://webcache.googleusercontent.com 2
http://www.linkedin.com 1
http://www.inoreader.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Django orm-tips Django orm-tips Presentation Transcript

  • Django ORM Tips Tareque Hossain
  • values_list, only & select_related
  • Use context
    • Templates have known performance weaknesses
    • Know your templates (what data/ variables are in use?) and prepare data in view
    • Context is passed between views and templates for a reason
    • Minimize queryset evaluations and ORM calls in templates
    • Template tags are nice, but will slow you down if you are making ORM calls
  • Use count()
    • len(QuerySet) is not a good idea
    • Evaluating full querysets for boolean values is wasteful
    • Don’t retrieve objects if you don’t use them
    • Use count and save it if you plan to use it later
  • Indices
    • Index will not magically solve your problem
    • Creating index requires a lot of space and may be expensive
    • DB will read ALL index data in addition to reading the table data
    • So don’t just go around creating index for each field in your model
    • Fields (columns) that will have nearly unique values are better candidates (pk/ unique auto)
    • Which fields appear in your filters most?
  • Order By
    • Looks naïve, but the DB will go crazy trying to sort your data
    • Indices help in increasing order_by performance significantly
    • Indices may not be used for sorting if you order by multiple fields even if they’re indices
    • Indices may not be used for sorting if you mix ascending and descending filters
  • That’s it!
    • @tarequeh
    • @pbs
    • codexn.com