八万条数据表查询的优化

第一次sql


<select id="getDailyCountList" parameterType="java.util.Map" resultType="customerQueryCount">
select T.queryTime,T.interfaceCode,T.searchType,T.totalSU,T.totalQG,T.totalDI,T.totalFA,T.companyName from 
(SELECT DATE_FORMAT(T1.baseYMD,'%Y-%m-%d') queryTime,T1.interfaceCode,T1.searchType,IFNULL(T5.totalSU,0) totalSU,IFNULL(T2.totalQG,0) totalQG,IFNULL(T3.totalDI,0) totalDI,IFNULL(T4.totalFA,0) totalFA,T6.companyName,T6.ownerId
FROM(select baseYMD,ettType,customerId,interfaceCode,searchType from interface_log.searchlogymd group by interfaceCode, searchType, baseYMD, customerId) T1
LEFT JOIN (select baseYMD,ettType,customerId,interfaceCode,searchType,SUM(total) totalQG from interface_log.searchlogymd where ettType = '1000' group by interfaceCode, searchType, baseYMD, customerId ) T2
ON T1.customerId = T2.customerId and T1.baseYMD = T2.baseYMD and T1.interfaceCode = T2.interfaceCode and T1.searchType = T2.searchType
LEFT JOIN (select baseYMD,ettType,customerId,interfaceCode,searchType,SUM(total) totalDI from interface_log.searchlogymd where ettType = '2001' group by interfaceCOde, searchType, baseYMD, customerId ) T3
ON T1.customerId = T3.customerId and T1.baseYMD = T3.baseYMD and T1.interfaceCode = T3.interfaceCode and T1.searchType = T3.searchType
LEFT JOIN (select baseYMD,ettType,customerId,interfaceCode,searchType,SUM(total) totalFA from interface_log.searchlogymd where ettType = '9999' group by interfaceCode, searchType, baseYMD, customerId ) T4
ON T1.customerId = T4.customerId and T1.baseYMD = T4.baseYMD and T1.interfaceCode = T4.interfaceCode and T1.searchType = T4.searchType
LEFT JOIN (select baseYMD,ettType,customerId,interfaceCode,searchType,SUM(total) totalSU from interface_log.searchlogymd where ettType = '0000' group by interfaceCode, searchType, baseYMD, customerId ) T5
ON T1.customerId = T5.customerId and T1.baseYMD = T5.baseYMD and T1.interfaceCode = T5.interfaceCode and T1.searchType = T5.searchType
LEFT JOIN (select customerId,companyName,ownerId,customerArea from service_manager.customer ) T6
ON T1.customerId = T6.customerId
<where>
<if test="ownerId != null ">
                AND T6.ownerId = #{ownerId}
            </if>
            <if test="regionList != null and regionList.size>0">
            OR T6.customerArea in 
            <foreach collection="regionList" item="region"  open="(" separator="," close=")">
            #{region.regionCode}
            </foreach>
            </if>
</where>) T
<where>
<if test="companyName != null">
and T.companyName like '%${companyName}%'
</if>
<if test="beginDate != null">
and T.queryTime >= DATE_FORMAT(#{beginDate},'%Y-%m-%d')
</if>
<if test="endDate != null">
and T.queryTime &lt;= DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>
</where>
ORDER BY T.queryTime desc,T.companyName,T.searchType
        limit #{start},#{pageSize}
</select>

优化逻辑,减少子查询


第二次sql

<select id="getDailyCountList" parameterType="java.util.Map" resultType="customerQueryCount">

select T.queryTime,T.interfaceCode,T.searchType,T.totalSU,T.totalQG,T.totalDI,T.totalFA,T.companyName from 
(SELECT TA.queryTime,TA.customerId,TA.interfaceCode,TA.searchType,IFNULL(TA.totalQG,0) totalQG,IFNULL(TA.totalDI,0) totalDI,IFNULL(TA.totalFA,0) totalFA,IFNULL(TA.totalSU,0) totalSU,TA.companyName,TA.ownerId,TA.customerArea FROM (
SELECT DATE_FORMAT(a.baseYMD,'%Y-%m-%d') queryTime,a.customerId,a.interfaceCode,a.searchType,
SUM(case when a.ettType = '1000' then a.total end ) totalQG ,
SUM(case when a.ettType = '2001' then a.total end ) totalDI ,
SUM(case when a.ettType = '9999' then a.total end ) totalFA ,
SUM(case when a.ettType = '0000' then a.total end ) totalSU , 
b.companyName,b.ownerId,b.customerArea
FROM interface_log.searchlogymd a LEFT JOIN
service_manager.customer b ON a.customerId = b.customerId
GROUP BY interfaceCode, searchType, baseYMD, customerId ) TA
<where>
<if test="ownerId != null ">
                AND TA.ownerId = #{ownerId}
            </if>
            <if test="regionList != null and regionList.size>0">
            OR TA.customerArea in 
            <foreach collection="regionList" item="region"  open="(" separator="," close=")">
            #{region.regionCode}
            </foreach>
            </if>
</where>) T
<where>
<if test="companyName != null">
and T.companyName like '%${companyName}%'
</if>
<if test="beginDate != null">
and T.queryTime >= DATE_FORMAT(#{beginDate},'%Y-%m-%d')
</if>
<if test="endDate != null">
and T.queryTime &lt;= DATE_FORMAT(#{endDate},'%Y-%m-%d')
</if>
</where>
ORDER BY T.queryTime desc,T.companyName,T.searchType
        limit #{start},#{pageSize}

</select>

优化逻辑,将过滤条件前置,降低数据量

第三次sql


<select id="getDailyCountList" parameterType="java.util.Map" resultType="customerQueryCount">
SELECT T1.queryTime,T1.customerId,T1.interfaceCode,T1.searchType,IFNULL(T1.totalQG,0) totalQG,IFNULL(T1.totalDI,0) totalDI,IFNULL(T1.totalFA,0) totalFA,IFNULL(T1.totalSU,0) totalSU,
T2.companyName,T2.ownerId,T2.customerArea FROM  
(SELECT DATE_FORMAT(baseYMD,'%Y-%m-%d') queryTime,customerId,interfaceCode,searchType,
SUM(case when ettType = '1000' then total end ) totalQG ,
SUM(case when ettType = '2001' then total end ) totalDI ,
SUM(case when ettType = '9999' then total end ) totalFA ,
SUM(case when ettType = '0000' then total end ) totalSU 
FROM interface_log.searchlogymd   
<where>
<if test="beginDate != null">
and baseYMD >= replace(#{beginDate},'-','')
</if>
<if test="endDate != null">
and baseYMD &lt;= replace(#{endDate},'-','')
</if>
</where>
GROUP BY interfaceCode, searchType, baseYMD, customerId ORDER BY null ) T1 
LEFT JOIN service_manager.customer T2 ON T1.customerId = T2.customerId
<where>
<if test="ownerId != null ">
               AND T2.ownerId = #{ownerId}
           </if>
           <if test="regionList != null and regionList.size>0">
            OR T2.customerArea in 
            <foreach collection="regionList" item="region"  open="(" separator="," close=")">
            #{region.regionCode}
            </foreach>
           </if>
           <if test="companyName != null">
and T2.companyName like '%${companyName}%'
</if>
</where>
ORDER BY T1.queryTime desc,T2.companyName,T1.searchType
        limit #{start},#{pageSize}
</select>

猜你喜欢

转载自blog.csdn.net/after_you/article/details/78343891