泽桐-Merlin

They built goals around their dreams and nerver quit.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
	- 性能相关
		user_list = models.UserInfo.objects.all()
			for row in user_list:
				# 只去取当前表数据
	
		select_related,主动连表查询【FK】
			
			user_list = models.UserInfo.objects.all().select_related('FK字段')
			for row in user_list:
				# 只去取当前表数据和FK表关联字段
		
		
			user_list = models.UserInfo.objects.values(...)
			for row in user_list:
				# 只去取当前表数据和FK表关联字段
		
			==》 连表降低性能
	
		prefetch_related
			user_list = models.UserInfo.objects.all().prefetch_related('FK字段')
	
			# [obj,obj,obj]
			# 1.查询用户表models.UserInfo.objects.all() 1000 select * from UserInfo ;
			# 2.把用户表中所有的ut_id拿到, 用户类型ID [1,2,3]  select * from UserType where id in [1,2,3]
			# 3.把用户表中所有的xx_id拿到, 用户类型ID [21,21,31]   select * from xx where id in [21,21,31]

			user_list = models.UserInfo.objects.all().prefetch_related('ut','xx')
			for row in user_list:
				print(row.name, row.pwd, row.ut.caption)
		补充:
			# [obj,obj,obj]
			# user_list = models.UserInfo.objects.all().only('name')   # 只取某个字段 select name from userinfo 
			# user_list = models.UserInfo.objects.all().defer('name')  # 排除当前字段	
			# for row in user_list:
			#     print(row.pwd)
	

  

posted on 2018-04-02 16:13  泽桐-Merlin  阅读(187)  评论(0编辑  收藏  举报