mysql优化查询的方式 优化查询的方式

一、假设有三张表

复制代码
 
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
复制代码

二、 需求:获取2017-11-11所有预定信息:

打印:用户名称,会议室名称, 预定时间段

复制代码
# 解决方案一:执行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,这只是对于跨表做的优化,如果是单表的话就没有必要进行优化查询了

那么什么时候用selsect_releated,什么时候用prefetch_releated呢?这个按情况而定,

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

prefetch_releated不连表执行3次SQL

二、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
复制代码

一、假设有三张表

复制代码
 
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
复制代码

二、 需求:获取2017-11-11所有预定信息:

打印:用户名称,会议室名称, 预定时间段

复制代码
# 解决方案一:执行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,这只是对于跨表做的优化,如果是单表的话就没有必要进行优化查询了

那么什么时候用selsect_releated,什么时候用prefetch_releated呢?这个按情况而定,

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

prefetch_releated不连表执行3次SQL

二、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
复制代码

猜你喜欢

转载自www.cnblogs.com/maaosheng/p/11619175.html