【MybatisPlus异常】The SQL execution time is too large, please optimize
本文目录
一、项目背景
二、报错信息
三、报错原因
四、解决方案
方案一:优化SQL语句
方案二:修改MP的配置
一、项目背景
Spring boot (v2.0.0.RELEASE) + mybatis-plus (3.1.1)
二、报错信息
在使用MybatisPlus的过程中,记录一下踩过的坑,以下是报错的内容:
### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
### The error may exist in com/uiotsoft/daily/module/dao/DailyReadRecordMapper.java (best guess)
### The error may involve com.uiotsoft.daily.module.dao.DailyReadRecordMapper.insert-Inline
### The error occurred while setting parameters
### SQL: INSERT INTO daily_job_read_record ( job_id, read_user, read_name ) VALUES ( ?, ?, ? )
### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 132 common frames omitted
Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38)
at com.baomidou.mybatisplus.core.toolkit.Assert.isFalse(Assert.java:50)
at com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor.intercept(PerformanceInterceptor.java:192)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.$Proxy194.update(Unknown Source)
at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:54)
at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
at com.sun.proxy.$Proxy193.update(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
... 138 common frames omitted
三、报错原因
出现此错误内容,说明 你写的SQL执行时间太长,它提示你让优化SQL语句。具体是哪一条SQL执行时间过长,上面也写明了。
至于为什么会导致报错,有两个原因:
第一、你写的SQL语句执行效率低,需要优化。
第二、mybatis-plus设置的最大时间配置的太短了。
那么既然知道原因了,解决方法就自然而然的知道了。
四、解决方案
根据上面两个原因,对应的解决方法也有两个,具体内容如下:
方案一:优化SQL语句
具体的sql优化我这里就不写,每个人的sql语句不一样,根据自己的sql做出优化即可。
方案二:修改MP的配置
方式1,我的项目里有 MybatisPlusConfig 配置类,所以采用以下方式解决方法。
方式2,有的项目是配置文件的形式,那么修改这个值为1000即可。
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="databaseIdProvider" ref="databaseIdProvider" />
<property name="plugins">
<array>
<bean id="paginationInterceptor"
class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql" />
</bean>
<bean id="performanceInterceptor"
class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor" >
<property name="maxTime" value="1000" />
<property name="format" value="true" />
</bean>
</array>
</property>
</bean>
以上就是针对这个错误给出的两种解决方案,可根据情况具体解决问题。
完结!
还没有评论,来说两句吧...