1、假設有三張表sql
Room
id 1 2 .. 1000 User: id 1 .. 10000 Booking: user_id room_id time_id date 1 1 8:00 2017-11-11 1 2 8:00 2017-11-11 1 3 8:00 2017-11-11 1 4 8:00 2017-11-11 1 5 8:00 2017-11-11
2、 需求:獲取2017-11-11全部預約信息:session
打印:用戶名稱,會議室名稱, 預約時間段app
# 解決方案一:執行11次sql語句 bk = models.Booking.objects.filter(date=2017-11-11) for item in bk: print(item.time_id, item.room.caption, item.user.user) # 解決方案二:執行1次 #select * from ... left join user ... join room bk = models.Booking.objects.filter(date=2017-11-11).select_related('user','room') for item in bk: print(item.time_id, item.room.caption, item.user.user) # 解決方案三:執行3次 #select * from booking where date=2017-11-11 #select * from user where id in [1,] #select * from room where id in [1,2,3,4,5] bk = models.Booking.objects.filter(date=2017-11-11).prefetch_related('user','room') for item in bk: print(item.time_id, item.room.caption, item.user.user)
總結:之後對於SQL語句的優化要加上selsect_releated或者prefetch_releated,這只是對於跨表作的優化,若是是單表的話就沒有必要進行優化查詢了post
那麼何時用selsect_releated,何時用prefetch_releated呢?這個按狀況而定,fetch
selsect_releated是主動連表,執行一次SQL優化
prefetch_releated不連表執行3次SQLcode
2、Q查詢的第二種方式對象
remove_booking = Q() for room_id, time_id_list in booking_info['del'].items(): for time_id in time_id_list: temp = Q() #實例化一個Q對象 temp.connector = 'AND' #以and的方式鏈接 # user_id是一個字段,後面的是一個字段對應的值 temp.children.append(('user_id', request.session['user_info']['id'],)) temp.children.append(('booking_date', booking_date,)) temp.children.append(('room_id', room_id,)) temp.children.append(('booking_time', time_id,)) remove_booking.add(temp, 'OR') #以or的方式添加到temp