SQL_Mybatics效率优化

1.先上表数据(简化版,真实数据量10W+)

select * from  issuBaseDB  where version_name = "WT_TEST" and user = "cwx605983";
select * from  issuTestDB  where version_name = "WT_TEST" and user = "cwx605983";

查询结果:

页面展示的数据如下:

查询SQL如下:

			select
			ifnull(b.procName,t.procName) as consumeName,
			ifnull(b.user,t.user) as userId,
			ifnull(b.version_name,t.version_name) as versionName,
			ifnull(round(b.time,0),"0") as  baseConsumeTime,
			ifnull(round(t.time,0),"0") as  testConsumeTime,
			ifnull(t.time - b.time,0) as deteriorationTime
			FROM issuBaseDB b 
			inner join issuTestDB t
			on b.procName = t.procName
			where 1=1
			and b.version_name = "WT_TEST"
			and t.version_name = "WT_TEST"
			and b.user = "cwx605983"
			and t.user = "cwx605983"
			and b.procName <> "DBSource"
			and t.procName <> "DBSource"
			
		    union 	 
			select 
			b.procName as consumeName,
			b.user as userId,
			b.version_name as versionName,
			b.time as  baseConsumeTime,
			"-"	as  testConsumeTime,
		    -b.time as deteriorationTime		
	        FROM  issuBaseDB  b 
	        left join  issuTestDB 	t
	        on (t.procName = b.procName and t.version_name = "WT_TEST" and t.user = "cwx605983" and t.procName <> "DBSource" )
			where t.procName is null 
		    and	b.version_name = "WT_TEST"
            and b.user = "cwx605983" 
            and b.procName <> "DBSource"
                     		
	   		union 	
			select           
			t.procName as consumeName,
			t.user as userId,
			t.version_name as versionName,
			"-"	as  baseConsumeTime,
			t.time as  testConsumeTime,
		    t.time as deteriorationTime		
	        FROM  issuTestDB  t 
	        left join  issuBaseDB 	b
	        on (t.procName = b.procName and b.version_name = "WT_TEST" and b.user = "cwx605983"  and b.procName <> "DBSource" )
			where b.procName is null 
            and t.version_name = "WT_TEST"
            and t.user = "cwx605983" 
            and t.procName <> "DBSource"  

扯到优化,就不得不说说几种表链接 : sql查询几种链接方式及过滤条件

left join : on 条件只过滤右表,返回左表所有数据

right join : on 条件只过滤左表,返回右表所有数据

inner join : on 条件同时过滤两张表

where 条件对生成的临时表进行二次过滤

查询分为3次union链接

第一次查询a1 这条数据  :inner join 查询

第二次查询a3这条数据:base表 left join test表 , on条件:过滤掉同一个user里不同的version_name里 procName 重名的干扰数据(不为null),where条件:对左链接的中间表进行二次过滤,获取同一用户,同一版本里只有base的数据

第三次查询a2这条数据 :test表 left join base表,on条件:过滤掉同一个user里不同的version_name里 procName 重名的干扰数据(不为null),where条件:对左链接的中间表进行二次过滤,获取同一用户,同一版本里只有test的数据

具体的优化细节:数据量超过1k以后,左链的两张表基本就可以放弃使用子表的方法了(子表也是临时表,没法用索引),而是在各个链接字段上加index索引,查询效率会大大大大提高!

2.表的数据量大: 拆分表:根据时间字段或者其他字段拆分

业务上对数据库表的数据做递归循环不能在sql里去实现,影响效率,而是取出数据在内存里去实现

猜你喜欢

转载自blog.csdn.net/m0_37840243/article/details/88389578