冲高级——篇三 之 查询SQL优化 (后续继续添加)

Mysql高级篇(中)—— SQL优化 本篇博文主要介绍了MySQL中的SQL优化思路、以及关联查询、子查询、分组排序相关优化内容;此外针对exists的理解,单路排序和多路排序的概念做了简述。 阅读详情

由于公司业务较复杂,以下是工作一年来遇到的sql优化经验,下面只是我能记住的部分

1.首先应尽量避免全表扫描,常用查询条件字段或者排序字段应为其添加索引

2.建表时应尽量给数值字段设定默认值,如 0 

select id from t where num is null 这样查

select id from t where num=0这样查

第一种情况就会索引失效

那么关于索引失效的情况,总结下:

查询的时候要考虑到where子句和order by子句的索引,如果在where子句中出现了<,>,<=,>=,!=,<>,is null,is nit null,算术运算,like '%'第一个为%的,会触发全表扫描

3.如果能确定两个结果集中不会有重复数据的情况下 使用union all 而不使用 union

4.应尽量避免在 where 子句中使用 or 来连接条件,否则将导致引擎放弃使用索引而进行全表扫描,如: select id from t where num=10 or num=20 可以这样查询: select id from t where num=10 union all select id from t where num=20

5.关于聚集索引

 create index IDerX_01_C_CODE on C_CODE(OLD_TABLE);4.关于多条件查询

 create index IDerX_02_C_CODE on C_CODE(OLD_ID);
 SELECT * FROM c_code WHERE old_table like 'TI%' AND old_id like '33%' ;  --45.436
 DROP INDEX IDerX_01_C_CODE;
DROP INDEX IDerX_02_C_CODE;
create index IDX_01_C_CODE on C_CODE (OLD_TABLE, OLD_ID)
SELECT * FROM c_code WHERE old_table like 'TI%' AND old_id like '33%' ;  --15.327

 

上面举的是一个我曾经的例子,c_code表有几千万条数据
刚开始是分别给每个字段建立索引
后面改为建立聚集索引
然后速度由45秒多提升到15秒

 

使用聚集索引要注意的点:where后面的第一个查询条件必须是聚集索引的第一个字段
否则聚集索引无效

6.当查询速度很慢时,看看sql里有没有 in 与not in ,将其替换为 exists 与not exists 

7.强制使用索引

当添加某字段条件进行范围性查询时会发现查询突然变得很慢,这个时候查看执行计划若该字段索引没有用到,则使用

force index('index_name'),如下就是摘录的一个例子

 

8.下面贴下入职以来写的最长的sql,如果后续有更长的再替换

总结就是:当查询场景较复杂时应充分了解需求,然后细化到各个模快再union all总和到一起

可加大查询效率

select zz.*,
       'LINAN' LINAN,
       (case
         when zz.zaituu = '有' then
          '有'
         when (select count(0)
                 from c_code cv, c_codeentity cc
                where cv.code_val between zz.STARTCODE and zz.ENDCODE
                  and cc.code_id = cv.id
                  and cc.status = 1) > 0 then
          '有'
         else
          '无'
       end) zaitu
  from (select min(uu.code_val) STARTCODE,
               max(uu.code_val) ENDCODE,
               (max(uu.code_val) - min(uu.code_val) + 1) NUMBER_CAP,
               uu.usingname USINGNAME,
               uu.status STATUS,
               uu.QLOCATIONNAME QLOCATIONNAME,
               uu.enclose ENCLOSE,
               uu.long_function LONG_FUNCTION,
               uu.control_way CONTROL_WAY,
               uu.charge CHARGE,
               uu.using,
               uu.username USERNAME,
               uu.zaituu ZAITUU,
               uu.gid GID,
               uu.ysh YSH,
               uu.node_name NODE_NAME
          from (select ww.code_val,
                       (ww.code_val - row_number()
                        over(partition by ww.usingname,
                             ww.using,
                             ww.status,
                             ww.name,
                             ww.enclose,
                             ww.long_function,
                             ww.control_way,
                             ww.charge,
                             ww.username,
                             ww.zaituu,
                             ww.gid,
                             ww.ysh,
                             ww.node_name order by code_val)) flag,
                       ww.usingname USINGNAME,
                       ww.status STATUS,
                       ww.name QLOCATIONNAME,
                       ww.enclose ENCLOSE,
                       ww.long_function LONG_FUNCTION,
                       ww.control_way CONTROL_WAY,
                       ww.charge CHARGE,
                       ww.username USERNAME,
                       ww.zaituu ZAITUU,
                       ww.using,
                       ww.gid GID,
                       ww.ysh YSH,
                       ww.node_name NODE_NAME
                  from (select c1.code_val,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c1.using) USINGNAME,
                               c1.using,
                               (case
                                 when (select n.name || '-' ||
                                              (select name
                                                 from c_location
                                                where id = n.location_id)
                                         from c_node n
                                        where n.id IN
                                              (SELECT entity_id
                                                 from c_codepoolentity aa
                                                where aa.codepool_id in
                                                      (c1.codepool_id))
                                          and n.nodetype_id = 102061) is not null then
                                  '占用'
                                 else
                                  '空闲'
                               end) STATUS,
                               (select cl.name
                                  from c_location cl
                                 where cl.id = c1.area_id) name,
                               (case
                                 when (select description
                                         from mm_dictionary
                                        where id = c1.lifecycle) = '投入使用' then
                                  '装入'
                                 when (select description
                                         from mm_dictionary
                                        where id = c1.lifecycle) = '工程' then
                                  '未装入'
                               end) ENCLOSE,
                               c1.long_function,
                               c1.control_way,
                               c1.charge,
                               '' USERNAME,
                               '无' zaituu,
                               '' GID,
                               '' YSH,
                               (select n.name || '-' ||
                                       (select name
                                          from c_location
                                         where id = n.location_id)
                                  from c_node n
                                 where n.id IN
                                       (SELECT entity_id
                                          from c_codepoolentity aa
                                         where aa.codepool_id in
                                               (c1.codepool_id))
                                   and n.nodetype_id = 102061) NODE_NAME
                          from c_code c1
                         where c1.codetype_id = 114003
                           and c1.using = f_get_dictionaryid('用途', 2, 'AG')
                           and c1.code_val between 33064000 and 33064999
                        union all
                        select c1.code_val,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c1.using) USINGNAME,
                               c1.using,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c1.servicestatus) STATUS,
                               (select cl.name
                                  from c_location cl
                                 where cl.id = c1.area_id) name,
                               (case
                                 when (select description
                                         from mm_dictionary
                                        where id = c1.lifecycle) = '投入使用' then
                                  '装入'
                                 when (select description
                                         from mm_dictionary
                                        where id = c1.lifecycle) = '工程' then
                                  '未装入'
                               end) ENCLOSE,
                               c1.long_function,
                               c1.control_way,
                               c1.charge,
                               '' USERNAME,
                               '无' zaituu,
                               '' GID,
                               '' YSH,
                               '' NODE_NAME
                          from c_code c1
                         where c1.codetype_id = 114003
                           and (c1.using not in
                               (f_get_dictionaryid('用途', 2, 'AG'),
                                 f_get_dictionaryid('用途', 3, 'PON'),
                                 f_get_dictionaryid('用途', 1, '普通号码')) or
                               c1.using is null)
                           and c1.codetype_id not in (114017, 114019)
                           and c1.code_val between 33064000 and 33064999
                        union all
                        select c2.code_val,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c2.using) USINGNAME,
                               c2.using,
                               (case
                                 when (select n.name || '-' ||
                                              (select name
                                                 from c_location
                                                where id = n.location_id)
                                         from c_node n
                                        where n.id IN
                                              (SELECT entity_id
                                                 from c_codepoolentity aa
                                                where aa.codepool_id in
                                                      (c2.codepool_id))
                                          and n.nodetype_id = 102042) is not null then
                                  '占用'
                                 else
                                  '空闲'
                               end) STATUS,
                               (select cl.name
                                  from c_location cl
                                 where cl.id = c2.area_id) name,
                               (case
                                 when (select description
                                         from mm_dictionary
                                        where id = c2.lifecycle) = '投入使用' then
                                  '装入'
                                 when (select description
                                         from mm_dictionary
                                        where id = c2.lifecycle) = '工程' then
                                  '未装入'
                               end) ENCLOSE,
                               c2.long_function,
                               c2.control_way,
                               c2.charge,
                               '' USERNAME,
                               '无' zaituu,
                               '' GID,
                               '' YSH,
                               (SELECT strcat(name)
                                  FROM c_node cn
                                 where id in (SELECT entity_id
                                                from c_codepoolentity aa
                                               where aa.codepool_id in
                                                     (c2.codepool_id))
                                   and cn.nodetype_id in (102042)) NODE_NAME
                          from c_code c2
                         where c2.codetype_id = 114003
                           and c2.using = f_get_dictionaryid('用途', 3, 'PON')
                           and c2.code_val between 33064000 and 33064999
                        union all
                        select c3.code_val,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c3.using) USINGNAME,
                               c3.using,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c3.servicestatus) STATUS,
                               (select cl.name
                                  from c_location cl
                                 where cl.id = c3.area_id) name,
                               (case
                                 when (select description
                                         from mm_dictionary
                                        where id = c3.lifecycle) = '投入使用' then
                                  '装入'
                                 when (select description
                                         from mm_dictionary
                                        where id = c3.lifecycle) = '工程' then
                                  '未装入'
                               end) ENCLOSE,
                               c3.long_function,
                               c3.control_way,
                               c3.charge,
                               '' USERNAME,
                               '无' zaituu,
                               '' GID,
                               '' YSH,
                               '' NODE_NAME
                          from c_code c3
                         where c3.codetype_id = 114003
                           and c3.using =
                               f_get_dictionaryid('用途', 1, '普通号码')
                           and c3.code_val between 33064000 and 33064999
                        union all
                        select c4.code_val,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c4.using) USINGNAME,
                               c4.using,
                               (select DESCRIPTION
                                  from mm_dictionary
                                 where id = c4.servicestatus) STATUS,
                               (select cl.name
                                  from c_location cl
                                 where cl.id = c4.area_id) name,
                               (case
                                 when (select description
                                         from mm_dictionary
                                        where id = c4.lifecycle) = '投入使用' then
                                  '装入'
                                 when (select description
                                         from mm_dictionary
                                        where id = c4.lifecycle) = '工程' then
                                  '未装入'
                               end) ENCLOSE,
                               c4.long_function,
                               c4.control_way,
                               c4.charge,
                               '' USERNAME,
                               '无' zaituu,
                               '' GID,
                               '' YSH,
                               '' NODE_NAME
                          from c_code c4
                         where c4.code_val between 33064000 and 33064999
                           and not exists
                         (select *
                                  from c_sncodeinfo zz
                                 where zz.startnum <= c4.code_val
                                   and zz.endnum >= c4.code_val)
                           and c4.codetype_id in (114017, 114019)) ww) uu
         group by uu.flag,
                  uu.usingname,
                  uu.using,
                  uu.status,
                  uu.QLOCATIONNAME,
                  uu.enclose,
                  uu.long_function,
                  uu.control_way,
                  uu.charge,
                  uu.username,
                  uu.zaituu,
                  uu.gid,
                  uu.ysh,
                  uu.node_name
        union all
        select si.startnum,
               si.endnum,
               (endnum - startnum + 1) NUMBER_CAP,
               si.usefor,
               '占用',
               (select cl.name
                  from c_location cl
                 where cl.id = si.qlocation_id) name,
               (select (case
                         when cc.lifecycle = 4965 then
                          '装入'
                         else
                          '未装入'
                       end) enclose
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.long_function
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.control_way
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.charge
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.using
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               ct.name username,
               '无' zaituu,
               si.gid,
               to_char(si.group_index_tn_logic) YSH,
               cn.name NODE_NAME
          from c_service         ce,
               c_customer        ct,
               c_servicecustomer sct,
               c_sncodeinfo      si,
               c_serviceentity   cs,
               c_node            cn
         where si.startnum >= 33064000
           and si.endnum <= 33064999
           and ce.id = si.hid
           and si.hid = sct.service_id
           and si.status = 1
           and ct.id(+) = sct.customer_id
           and ce.id = cs.service_id
           and cs.entity_id = cn.id
           and not exists (select sss.*
                  from c_sncodeinfo zz, c_servicecurrent sss
                 where zz.gid = si.gid
                   and sss.id = zz.hid
                   and zz.status = 1)
           and si.usefor in ('30B+D SIP', 'DID SIP')
           and cs.entitytype_id = 102023
        union all
        select si.startnum,
               si.endnum,
               (endnum - startnum + 1) NUMBER_CAP,
               si.usefor,
               '占用',
               (select cl.name
                  from c_location cl
                 where cl.id = si.qlocation_id) name,
               (select (case
                         when cc.lifecycle = 4965 then
                          '装入'
                         else
                          '未装入'
                       end) enclose
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.long_function
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.control_way
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.charge
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.using
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               ct.name username,
               '有' zaituu,
               si.gid,
               to_char(si.group_index_tn_logic) YSH,
               cn.name NODE_NAME
          from c_servicecurrent       ce,
               c_customer             ct,
               c_servicecustomer      sct,
               c_sncodeinfo           si,
               c_servicecurrententity cs,
               c_node                 cn
         where si.startnum >= 33064000
           and si.endnum <= 33064999
           and ce.id = si.hid
           and si.hid = sct.service_id
           and si.status = 1
           and ct.id(+) = sct.customer_id
           and ce.id = cs.service_id
           and cs.entity_id = cn.id
           and not exists (select sss.*
                  from c_sncodeinfo zz, c_service sss
                 where zz.gid = si.gid
                   and sss.id = zz.hid
                   and zz.status = 1)
           and si.usefor in ('30B+D SIP', 'DID SIP')
           and cs.entitytype_id = 102023
        union all
        select si.startnum,
               si.endnum,
               (endnum - startnum + 1) NUMBER_CAP,
               si.usefor,
               '占用',
               (select cl.name
                  from c_location cl
                 where cl.id = si.qlocation_id) name,
               (select (case
                         when cc.lifecycle = 4965 then
                          '装入'
                         else
                          '未装入'
                       end) enclose
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.long_function
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.control_way
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.charge
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.using
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               ct.name username,
               '有' zaituu,
               si.gid,
               to_char(si.group_index_tn_logic) YSH,
               cn.name NODE_NAME
          from c_servicecurrent       ce,
               c_customer             ct,
               c_servicecustomer      sct,
               c_sncodeinfo           si,
               c_servicecurrententity cs,
               c_node                 cn
         where si.startnum >= 33064000
           and si.endnum <= 33064999
           and ce.id = si.hid
           and si.hid = sct.service_id
           and si.status = 1
           and ct.id(+) = sct.customer_id
           and ce.id = cs.service_id
           and cs.entity_id = cn.id
           and exists (select sss.*
                  from c_sncodeinfo zz, c_service sss
                 where zz.gid = si.gid
                   and sss.id = zz.hid
                   and zz.status = 1)
           and si.usefor in ('30B+D SIP', 'DID SIP')
           and cs.entitytype_id = 102023
        union all
        select si.startnum,
               si.endnum,
               (endnum - startnum + 1) NUMBER_CAP,
               si.usefor,
               '占用',
               (select cl.name
                  from c_location cl
                 where cl.id = si.qlocation_id) name,
               (select (case
                         when cc.lifecycle = 4965 then
                          '装入'
                         else
                          '未装入'
                       end) enclose
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.long_function
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.control_way
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.charge
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.using
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               ct.name username,
               '无' zaituu,
               si.gid,
               to_char(si.group_index_tn_logic) YSH,
               (case
                 when (select count(0)
                         from c_serviceservice eg2, c_service cs
                        where eg2.businessglobalserviceid = ce.globalserviceid
                          and eg2.entityglobalserviceid = cs.globalserviceid
                          and cs.servicetype_id in (110132, 110129)) > 0 then
                  (SELECT strcat(n.name)
                     FROM c_node           n,
                          c_port           p,
                          c_serviceentity  se,
                          c_service        s1,
                          c_serviceservice eg1,
                          c_serviceservice eg2
                    where n.id = p.node_id
                      and p.id = se.entity_id
                      and s1.id = se.service_id
                      and s1.globalserviceid = eg1.entityglobalserviceid
                      and eg1.businessglobalserviceid =
                          eg2.entityglobalserviceid
                      and eg2.businessglobalserviceid = ce.globalserviceid
                      and se.servicetype_id = 110125
                      and se.abstractentity_id = 105
                      and n.nodetype_id in (102010, 102062))
                 else
                  (SELECT strcat(n.name)
                     FROM c_node           n,
                          c_port           p,
                          c_serviceentity  se,
                          c_service        s1,
                          c_serviceservice eg1,
                          c_serviceservice eg2,
                          c_serviceservice eg3
                    where n.id = p.node_id
                      and p.id = se.entity_id
                      and s1.id = se.service_id
                      and s1.globalserviceid = eg1.entityglobalserviceid
                      and eg1.businessglobalserviceid =
                          eg2.entityglobalserviceid
                      and eg2.businessglobalserviceid =
                          eg3.entityglobalserviceid
                      and eg3.businessglobalserviceid = ce.globalserviceid
                      and se.servicetype_id in (110128, 110125)
                      and se.abstractentity_id = 105
                      and n.nodetype_id in (102010, 102062))
               end) NODE_NAME
          from c_service         ce,
               c_customer        ct,
               c_servicecustomer sct,
               c_sncodeinfo      si,
               c_serviceentity   cs,
               c_node            cn
         where si.startnum >= 33064000
           and si.endnum <= 33064999
           and ce.id = si.hid
           and si.hid = sct.service_id
           and si.status = 1
           and ct.id(+) = sct.customer_id
           and ce.id = cs.service_id
           and cs.entity_id = cn.id
           and not exists (select sss.*
                  from c_sncodeinfo zz, c_servicecurrent sss
                 where zz.gid = si.gid
                   and sss.id = zz.hid
                   and zz.status = 1)
           and si.usefor not in ('30B+D SIP', 'DID SIP')
           and cs.entitytype_id = 102023
        union all
        select si.startnum,
               si.endnum,
               (endnum - startnum + 1) NUMBER_CAP,
               si.usefor,
               '占用',
               (select cl.name
                  from c_location cl
                 where cl.id = si.qlocation_id) name,
               (select (case
                         when cc.lifecycle = 4965 then
                          '装入'
                         else
                          '未装入'
                       end) enclose
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.long_function
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.control_way
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.charge
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.using
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               ct.name username,
               '有' zaituu,
               si.gid,
               to_char(si.group_index_tn_logic) YSH,
               (case
                 when (select count(0)
                         from c_serviceservice eg2, c_service cs
                        where eg2.businessglobalserviceid = ce.globalserviceid
                          and eg2.entityglobalserviceid = cs.globalserviceid
                          and cs.servicetype_id in (110132, 110129)) > 0 then
                  (SELECT strcat(n.name)
                     FROM c_node           n,
                          c_port           p,
                          c_serviceentity  se,
                          c_service        s1,
                          c_serviceservice eg1,
                          c_serviceservice eg2
                    where n.id = p.node_id
                      and p.id = se.entity_id
                      and s1.id = se.service_id
                      and s1.globalserviceid = eg1.entityglobalserviceid
                      and eg1.businessglobalserviceid =
                          eg2.entityglobalserviceid
                      and eg2.businessglobalserviceid = ce.globalserviceid
                      and se.servicetype_id = 110125
                      and se.abstractentity_id = 105
                      and n.nodetype_id in (102010, 102062))
                 else
                  (SELECT strcat(n.name)
                     FROM c_node           n,
                          c_port           p,
                          c_serviceentity  se,
                          c_service        s1,
                          c_serviceservice eg1,
                          c_serviceservice eg2,
                          c_serviceservice eg3
                    where n.id = p.node_id
                      and p.id = se.entity_id
                      and s1.id = se.service_id
                      and s1.globalserviceid = eg1.entityglobalserviceid
                      and eg1.businessglobalserviceid =
                          eg2.entityglobalserviceid
                      and eg2.businessglobalserviceid =
                          eg3.entityglobalserviceid
                      and eg3.businessglobalserviceid = ce.globalserviceid
                      and se.servicetype_id in (110128, 110125)
                      and se.abstractentity_id = 105
                      and n.nodetype_id in (102010, 102062))
               end) NODE_NAME
          from c_servicecurrent       ce,
               c_customer             ct,
               c_servicecustomer      sct,
               c_sncodeinfo           si,
               c_servicecurrententity cs,
               c_node                 cn
         where si.startnum >= 33064000
           and si.endnum <= 33064999
           and ce.id = si.hid
           and si.hid = sct.service_id
           and si.status = 1
           and ct.id(+) = sct.customer_id
           and ce.id = cs.service_id
           and cs.entity_id = cn.id
           and not exists (select sss.*
                  from c_sncodeinfo zz, c_service sss
                 where zz.gid = si.gid
                   and sss.id = zz.hid
                   and zz.status = 1)
           and si.usefor not in ('30B+D SIP', 'DID SIP')
           and cs.entitytype_id = 102023
        union all
        select si.startnum,
               si.endnum,
               (endnum - startnum + 1) NUMBER_CAP,
               si.usefor,
               '占用',
               (select cl.name
                  from c_location cl
                 where cl.id = si.qlocation_id) name,
               (select (case
                         when cc.lifecycle = 4965 then
                          '装入'
                         else
                          '未装入'
                       end) enclose
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.long_function
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.control_way
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.charge
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               (select cc.using
                  from c_code cc
                 where cc.code_val = si.startnum
                   and cc.codetype_id in (114017, 114019)),
               ct.name username,
               '有' zaituu,
               si.gid,
               to_char(si.group_index_tn_logic) YSH,
               (case
                 when (select count(0)
                         from c_serviceservice eg2, c_service cs
                        where eg2.businessglobalserviceid = ce.globalserviceid
                          and eg2.entityglobalserviceid = cs.globalserviceid
                          and cs.servicetype_id in (110132, 110129)) > 0 then
                  (SELECT strcat(n.name)
                     FROM c_node           n,
                          c_port           p,
                          c_serviceentity  se,
                          c_service        s1,
                          c_serviceservice eg1,
                          c_serviceservice eg2
                    where n.id = p.node_id
                      and p.id = se.entity_id
                      and s1.id = se.service_id
                      and s1.globalserviceid = eg1.entityglobalserviceid
                      and eg1.businessglobalserviceid =
                          eg2.entityglobalserviceid
                      and eg2.businessglobalserviceid = ce.globalserviceid
                      and se.servicetype_id = 110125
                      and se.abstractentity_id = 105
                      and n.nodetype_id in (102010, 102062))
                 else
                  (SELECT strcat(n.name)
                     FROM c_node           n,
                          c_port           p,
                          c_serviceentity  se,
                          c_service        s1,
                          c_serviceservice eg1,
                          c_serviceservice eg2,
                          c_serviceservice eg3
                    where n.id = p.node_id
                      and p.id = se.entity_id
                      and s1.id = se.service_id
                      and s1.globalserviceid = eg1.entityglobalserviceid
                      and eg1.businessglobalserviceid =
                          eg2.entityglobalserviceid
                      and eg2.businessglobalserviceid =
                          eg3.entityglobalserviceid
                      and eg3.businessglobalserviceid = ce.globalserviceid
                      and se.servicetype_id in (110128, 110125)
                      and se.abstractentity_id = 105
                      and n.nodetype_id in (102010, 102062))
               end) NODE_NAME
          from c_servicecurrent       ce,
               c_customer             ct,
               c_servicecustomer      sct,
               c_sncodeinfo           si,
               c_servicecurrententity cs,
               c_node                 cn
         where si.startnum >= 33064000
           and si.endnum <= 33064999
           and ce.id = si.hid
           and si.hid = sct.service_id
           and si.status = 1
           and ct.id(+) = sct.customer_id
           and ce.id = cs.service_id
           and cs.entity_id = cn.id
           and exists (select sss.*
                  from c_sncodeinfo zz, c_service sss
                 where zz.gid = si.gid
                   and sss.id = zz.hid
                   and zz.status = 1)
           and si.usefor not in ('30B+D SIP', 'DID SIP')
           and cs.entitytype_id = 102023) zz
 order by zz.STARTCODE
 

 

 

SQL查询优化-添加索引 对于新手小白,想要优化SQL,提升查询速度的,简单粗暴的方式。 阅读详情

相关推荐

MySQL(高级性能篇)10章——索引优化与查询优化

MySQL(高级性能篇)10章——索引优化与查询优化

weixin_74019079的博客 1068

java程序员要了解的sql语句优化技巧大全

java程序员要了解的sql语句优化技巧大全

大壮 1195

SQL优化篇 - MySQL查询select语句优化

使用MySQL,SQL优化,以及工作中实战的操作

2301_76868495的博客 1261

一次900万+数据量的 SQL 查询优化分析

祝大家新年快乐,在新的一年里,工作顺利,身体健康,万事如意!另外,最近大家没有特别重要的事情不要出门了,尽量在家呆着,减少与他人接触,出门一定记得带有防护作用的口罩,注意安全!保护好自己...

Java后端技术 970

高级 SQL 优化:让查询飞起来

通过选择合适的索引、避免全表扫描、减少不必要的子查询和 JOIN、分析执行计划等手段,你可以让查询速度大大提升。记住,数据库的性能优化是一个持续的过程,时刻关注执行计划和数据库的负载,才能真正做到让 SQL 查询飞起来!可以帮助你查看 SQL 查询的执行计划,帮助你了解查询的执行方式,进而找出性能瓶颈。虽然子查询(Subquery)非常灵活,但在处理大数据量时,它会导致性能下降,尤其是嵌套的子查询会被重复执行。执行计划会显示你查询的扫描方式、使用的索引等信息,通过分析执行计划,可以帮助你优化查询。

Kenneth55的专栏 1003

别再只调颜色阈值了!优化K210色块追踪性能的5个关键参数与实战技巧

本文深入探讨了K210色块追踪性能优化的五大核心策略,包括图像预处理参数精细调控、find_blobs参数进阶配置、运动控制算法升级、硬件时序优化以及可视化调试系统构建。通过实战技巧和参数调整方法,帮助开发者提升K210在色块追踪项目中的识别稳定性和响应速度,适用于云台追踪等应用场景。

weixin_42531588的博客 225

Java 归纳总结 -- 提高SQL查询效率的30种方法

1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。  2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:  select id from t where num is null  可以在num上设置默认值0,确保表中num列没有null值,然后这样查询:  selec

jcpp9527的博客 3825

短视频团队架构&工作流程---2025.3.30

1、负责短视频日常内容分发上线,包括视频头图、标题、简介、推荐位及部分内容元数据的日常导入、审核、上线、下线,并提供各品类短视频的内容上线计划表。(20)运营:视频推送后,实时跟进视频数据,定期统计分析数据报表,根据数据表现 找到问题,并发送给各个小组的负责人,对下一期视频内容进行调整。(4)运营:收到成片,设计视频头图、标题、简介、推荐位等,分发到各个平台,统计分析视频的数据,对各个平台的视频进行管理、用户互动等。1、负责影视、戏剧、广告、专题片等的策划、组稿选题、材料收集、内容策划。

超越神明 2153

java sql优化_Java教程之SQL性能优化提升

Ø 简单的性能优化Sql的性能优化是数据库工程师在实际工作中必须面对的重要课题之一。对于某些数据库工程师来说,它几乎唯一的命题。实际上,像WEB服务这样需要快速响应的应用场景中,SQL的性能直接决定了系统是否可以使用。这里主要介绍一些使用SQL执行速度更快,消耗内存更少的优化技巧,今天的文章只介绍其中的一种,后续会继续更新一些其它的优化方式。严格地优化查询性能时,必须要了解所使用的数据库的功能特点...

weixin_39811478的博客 265

用Verilog搭出RISC-V架构单周期CPU

这是笔者学校暑期学期的必修课,难度并不高(仅追求及格的话),但相当耗时,对于在硬件设计方面完全不感冒的同学更是一场避不开的折磨,所以笔者想通过自己的成果和被折磨经验来帮助有此方面需求的同学,或是对于CPU设计有浓厚兴趣的人。

paticita的博客 1万+

接口调试必看:status显示canceled的3种常见原因及修复方法

本文深度解析了接口调试中status显示canceled的三种核心原因:网络层中断(如请求超时)、应用层逻辑主动取消(如请求竞态、组件卸载)以及用户交互与浏览器行为。文章提供了从诊断到修复的系统性方案,包括分析Network Timing、使用Axios CancelToken、优化请求策略与性能,帮助开发者从根本上解决这一常见问题。

bb456的博客 936

MySQL 高级查询与 SQL 优化详解

连接类型描述INNER JOIN只返回两个表中匹配的行LEFT JOIN返回左表所有行 + 右表匹配行(无匹配则为NULL)RIGHT JOIN返回右表所有行 + 左表匹配行(无匹配则为NULL)FULL JOIN返回两个表中所有行(无匹配的列为NULL)优化建议:确保 JOIN 字段上有索引,避免 JOIN 造成全表扫描。尽量减少 JOIN 表的数量,复杂 JOIN 会导致查询性能下降。使用索引优化查询,避免全表扫描。尽量避免 SELECT *,只查询需要的字段。

weixin_46538011的博客 1415

嵌入式系统开发笔记101:STM32CubeIDE安装过程中的2个错误及其解决方法

STM32CubeIDE在安装过程中,遇到了2个错误,本文阐述其解决方法。产生上述错误的原因,都在于中文。STM32CubeIDE不能很好的支持中文,所以在使用STM32CubeIDE进行项目设计的过程中,不要使用中文,包括项目所在的路径。

snmplink的博客 8555

高级的SQL查询技巧有哪些?

关于【高级SQL查询技巧】 目录: 一、使用子查询 二、利用JOIN关联多个表 三、应用应用函数 四、采用CTE(Common Table Expressions) 五、利用递归查询 六、哪些高级查询技巧可以应用于SQL语言?

zhaoylzy的博客 2807

[23]-->变长SPI接口

然而,FPGA技术复杂,学习门槛较高,许多人(初学者或学生)在学习过程中面临诸多挑战。(1)模式0:SCK串行时钟线空闲是为低电平,数据在SCK时钟的上升沿被采样,数据在SCK时钟的下降沿切换,CPOL= 0,CPHA=0;(2)模式1:SCK串行时钟线空闲是为低电平,数据在SCK时钟的下降沿被采样,数据在SCK时钟的上升沿切换,CPOL= 0,CPHA=1;(3)模式3:SCK串行时钟线空闲是为高电平,数据在SCK时钟的上升沿被采样,数据在SCK时钟的下降沿切换,CPOL= 1,CPHA=1;

m0_46498597的博客 178

Spring AI Alibaba实战训练营-20 基于注解驱动的MCP快速开发入门指南

本文详细讲解如何使用SpringAiAlibaba快速开发基于注解驱动的MCP应用。文章详细介绍了MCP协议并且重点介绍了Spring AI通过注解驱动的开发方式简化MCP服务开发。具体包括服务端和客户端的注解使用、城市最新时间查询MCP项目结构概览(服务端和客户端模块)以及环境配置要求。

program哲学 295

SQL慢查询优化方式

MYSQL优化一般从SQL语句开始优化,再分析索引,即使有了良好的索引,糟糕的查询语句也可能导致性能问题优化查询语句可以帮助数据库更有效地利用现有的资源,减少不必要的开销。

echola_mendes的博客 5690

人工智能智能体研究综述:从理论架构到前沿应用

AI Agent,即人工智能智能体,是指能够在特定环境中自主感知、推理、决策并执行行动以实现特定目标的智能系统[1]。自主性反应性主动性(Pro-activeness)和社会性fill:#333;important;important;fill:none;color:#333;color:#333;important;fill:none;fill:#333;height:1em;AI Agent核心特征自主性 Autonomy反应性 Reactivity主动性 Pro-activeness。

nmdbbzcl的博客 902

《MySQL高级篇》八、索引优化与查询优化

《MySQL高级篇》八、索引优化与查询优化

LXYDSF的博客 5784

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

SQL查询优化

SQL查询优化 一、获取有性能问题SQL的三种方法 通过用户反馈获取存在性能问题的SQL 通过慢查询日志获取存在性能问题的SQL 实时获取存在性能问题的SQL 二、慢查询日志介绍 1、使用慢查询日志获取有性能问题的SQL (1) 参数配置 ...

weixin_38477351的博客 2万+

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

高级SQL查询技巧

在这些技巧中,例如,应用窗口函数可以在不需要分组数据的情况下计算数据的排名或者进行数据的分区统计,这是常规的GROUP BY所不能实现的。子查询允许你在SQL语句中嵌入另一个查询,它可以用在SELECT、INSERT、UPDATE和DELETE语句之中,提高查询的灵活性和复杂度。使用CTE能让查询更加易读,特别是在执行复杂的查询、多层次的子查询或具有递归逻辑的查询时。,它仅返回两个表中匹配的行。,因为你可以将查询分成多个逻辑上的部分,先定义CTE,然后在主查询中引用这些CTE,就像引用真实的表或视图一样。

2401_86649449的博客 1308

MySQL 高级篇 -- 查询优化及索引优化

关于索引失效的场景、查询优化的手段、索引优化的方法以及如何设计主键。

L小Ray想有腮 2996

Mysql高级篇(中)—— SQL优化之查询截取分析

本文主要介绍慢查询日志的使用、日志分析工具的使用、SHOW PROCESSLIST、Performance Schema、以及对Show Profile及全局查询日志的了解等内容

weixin_44666786的博客 1606

Mysql SQL优化实战 之 EXPLAIN篇(三表查询)

EXPLAIN ps:本篇博客记录sql优化实战 explain 相关理论知识在其他文章中总结,一些关键字或者知识点就不一一讲解了。表结构在之前博客中有提到 https://blog.youkuaiyun.com/LiuY521/article/details/114142914?spm=1001.2014.3001.5501数据库 一.三表优化 1.创建表 2.添加数据 3.查看三表索引 把之前两表创建的索引删除 4.三表查询SQL 1.Using join buffer 使用连接.

LiuY521的博客 681

高级sql技巧 从复杂查询到性能优化 提升数据处理效率

通过掌握高级 SQL 技巧,我们可以更高效地处理复杂数据查询,提高数据库操作的性能和效率。在实际工作中,合理使用窗口函数、递归查询、子查询优化、索引管理、数据透视表和复杂聚合等技巧,可以显著提升数据处理的准确性和效率。希望本文能够帮助你在实际项目中更好地应用这些高级 SQL 技巧。如果你有任何疑问或建议,欢迎在评论区留言,我们一起探讨。

C_V_Better的博客 1727
上一篇: 网页静态化技术Freemarker入门
下一篇: JS跨域解决方案
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我才是真的封不觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值