未加星标

Django Tips #16 Simple Database Access Optimizations

字体大小 | |
[开发(python) 所属分类 开发(python) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

So, I just wanted to share a few straightforward tips about database access optimization. For the most those tips are not something you should go to your code base and start refactoring. Actually for existing code it’s a better idea to profile first (with Django Debug Toolbar for example). But those tips are very easy to start applying, so keep them in mind when writing new code!

Accessing Foreign Key Values

If you only need the ID of the Foreign Key:

Do

post.author_id

Don't

post.author.id

If you have a foreign key named author , Django will automatically store the primary key in the property author_id , while in the author property will be stored a lazy database reference. So if you access the id via the author instance, like this post.author.id , it will cause an additional query.

Bulk Insert on Many to Many Fields

Do

user.groups.add(administrators, managers)

Don't

user.groups.add(administrators) user.groups.add(managers)

Counting QuerySets

If you only need the count:

Do

users = User.objects.all() users.count() # Or in template... {{ users.count }}

Don't

users = User.objects.all() len(users) # Or in template... {{ users|length }}

Empty QuerySets

If you only need to know if the queryset is empty:

Do

groups = Group.objects.all() if groups.exists(): # Do something...

Don't

groups = Group.objects.all() if groups: # Do something...

Reduce Query Counts

Do

review = Review.objects.select_related('author').first() # Select the Review and the Author in a single query name = review.author.first_name

Don't

review = Review.objects.first() # Select the Review name = review.author.first_name # Additional query to select the Author

Select Only What You Need

Let’s say the Invoice model has 50 fields and you want to create a view to display just a summary, with the number , date and value :

Do

# views.py # If you don't need the model instance, go for: invoices = Invoice.objects.values('number', 'date', 'value') # Returns a dict # If you still need to access some instance methods, go for: invoices = Invoice.objects.only('number', 'date', 'value') # Returns a queryset # invoices.html <table> {% for invoice in invoices %} <tr> <td>{{ invoice.number }}</td> <td>{{ invoice.date }}</td> <td>{{ invoice.value }}</td> </tr> {% endfor %} </table>

Don't

# views.py invoices = Invoice.objects.all() # invoices.html <table> {% for invoice in invoices %} <tr> <td>{{ invoice.number }}</td> <td>{{ invoice.date }}</td> <td>{{ invoice.value }}</td> </tr> {% endfor %} </table>

Bulk Updates

Do

from django.db.models import F Product.objects.update(price=F('price') * 1.2)

Don't

products = Product.objects.all() for product in products: product.price *= 1.2 product.save()

本文开发(python)相关术语:python基础教程 python多线程 web开发工程师 软件开发工程师 软件开发流程

主题: Django
分页:12
转载请注明
本文标题:Django Tips #16 Simple Database Access Optimizations
本站链接:http://www.codesec.net/view/479905.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(python) | 评论(0) | 阅读(49)