• sql大数据多条件查询索引优化


    此优化的前提可以称之为最近流行的头条人物“许三多”,总数据多,查询条件多,返回列多

    优化前分页查询内部select列为需要的全部列,优化后内部select只返回ID主键,外部查询关联原数据表,然后查出所需要的列

    例子1

    优化前:

    1. select t.* from (  
    2.         select r.* ,row_number() over(order by r.id desc) row from tab(nolock) r   
    3.         where 1=1 and r.IsDelete=0 and r.Status>0 and r.PlatformID=1 and r.CreateUser=100000       
    4. as t  where row between 1 and 10  
    select t.* from (
            select r.* ,row_number() over(order by r.id desc) row from tab(nolock) r 
            where 1=1 and r.IsDelete=0 and r.Status>0 and r.PlatformID=1 and r.CreateUser=100000 	
    ) as t  where row between 1 and 10

    优化后:

    1. select r.* from (  
    2.         select r.ID ,row_number() over(order by r.id desc) row from tab(nolock) r   
    3.         where 1=1 and r.IsDelete=0 and r.Status>0 and r.PlatformID=1 and r.CreateUser=100000       
    4. as t join tab r on r.id=t.id where row between 1 and 10  
    select r.* from (
            select r.ID ,row_number() over(order by r.id desc) row from tab(nolock) r 
            where 1=1 and r.IsDelete=0 and r.Status>0 and r.PlatformID=1 and r.CreateUser=100000 	
    ) as t join tab r on r.id=t.id where row between 1 and 10
    

    最近又有一个例子

    例子2

    优化前:tablA数据量1千多万,tablB数据量几百万,查询速度11秒多

    1. select * from (  
    2.         select d.LessonPlanID,d.ResUrl,p.createID,p.CreateTime,row_number() over(order by d.id desc) row  
    3.         from tablA(nolock) d   
    4.         join tablB(nolock) p on p.id=d.lessonplanid  
    5.         where p.createID in(109486,103295,103298,109347,130346,181382,330312)  
    6.     ) t where t.row between 1 and 20   
    select * from (
    	    select d.LessonPlanID,d.ResUrl,p.createID,p.CreateTime,row_number() over(order by d.id desc) row
    	    from tablA(nolock) d 
    		join tablB(nolock) p on p.id=d.lessonplanid
    	    where p.createID in(109486,103295,103298,109347,130346,181382,330312)
    	) t where t.row between 1 and 20 

    优化后:查询速度14毫秒

    1. select d.LessonPlanID,d.ResUrl,p.createID,p.CreateTime from (  
    2.         select  d.id,row_number() over(order by d.id desc) row  
    3.         from tablA(nolock) d   
    4.         join tablB(nolock) p on p.id=d.lessonplanid  
    5.         where p.createID in(109486,103295,103298,109347,130346,181382,330312)  
    6. ) t join tablA(nolock) d on d.id=t.id   join tablB(nolock) p on p.id=d.lessonplanid  
    7.  where t.row between 1 and 20   
    select d.LessonPlanID,d.ResUrl,p.createID,p.CreateTime from (
    	    select  d.id,row_number() over(order by d.id desc) row
    	    from tablA(nolock) d 
    		join tablB(nolock) p on p.id=d.lessonplanid
    	    where p.createID in(109486,103295,103298,109347,130346,181382,330312)
    ) t join tablA(nolock) d on d.id=t.id	join tablB(nolock) p on p.id=d.lessonplanid
     where t.row between 1 and 20 
  • 相关阅读:
    高并发场景 LVS 安装及keepalived的应用
    使用nginx作为http/https正向代理
    Spring5【七】Spring 整合 MyBatis
    Spring5【六】代理模式及 AOP
    MyBatis 配置模板
    Spring5【五】Bean 的自动装配及注解开发
    Spring5【四】依赖注入(DI)
    Spring5【三】IoC 创建对象的方式及配置说明
    Spring5【一】Spring 简介
    MyBatis【七】缓存
  • 原文地址:https://www.cnblogs.com/firstdream/p/8807829.html
Copyright © 2020-2023  润新知