Interface performance optimization ----- PageHelper

Problem Description

A complex interface in the project takes too much time, and the amount of table data to be queried is large. The interface uses PageHelper to paginate the query results, and the query sql performs joint table query. By looking at the log and raising the sql, it is found that the PageHelper component is slow when counting, and this interface query is not slow because it is paged and indexed.

PageHelper.startPage(searchRecordPosition.getPage(), searchRecordPosition.getSize());
List<positionRecordVO> result = positionRecordMapper.getRecordPositionList(searchRecordPosition);

This is written sql

<select id="getRecordPositionList" resultType="com.hengtiansoft.hzrc.dal.vo.positionRecordVO">
        SELECT p.id AS positionId,p.NAME AS positionName,p.education_id AS
        educationId,p.working_years_id AS workYearsId,p.recruit_number AS recruitNumber,
        p.high_tech as highTech,p.urgently_hired as urgentlyHired,r.id as recruitmentId,c.id as companyId,
        r.way as positionSource,r.name as recruitmentName,r.status as status,c.name as companyName,
        c.credit_code as creditCode,p.create_ts as releaseTs,
        p.tag_id AS tagId,p.s_region_id AS sRegionId,p.property as property,p.salary_lower as salaryLower,
        p.salary_max as salaryMax,p.contact as contact,p.contact_number as contactNumber,p.update_by as updateBy,r.id as
        recruitmentId
        FROM position_record p
        LEFT JOIN recruitment r ON p.recruitment_id = r.id
        LEFT JOIN company c ON p.company_id = c.id
        <where>
            <if test="null != searchPosition.educationId and searchPosition.educationId != ''">
                AND p.education_id = #{
    
    searchPosition.educationId}
            </if>
            <if test="searchPosition.companyId != null  and searchPosition.companyId != ''">
                and p.company_id = #{
    
    searchPosition.companyId}
            </if>
            <if test="searchPosition.positionName != null and searchPosition.positionName != ''">
                and p.name LIKE CONCAT('%',#{
    
    searchPosition.positionName},'%')
            </if>
            <if test="searchPosition.companyName != null and searchPosition.companyName != ''">
                and c.name LIKE CONCAT('%',#{
    
    searchPosition.companyName},'%')
            </if>
            <if test="searchPosition.startTime != null and searchPosition.endTime != null">
                and p.release_ts<![CDATA[>=]]> #{
    
    searchPosition.startTime}
                and p.release_ts <![CDATA[<=]]> #{
    
    searchPosition.endTime}
                and p.status != '1'
            </if>
            <if test="searchPosition.createStartTs !=null ">
                and p.create_ts <![CDATA[>=]]> #{
    
    searchPosition.createStartTs}
            </if>
            <if test="searchPosition.createEndTs !=null ">
                and p.create_ts <![CDATA[<=]]> #{
    
    searchPosition.createEndTs}
            </if>
            <if test="searchPosition.property !=null and searchPosition.property != ''">
                and p.property = #{
    
    searchPosition.property}
            </if>
            <if test="searchPosition.salaryMax !=null and searchPosition.salaryMax != ''">
                and p.salary_max <![CDATA[<=]]> #{
    
    searchPosition.salaryMax}
            </if>
            <if test="searchPosition.salaryLower !=null and searchPosition.salaryLower != ''">
                and p.salary_lower <![CDATA[>=]]> #{
    
    searchPosition.salaryLower}
            </if>
            <if test="searchPosition.workingYearsId !=null and searchPosition.workingYearsId != ''">
                and p.working_years_id = #{
    
    searchPosition.workingYearsId}
            </if>
            <if test="searchPosition.sRegionId !=null and searchPosition.sRegionId != ''">
                and p.s_region_id = #{
    
    searchPosition.sRegionId}
            </if>
            <if test="searchPosition.urgentlyHired !=null and searchPosition.urgentlyHired != ''">
                and p.urgently_hired = #{
    
    searchPosition.urgentlyHired}
            </if>
            <if test="searchPosition.tagId != null and searchPosition.tagId != ''">
                and p.tag_id = #{
    
    searchPosition.tagId}
            </if>
            <if test="searchPosition.recruitmentName != null and searchPosition.recruitmentName != ''">
                and r.name LIKE CONCAT('%',#{
    
    searchPosition.recruitmentName},'%')
            </if>
            <if test="searchPosition.positionSource !=null and searchPosition.positionSource != ''">
                and r.way = #{
    
    searchPosition.positionSource}
            </if>
            <if test="searchPosition.status !=null and searchPosition.status != ''">
                and r.status = #{
    
    searchPosition.status}
            </if>
        </where>
        order by p.create_ts desc
    </select>

solution

The problem lies in the count, and the slowness of the count is actually due to the joint table, so I chose to rewrite the count and use the if tag to avoid the joint table. When doing conditional query, the amount of data will be much smaller due to filtering, so it is not slow

<select id="getRecordPositionList_COUNT" resultType="Long">
        SELECT count(*)
        FROM position_record p
        <if test="searchPosition.recruitmentName != null and searchPosition.recruitmentName != '' and searchPosition.positionSource !=null and searchPosition.positionSource != '' and searchPosition.status !=null and searchPosition.status != ''">
            LEFT JOIN recruitment r ON p.recruitment_id = r.id
        </if>
        <if test="null != searchPosition.companyName and searchPosition.companyName != ''">
            LEFT JOIN company c ON p.company_id = c.id
        </if>
        <where>
            <if test="null != searchPosition.educationId and searchPosition.educationId != ''">
                AND p.education_id = #{
    
    searchPosition.educationId}
            </if>
            <if test="searchPosition.companyId != null  and searchPosition.companyId != ''">
                and p.company_id = #{
    
    searchPosition.companyId}
            </if>
            <if test="searchPosition.positionName != null and searchPosition.positionName != ''">
                and p.name LIKE CONCAT('%',#{
    
    searchPosition.positionName},'%')
            </if>
            <if test="searchPosition.companyName != null and searchPosition.companyName != ''">
                and c.name LIKE CONCAT('%',#{
    
    searchPosition.companyName},'%')
            </if>
            <if test="searchPosition.startTime != null and searchPosition.endTime != null">
                and p.release_ts<![CDATA[>=]]> #{
    
    searchPosition.startTime}
                and p.release_ts <![CDATA[<=]]> #{
    
    searchPosition.endTime}
                and p.status != '1'
            </if>
            <if test="searchPosition.createStartTs !=null ">
                and p.create_ts <![CDATA[>=]]> #{
    
    searchPosition.createStartTs}
            </if>
            <if test="searchPosition.createEndTs !=null ">
                and p.create_ts <![CDATA[<=]]> #{
    
    searchPosition.createEndTs}
            </if>
            <if test="searchPosition.property !=null and searchPosition.property != ''">
                and p.property = #{
    
    searchPosition.property}
            </if>
            <if test="searchPosition.salaryMax !=null and searchPosition.salaryMax != ''">
                and p.salary_max <![CDATA[<=]]> #{
    
    searchPosition.salaryMax}
            </if>
            <if test="searchPosition.salaryLower !=null and searchPosition.salaryLower != ''">
                and p.salary_lower <![CDATA[>=]]> #{
    
    searchPosition.salaryLower}
            </if>
            <if test="searchPosition.workingYearsId !=null and searchPosition.workingYearsId != ''">
                and p.working_years_id = #{
    
    searchPosition.workingYearsId}
            </if>
            <if test="searchPosition.sRegionId !=null and searchPosition.sRegionId != ''">
                and p.s_region_id = #{
    
    searchPosition.sRegionId}
            </if>
            <if test="searchPosition.urgentlyHired !=null and searchPosition.urgentlyHired != ''">
                and p.urgently_hired = #{
    
    searchPosition.urgentlyHired}
            </if>
            <if test="searchPosition.tagId != null and searchPosition.tagId != ''">
                and p.tag_id = #{
    
    searchPosition.tagId}
            </if>
            <if test="searchPosition.recruitmentName != null and searchPosition.recruitmentName != ''">
                and r.name LIKE CONCAT('%',#{
    
    searchPosition.recruitmentName},'%')
            </if>
            <if test="searchPosition.positionSource !=null and searchPosition.positionSource != ''">
                and r.way = #{
    
    searchPosition.positionSource}
            </if>
            <if test="searchPosition.status !=null and searchPosition.status != ''">
                and r.status = #{
    
    searchPosition.status}
            </if>
        </where>
    </select>

How to use

The original code does not need to be moved, just add a count query to the xml file of mybatis
Here, pay attention to the following three points:

1. The id is consistent with the corresponding query statement, and ends with _COUNT, pay attention to capitalization
2. The input parameter is consistent with the corresponding query statement
3. The output parameter is resultType="Long"
4. The version of PageHelper must be 5.0 .5 and above

Guess you like

Origin blog.csdn.net/weixin_45933454/article/details/130752123