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

Thanks for flagging this SlideShare!

Oops! An error has occurred.

Django orm-tips

  • 2,645 views
Published

 

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,645
On SlideShare
0
From Embeds
0
Number of Embeds
5

Actions

Shares
Downloads
39
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Django ORM Tips Tareque Hossain
  • 2. values_list, only & select_related
  • 3. 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
  • 4. 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
  • 5. 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?
  • 6. 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
  • 7. That’s it!
    • @tarequeh
    • @pbs
    • codexn.com