zoukankan      html  css  js  c++  java
  • SQL 优化案例之变更表结构

    从慢日志报表中看到一条很长的SQL

    select 
      id 
    from
      myinfo 
    WHERE 1 = 1 
      and ((
        SUBSTRING_INDEX(location_axis, '$', 3) like concat('%$', 2334) 
        or SUBSTRING_INDEX(location_axis, '$', 3) like concat(concat('%$', 2334), '$%')
      ) 
      or (
        SUBSTRING_INDEX(location_axis, '$', 3) like concat('%$', 2337) 
        or SUBSTRING_INDEX(location_axis, '$', 3) like concat(concat('%$', 2337), '$%')
      ) 
      or (
        SUBSTRING_INDEX(location_axis, '$', 3) like concat('%$', 2340) 
        or SUBSTRING_INDEX(location_axis, '$', 3) like concat(concat('%$', 2340), '$%')
      ) 
      or (
        SUBSTRING_INDEX(location_axis, '$', 3) like concat('%$', 2353) 
        or SUBSTRING_INDEX(location_axis, '$', 3) like concat(concat('%$', 2353), '$%')
      ) 
      or (
        SUBSTRING_INDEX(location_axis, '$', 3) like concat('%$', 2367) 
        or SUBSTRING_INDEX(location_axis, '$', 3) like concat(concat('%$', 2367), '$%')
      ) 
      or ......

    询问开发得知是通过程序拼成的sql

    表中存入的location_axis是区域信息,存三个区域,用$分隔,如 $1$2$3,可以看出要实现的是匹配值是否在location_axis中的第一个域或第二个域中

    匹配的值是从另一个表中根据条件获取的如:

    select it_area_id itinfo where dep_id='0111'

    问题:繁琐、前缀为%无法使用索引,导致比较慢

    优化方式:

    将第一个地域编码和第二个分别存在单独的列中,然后和表it_area_id进行关联查询

    变更表结构

    alter table myinfo add first_axis varchar(10),add sec_axis varchar(10);
    update myinfo t1 inner join (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(location_axis,'$',2),'$',-1) as c1 from itinfo)t2 using(id) set t1.first_axis=t2.c1; 
    update myinfo t1 inner join (select id,SUBSTRING_INDEX(SUBSTRING_INDEX(location_axis,'$',3),'$',-1) as c1 from itinfo)t2 using(id) set t1.sec_axis=t2.c1;   
    

    查询语句变为

    select opr_bo_id from myinfo t1 inner join itinfo t2 on t1.first_axis=t2.it_area_id or t1.sec_axis=t2.it_area_id where t2.dep_id='0111';

    结果达到毫秒级别

  • 相关阅读:
    关于echarts
    es6数组相关操作
    日期格式化(超实用)
    vue对象深拷贝(避免对象赋值,改变一个对象的值,另一个对象也变化)
    vue在js公用文件中使用this
    VUE百度地图API调用(手机端、PC端、微信通用)
    promise与async/await
    手机端软键盘弹出又收回后,页面下方留空白、样式定位错乱或不能滚动
    移动端表格(固定首列及表头)
    VUE面试知识整理
  • 原文地址:https://www.cnblogs.com/Bccd/p/7846081.html
Copyright © 2011-2022 走看看