MySQL的去重复优化

MySQL的去重复优化
在说去重复优化之前,我必须对fetch和duration这两个名词进行解读一下
mysql workbench中fetch和duration分别是什么意思
fetch:
它是指SQL在网络传输的过程中所用的时间,但不是SQL执行的时间
duration:
它指的是SQL语句执行的时间
下面有三种不同的SQL语句,最终的查询结果是一样的,但是SQL的执行时间不同
第一种:

select r.username,r.userid,r.photo from user r,
  (select t.userid from electricdevice natural join lessonuse u natural join lessonform right join tickup t on t.lessonuseid=u.lessonuseid  where edeviceip='192.168.10.154' and date_sub(timestart,interval '00:15:00' hour_second)<=Now() and timeend>=Now()
  and date_add(timestart,interval '00:15:00' hour_second) >= tickuptime and date_sub(timestart,interval '00:15:00' hour_second) <= tickuptime) tt where r.userid=tt.userid;

查询结果如下:
在这里插入图片描述
在这里插入图片描述
第二种:

select distinct(r.userid),r.username,r.photo from user r,
  (select t.userid from electricdevice natural join lessonuse u natural join lessonform right join tickup t on t.lessonuseid=u.lessonuseid  where edeviceip='192.168.10.154' and date_sub(timestart,interval '00:15:00' hour_second)<=Now() and timeend>=Now()
  and date_add(timestart,interval '00:15:00' hour_second) >= tickuptime and date_sub(timestart,interval '00:15:00' hour_second) <= tickuptime) tt where r.userid=tt.userid;

结果如下:
在这里插入图片描述
在这里插入图片描述
第三种:

select r.userid,r.username,r.photo from user r,
  (select distinct(t.userid) from electricdevice natural join lessonuse u natural join lessonform right join tickup t on t.lessonuseid=u.lessonuseid  where edeviceip='192.168.10.154' and date_sub(timestart,interval '00:15:00' hour_second)<=Now() and timeend>=Now()
  and date_add(timestart,interval '00:15:00' hour_second) >= tickuptime and date_sub(timestart,interval '00:15:00' hour_second) <= tickuptime) tt where r.userid=tt.userid;

在这里插入图片描述
总结:
在MySQL的SQL查询的过程中distinct去重的时候,如果能够在子查询中直接过滤的话,那么在子查询中过滤,因为这种情况下SQL的执行效率最高。

你可能感兴趣的:(MySQL)