数据库优化查询方式

数据库优化查询方式:
获取某一天的预定 current_date
= "2017-11-11" current_date = datetime.datetime.strptime(current_date, '%Y-%m-%d').date() # 查询 # select * from booking models.booking.objects.filter(date=current_date) # 主动连表(连表查询有性能损耗)PS:大公司用空间换时间 # select * from booking left join user on user.id = booing.user_id ... models.booking.objects.filter(date=current_date).select_related('user','room') # 不连表 # select * from booing # 获取booking中取到的所有 user_id_list # select * from user where id in user_id_list models.booking.objects.filter(date=current_date).prefetch_related('user','room') models.booking.objects.filter(date=current_date).select_related('user','room')

优化查询的两种方式:(1):prefetch_related

                                 (2):select_related

selsect_releated是主动连表,执行一次SQL

prefetch_releated不连表执行3次SQL

 

posted @ 2017-12-13 08:34  孟庆健  阅读(193)  评论(0编辑  收藏  举报