First, assume that there are three tables
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
Second, demand: 2017-11-11 get all reservation information:
Print: user name, the name of the conference room, the predetermined time period
# Solution one: Perform 11 times sql statement BK = models.Booking.objects.filter (DATE = 2017-11-11 ) for Item in BK: Print (item.time_id, item.room.caption, item.user.user ) # solution two: perform 1 # the SELECT * from the User ... ... left the Join the Join Room BK = models.Booking.objects.filter (DATE = 2017-11-11) .select_related ( ' the User ' , ' Room ' ) for Item in BK: Print (item.time_id, item.room.caption, item.user.user) # solution three: perform 3 times # the SELECT * 010-51287744, from the 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)
Summary: After optimizing the SQL statements to add selsect_releated or prefetch_releated, this is only done for optimization across the table, if it is, then there is no need to single-table queries to optimize the
So when to use selsect_releated, when to use prefetch_releated it? This, as the case may be,
selsect_releated is active even table, perform a SQL
prefetch_releated not even perform three times a SQL table
Second, the second way Q query
= remove_booking Q () for room_id, time_id_list in booking_info [ ' del ' ] .items (): for TIME_ID in time_id_list: TEMP = Q () Q # instantiates an object is temp.connector = ' the AND ' # in a way to connect and # user_id is a field, the latter is a corresponding field value temp.children.append (( ' user_id ' , makes 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
First, assume that there are three tables
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
Second, demand: 2017-11-11 get all reservation information:
Print: user name, the name of the conference room, the predetermined time period
# Solution one: Perform 11 times sql statement BK = models.Booking.objects.filter (DATE = 2017-11-11 ) for Item in BK: Print (item.time_id, item.room.caption, item.user.user ) # solution two: perform 1 # the SELECT * from the User ... ... left the Join the Join Room BK = models.Booking.objects.filter (DATE = 2017-11-11) .select_related ( ' the User ' , ' Room ' ) for Item in BK: Print (item.time_id, item.room.caption, item.user.user) # solution three: perform 3 times # the SELECT * 010-51287744, from the 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)
Summary: After optimizing the SQL statements to add selsect_releated or prefetch_releated, this is only done for optimization across the table, if it is, then there is no need to single-table queries to optimize the
So when to use selsect_releated, when to use prefetch_releated it? This, as the case may be,
selsect_releated is active even table, perform a SQL
prefetch_releated not even perform three times a SQL table
Second, the second way Q query
= remove_booking Q () for room_id, time_id_list in booking_info [ ' del ' ] .items (): for TIME_ID in time_id_list: TEMP = Q () Q # instantiates an object is temp.connector = ' the AND ' # in a way to connect and # user_id is a field, the latter is a corresponding field value temp.children.append (( ' user_id ' , makes 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