【MybatisPlus异常】The SQL execution time is too large, please optimize

Bertha 。 2022-10-17 10:46 290阅读 0赞

本文目录

一、项目背景

二、报错信息

三、报错原因

四、解决方案

方案一:优化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 !

  1. Caused by: org.apache.ibatis.exceptions.PersistenceException:
  2. ### Error updating database. Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
  3. ### The error may exist in com/uiotsoft/daily/module/dao/DailyReadRecordMapper.java (best guess)
  4. ### The error may involve com.uiotsoft.daily.module.dao.DailyReadRecordMapper.insert-Inline
  5. ### The error occurred while setting parameters
  6. ### SQL: INSERT INTO daily_job_read_record ( job_id, read_user, read_name ) VALUES ( ?, ?, ? )
  7. ### Cause: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
  8. at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
  9. at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:199)
  10. at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
  11. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  12. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  13. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  14. at java.lang.reflect.Method.invoke(Method.java:498)
  15. at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
  16. ... 132 common frames omitted
  17. Caused by: com.baomidou.mybatisplus.core.exceptions.MybatisPlusException: The SQL execution time is too large, please optimize !
  18. at com.baomidou.mybatisplus.core.toolkit.ExceptionUtils.mpe(ExceptionUtils.java:49)
  19. at com.baomidou.mybatisplus.core.toolkit.Assert.isTrue(Assert.java:38)
  20. at com.baomidou.mybatisplus.core.toolkit.Assert.isFalse(Assert.java:50)
  21. at com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor.intercept(PerformanceInterceptor.java:192)
  22. at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
  23. at com.sun.proxy.$Proxy194.update(Unknown Source)
  24. at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:54)
  25. at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
  26. at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)
  27. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  28. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  29. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  30. at java.lang.reflect.Method.invoke(Method.java:498)
  31. at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
  32. at com.sun.proxy.$Proxy193.update(Unknown Source)
  33. at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
  34. ... 138 common frames omitted

三、报错原因

出现此错误内容,说明 你写的SQL执行时间太长,它提示你让优化SQL语句。具体是哪一条SQL执行时间过长,上面也写明了。

至于为什么会导致报错,有两个原因:

第一、你写的SQL语句执行效率低,需要优化。

第二、mybatis-plus设置的最大时间配置的太短了。

那么既然知道原因了,解决方法就自然而然的知道了。

四、解决方案

根据上面两个原因,对应的解决方法也有两个,具体内容如下:

方案一:优化SQL语句

具体的sql优化我这里就不写,每个人的sql语句不一样,根据自己的sql做出优化即可。

方案二:修改MP的配置

方式1,我的项目里有 MybatisPlusConfig 配置类,所以采用以下方式解决方法。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDI5OTAyNw_size_16_color_FFFFFF_t_70

方式2,有的项目是配置文件的形式,那么修改2021060214414765.png这个值为1000即可。

  1. <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
  2. <property name="dataSource" ref="dataSource"/>
  3. <property name="databaseIdProvider" ref="databaseIdProvider" />
  4. <property name="plugins">
  5. <array>
  6. <bean id="paginationInterceptor"
  7. class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
  8. <property name="dialectType" value="mysql" />
  9. </bean>
  10. <bean id="performanceInterceptor"
  11. class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor" >
  12. <property name="maxTime" value="1000" />
  13. <property name="format" value="true" />
  14. </bean>
  15. </array>
  16. </property>
  17. </bean>

以上就是针对这个错误给出的两种解决方案,可根据情况具体解决问题。

完结!

发表评论

表情:
评论列表 (有 0 条评论,290人围观)

还没有评论,来说两句吧...

相关阅读