查询语句有 or 会导致查询速度变慢问题解决

先上查询语句

select u.c_user_id      用户编号,
       u.c_old_user_id  旧编号,
       u.c_user_name    用户名称,
       u.c_user_address 用户地址,
       日志说明         日志说明,
       操作人           操作人,
       操作时间         操作时间,
       备注原因         备注原因
  from yx_user u,
       (select regexp_substr(substr(l.c_log, 1, 13), '[0-9]*[0-9]', 1) 用户编号,
               l.c_log 日志说明,
               q.c_user_name 操作人,
               l.d_log_operation_time 操作时间,
               l.c_log_remark 备注原因
          from yx_log l, qx_systemuser q
         where q.n_systemuser_id = l.n_log_operator_id
           and l.d_log_operation_time between
               to_date('2020/9/26 0:00:00', 'yyyy-MM-dd hh24:mi:ss') and
               to_date('2020/10/27 23:59:59', 'yyyy-MM-dd hh24:mi:ss')
           and l.n_log_name_id = 3
           and l.n_log_operator_id = 1) w
 where (u.c_user_id = w.用户编号 or u.c_old_user_id = w.用户编号)
 order by 操作时间 desc

  上面查询sql 只有两会在那个表  u 和 w ,w 里面的sql 没有出现查询慢的情况。

(u.c_user_id = w.用户编号 or u.c_old_user_id = w.用户编号) ;

上面条件 使得查询语句贼慢;

优化如下:

(case when length(u.c_user_id) =13 then  u.c_user_id  else  u.c_old_user_id  end = w.用户编号 )

优化以后可以说是差不出来数据和秒出的区别

数据量 u表是 主表 大概有几万条数据 w表是临时表 应该就百十来条数据; 第一条查询条件个人理解就是遍历查询两次 第二条 就是遍历查询一次;

深层理解 请参考:https://www.cnblogs.com/kerrycode/p/11911998.html

做个记录;理解偏差很大 ;

以后弥补

 2020-12-22

今天查询发现上面优化存在错误:

(case when length(u.c_user_id) =13 then  u.c_user_id  else  u.c_old_user_id  end = w.用户编号 )  这里条件应该是 : (case when length(w.用户编号) =13 then  u.c_user_id  else  u.c_old_user_id  end = w.用户编号 )  ;

运行修改后的sql 感觉和优化前的(u.c_user_id = w.用户编号 or u.c_old_user_id = w.用户编号) 没有区别;

进过多次测试最后 将用户编号 直接在W表查询时如果有8位的编号直接用8位的找到13位的编号;

 case
                 when length(regexp_substr(substr(l.c_log, 1, 13),
                                           '[0-9]*[0-9]',
                                           1)) = 13 then
                  regexp_substr(substr(l.c_log, 1, 13), '[0-9]*[0-9]', 1)
                 else
                  (select c_user_id
                     from yx_user
                    where c_old_user_id = regexp_substr(substr(l.c_log, 1, 13),
                                                        '[0-9]*[0-9]',
                                                        1))
               end 用户编号

条件直接关联    where u.c_user_id  = w.用户编号  虽说有点慢  但是能接受

 

posted @ 2020-10-27 10:26  求路问道  阅读(1037)  评论(0编辑  收藏  举报