MySQL Tuning Practice

MySQL Tuning Practice

记一次实习MySQL调优

Business requirement: display of popular posts

  • Count the number of comments and views of the post within a certain period of time, and sort in descending order according to the number of comments, views, and post creation time

Three tables: app_invitationpost table, app_invitation_commentpost comment table, app_invitation_lookpost browsing record table

Tuning steps

Tuning background: The part developed by the former employee had a small amount of data during the test, and no problem of too slow response speed was found. The amount of data increased, and the response speed was too slow, so it needs to be optimized

  1. First check whether the code needs to be optimized:

    • It is not necessary to process all the queried data, but to process the part that needs to be displayed
  2. Writing SQL statements

    The original sql statement is redundantly written, and both the table join and the subquery are to obtain the same information point

    <select id="moreHotList" resultType="AppInvitation" >
            SELECT count(b.id) count,a.look,
        	(SELECT IFNULL(sum(look),0) from
            app_invitation_look where create_time BETWEEN #{start} and #{end} and invitation_id = a.id) look2,
        	a.id, a.type,a.tile_category tileCategory, a.tile_specification tileSpecification,a.description, a.uid, a.phone, a.city, a.hot, a.top,  a.longitude, a.latitude, a.enterprise,a.machine_trial machineTrial, a.state, a.dev, a.create_time createTime, a.update_time updateTime FROM
            app_invitation a LEFT JOIN app_invitation_comment b ON a.id = b.invitation_id LEFT JOIN app_invitation_look c on a.id = c.invitation_id
        	where a.dev = 1 AND a.state = 1 AND a.machine_trial = 1
            GROUP BY a.id 
            <if test="last != null ">
                and a.update_time &lt;= #{last}
            </if>
            ORDER BY count desc,look2 desc,a.create_time desc
        </select>
    
    • By explainlooking at the execution plan, the type of the two tables isall

      • Respectively app_invitation_comment post comment table, app_invitation_look post browsing record table建立invitation_id的索引
      • Result: the type of subquery and join table part isref
    • Extra column for post table queryusing temporary、using filesort

      • innodb_buffer_pool_sizeUsing temporary, through online search, it is found that the temporary table may be set up because the setting is too small. Through show variable like “%innodb_buffer_pool_size%”, it is found that the existing setting is 128M, and it is modified to 256M

      • Using filesort is sorted in memory. The sorting field is the field that is calculated by using the joint table/subquery, so it is not easy to build an index:

        (Note: The following execution time is due to the fact that the author is executing in a database with a small amount of data and is only for display)

        1. By set profiling = 1;executing the sql statement, show profilesyou can view the specific execution time

          [External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-5OAd23kt-1661079391604) (C:\Users\Cai Xiaona\AppData\Roaming\Typora\typora-user-images\ image-20220821183759188.png)]

        2. You can pass show profile for query query_id, query_id, which corresponds to the specific value of the Query_ID column,

          That is, show profile for query 2;you can view the specific time spent on each step

          [External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-xmQZ6pWG-1661079391606) (C:\Users\Cai Xiaona\AppData\Roaming\Typora\typora-user-images\ image-20220821184058085.png)]

        3. It is found that sorting takes more time. The sorting field is a field obtained by using a subquery, so it is not easy to build an index; using filesort sorts in memory, and there are two sorting methods, which determine the parameter setting of the sorting method. The default is 1MB, which is a single- sort_buffer_size way Sort, choose to increase parameter value

        4. The creation time of one of the sorting fields is positively correlated with the auto-increment id, so change create_time to id

        5. Combined with business needs, the database will contain logically deleted posts, that is, not all posts need to be retrieved. Originally, the where statement was used to filter the tables after joining, but the author changed to filter the posts before joining.

Final version:

<select id="moreHotList" resultType="AppInvitation" >
    SELECT count(b.id) comment,a.look,
    (SELECT IFNULL(sum(look),0) from app_invitation_look where create_time BETWEEN #{start} and #{end} and invitation_id = a.id) look2,
    a.id, a.type,a.tile_category tileCategory, a.tile_specification tileSpecification,
    a.description, a.uid, a.phone, a.city, a.hot, a.top, a.longitude, a.latitude, a.enterprise,
    a.machine_trial machineTrial, a.state, a.dev, a.create_time createTime, a.update_time updateTime
    FROM (SELECT * FROM app_invitation where dev = 1 AND state = 1 AND machine_trial = 1
    <if test="last != null ">
        and update_time &lt;= #{last}
    </if>
    ) a
    LEFT JOIN app_invitation_comment  b
    on a.id = b.invitation_id and b.create_time BETWEEN #{start} and #{end} and b.hidden = 2 and b.machine_trial = 1 and b.dev = 2
    GROUP BY a.id
    ORDER BY comment desc,look2 desc,a.id desc
</select>
and b.dev = 2
    GROUP BY a.id
    ORDER BY comment desc,look2 desc,a.id desc
</select>

Guess you like

Origin blog.csdn.net/weixin_47407737/article/details/126454080