记录一次使用 PageHelper 插件进行分页的糟糕体验mysql
在一次服务上线后,进行验证时发现其中一个页面加载异常的缓慢,当时并无意识到是SQL的问题; 后来运维组梳理统计慢SQL时将某一个SQL语句发给我,让我进行优化;git
语句以下:github
SELECT COUNT(0) FROM ( SELECT id, gmt_create, gmt_modify, order_no,··· FROM xxxlog WHERE payment_time >= '2018-10-08 09:55:33' AND corp_no IN ('3702040001', '3702040002', ) ORDER BY payment_time DESC ) tmp_count;
select id, gmt_create, gmt_modify, order_no,··· from (SELECT id, gmt_create, gmt_modify, order_no,··· FROM xxxlog WHERE payment_time >= ? and corp_no in ( ? , ? ) order by payment_time desc) as tmp_page limit 0,10
What ? What ? What ? 咱们系统的xxxlog表中有超过千万的数据量,直接使用这样方式的SQL进行分页,不是疯了吗!!!spring
检查系统代码后,并无发现mapper中有这样的SQL语句,忽然想起系统好像重构后使用了一种分页插件,会不会是分页插件默认添加的呢? debug发现,没错,是他就是他,PageHelper默认将分页语句拼装成了那样的SQL;sql
结论:框架拿来就用是有风险的,须要评估后使用。mybatis
后来深刻了解了一下这个框架,发现出现这种状况是因为咱们依赖的版本过低致使的; 依赖版本:app
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.2.3</version> </dependency>
MyBatis配置框架
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:mapper/**/*Mapper.xml" /> <property name="plugins"> <array> <bean class="com.github.pagehelper.PageHelper"> <property name="properties"> <value> dialect=mysql pageSizeZero=true reasonable=true </value> </property> </bean> </array> </property> </bean>