MySQL优化之-数据量大的情况下分页优化

简述

在项目中遇到数据量比较大的情况下,数据加载会比较缓慢,mysql大数据量使用limit分页,随着页码的增大,查询效率越低下,如何处理这种问题呢?

MySQL优化之-数据量大的情况下分页优化_第1张图片

解决方案

将语句 :SELECT id FROM tablename LIMIT 190920,20

更改为:SELECT a.id FROM tablename a JOIN (SELECT id FROM tablename LIMIT 190920,20)  b ON a.id = b.id

应用场景

优化前SQL:执行时间:10.951s

SELECT sc.id,sc.cname,sc.labelpeer,sc.intentionally,sc.ittnxl,sc.ittnzy,sc.ittnyx,sc.ittnxm,sc.ittngj,sc.budget,sc.timeline,sc.graduate,sc.graduatezy,sc.xuel,sc.tolink,sc.note,sc.creatid,sc.is_top,sc.charge_person,sc.follow_up,sc.follow_person,sc.follw_time,sc.next_follow,sc.xueshuchengji,sc.yuyanchengji,sc.hzID,sc.sort,cd.cid,cd.zdnum,cd.own,cd.occupation,cd.groupid,cd.sex,cd.age,cd.station,cd.industry,cd.experience,cd.company,cd.scale,cd.CHARACTER,cd.agent_name,cd.cphone,cd.cphonetwo,cd.cphonethree,cd.telephone,cd.formwhere,cd.formwhere2,cd.formwhere3,cd.wxnum,cd.cemail,cd.qq,cd.create_time,cd.ocreatid,cd.project_name,'' AS bid
FROM
	crm_customer sc
LEFT JOIN crm_customer_data cd ON sc.id = cd.cid
WHERE
	sea_type = 0
ORDER BY
	sc.is_top DESC,
	cd.cid DESC
LIMIT 190940,
 20

优化前执行效果:

 

优化之后的SQL:执行时间:0.059s

SELECT sc.id,sc.cname,sc.labelpeer,sc.intentionally,sc.ittnxl,sc.ittnzy,sc.ittnyx,sc.ittnxm,sc.ittngj,sc.budget,sc.timeline,sc.graduate,sc.graduatezy,sc.xuel,sc.tolink,sc.note,sc.creatid,sc.is_top,sc.charge_person,sc.follow_up,sc.follow_person,sc.follw_time,sc.next_follow,sc.xueshuchengji,sc.yuyanchengji,sc.hzID,sc.sort,cd.cid,cd.zdnum,cd.own,cd.occupation,cd.groupid,cd.sex,cd.age,cd.station,cd.industry,cd.experience,cd.company,cd.scale,cd.CHARACTER,cd.agent_name,cd.cphone,cd.cphonetwo,cd.cphonethree,cd.telephone,cd.formwhere,cd.formwhere2,cd.formwhere3,cd.wxnum,cd.cemail,cd.qq,cd.create_time,cd.ocreatid,cd.project_name,'' AS bid
FROM
	crm_customer sc
JOIN (
	SELECT
		id
	FROM
		crm_customer
	LIMIT 190920,
	20
) sb ON sc.id = sb.id
LEFT JOIN crm_customer_data cd ON sc.id = cd.cid
WHERE
	sea_type = 0
ORDER BY
	sc.is_top DESC,
	cd.cid DESC

优化后执行效果:

总结

优化完,加载速度嗖嗖的~

你可能感兴趣的:(MySQL)