【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒
这次直接使用delete from emp where cdate<'2018-02-02',看看究竟会发生什么。
Mapper里写好SQL:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.hy.mapper.EmpMapper">
<select id="selectById" resultType="com.hy.entity.Employee">
select id,name,age,cdate as ctime from emp where id=#{id}
</select>
<insert id="batchInsert">
insert into emp(name,age,cdate)
values
<foreach collection="list" item="emp" separator=",">
(#{emp.name},#{emp.age},#{emp.ctime,jdbcType=TIMESTAMP})
</foreach>
</insert>
<insert id="singleInsert">
insert into emp(name,age,cdate)
values (#{name},#{age},#{ctime,jdbcType=TIMESTAMP})
</insert>
<select id="selectIdsByDate" resultType="java.lang.Long">
select id from emp where cdate<#{date,jdbcType=TIMESTAMP} limit 10000
</select>
<delete id="deleteByIds">
delete from emp where id in
<foreach collection="list" open="(" close=")" separator="," item="id" index="i">
#{id}
</foreach>
</delete>
<delete id="deleteByDate">
delete from emp where id in (select id from (select id from emp where cdate<#{date,jdbcType=TIMESTAMP}) as tb)
</delete>
<delete id="deleteEmpByDate">
delete from emp where cdate<#{date,jdbcType=TIMESTAMP}
</delete>
</mapper>
接口也做上:
package com.hy.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.hy.entity.Employee;
public interface EmpMapper {
Employee selectById(long id);
int batchInsert(List<Employee> emps);
// 用@Param标签指明和SQL的参数对应能避免出现org.apache.ibatis.binding.BindingException异常
int singleInsert(@Param("name")String name,@Param("age")int age,@Param("ctime")String ctime);
List<Long> selectIdsByDate(String date);
int deleteByIds(List<Long> ids);
int deleteByDate(String date);
int deleteEmpByDate(String date);
}
代码写好:
package com.hy.action;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import com.hy.entity.Employee;
import com.hy.mapper.EmpMapper;
public class BatchDelete3 {
private static Logger logger = Logger.getLogger(SelectById.class);
public static void main(String[] args) throws Exception{
long startTime = System.currentTimeMillis();
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(reader);
reader.close();
SqlSession session=ssf.openSession();
try {
EmpMapper mapper=session.getMapper(EmpMapper.class);
int changed=mapper.deleteEmpByDate("2018-02-02");
session.commit();
System.out.println("All deleted="+changed);
}catch(Exception ex) {
logger.error(ex);
session.rollback();
}finally {
session.close();
long endTime = System.currentTimeMillis();
logger.info("Time elapsed:" + toDhmsStyle((endTime - startTime)/1000) + ".");
}
}
// format seconds to day hour minute seconds style
// Example 5000s will be formatted to 1h23m20s
private static String toDhmsStyle(long allSeconds) {
String DateTimes = null;
long days = allSeconds / (60 * 60 * 24);
long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
long minutes = (allSeconds % (60 * 60)) / 60;
long seconds = allSeconds % 60;
if (days > 0) {
DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
DateTimes = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
DateTimes = minutes + "m" + seconds + "s";
} else {
DateTimes = seconds + "s";
}
return DateTimes;
}
}
然后塞了一千万条数据一执行,本以为会出现超时异常,回滚段异常,log区异常之类的,结果完全没有,反而还跑出了个最快结果:
All deleted=8035199 INFO [main] - Time elapsed:1m9s.
数据库的情况也证实了删除操作的正确性:


看来MySql这边千万级数据要删除也就是直接进行的事情,不知道在拿另一环境中的的21张三四百万级的Oracle数据库实验又会是怎样的结果。
凭感觉,无论是插值还是删除,我虚拟机上的MySql(mysql Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper)比单位实装的Oracle要迅速多了。
--END-- 2019年10月14日14:23:54
【MyBatis】【SQL】没有最快,只有更快,从一千万条记录中删除八百万条仅用1分9秒的更多相关文章
- 【MyBatis】【SQL】删除最快纪录诞生,从一千万条记录中删除八百万条仅用2分6秒
在 https://www.cnblogs.com/xiandedanteng/p/11669629.html 里我做个一个循环按时间查ID并删除之的程序,运行时间是4分7秒. 但是这个程序走了很多次 ...
- 【MyBatis】从一千万记录中批量删除八百万条,耗时4m7s
批量删除主要借助了MySql的limit函数,其次用了in删除. 代码如下: package com.hy.action; import java.io.Reader; import java.uti ...
- SQL 从100万条记录中的到 成绩最高的记录
从100万条记录中的到 成绩最高的记录 问题分析:要从一张表中找到成绩最高的记录并不难,有很多种办法,最简单的就是利用TOP 1 select top 1 * from student order b ...
- sql 查询某个条件多条数据中最新的一条数据或最老的一条数据
sql 查询某个条件下多条数据中最新的一条数据或最老的一条数据 test_user表结构如下: 需求:查询李四.王五.李二创建的最初时间或者最新时间 1:查询最初的创建时间: SELECT * FRO ...
- 快还要更快,让PHP 7 运行更加神速
导读 PHP 7 比5.x 快上很多,即使只有单纯的版本升级就已经很有感,不过大家还是希望它变得越来越快,这时再做些小调整就会更有fu,Let's try it! 事前准备 说到PHP 7,那一定跑不 ...
- SQL查找TCar表中同一辆车前后两条记录的CarId,两条记录中有多个字段值一样
查询同一个表中某一字段值相同的记录 select * from 表名 where 字段 in(select 字段 from 表名 group by 字段 having count(1)>1) s ...
- 前端通信:ajax设计方案(八)--- 设计请求池,复用请求,让前端通信快、更快、再快一点
直接进入主题,本篇文章有点长,包括从设计阶段,到摸索阶段,再到实现阶段,最后全面覆盖测试阶段(包括数据搜集清洗),还有与主流前端通信框架进行对比PK阶段. 首先介绍一下一些概念: 1. 浏览器的并发能 ...
- Sql server 中count(1) 与 sum(1) 那个更快?
上一篇中,简单的说明了下 count() 与 sum() 的区别,虽然count 函数是汇总行数的,不过我汇总行数的时候经常是使用SUM(1) ,那么问题来了,count(1) 与 sum(1) 那 ...
- MyBatis 与 Hibernate 到底哪个更快?
前言 由于编程思想与数据库的设计模式不同,生出了一些ORM框架. 核心都是将关系型数据库和数据转成对象型.当前流行的方案有Hibernate与myBatis. 两者各有优劣.竞争激烈,其中一个比较重要 ...
随机推荐
- asp.net mvc4 学习1
1 简介:微软在很早就看到了基于windows系统的web开发平台的需求,这时便开始提出自己的解决方案即微软的第一个基于web开发的平台ASP.再后来随着需求和性能的要求再2002年推出第二个解决方案 ...
- About Spring MVC
一.简介 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解 ...
- element之tree组件样式重写
1.改写实现效果: 2.页面代码 <el-tree :data="data" :props="defaultProps" @node-click=&quo ...
- css 之calc无效踩坑
踩坑: 1. height:calc(100vh-60); 无效 2.height:calc(100vh-60px); 无效 3.height:calc(100vh - 60px); 终于起效 总 ...
- docker images 导入和导出
目录 docker images 导入和导出 1.前言 2.docker image 的保存 3.docker image 的导入 docker images 导入和导出 1.前言 前提是现在有一个可 ...
- git回退到历史版本
问题描述 在开发的过程中,想要修改一个参数的命名.然后修改各种地方,并且push上码云的远程仓库.然后突然发现还要改很多地方,突然后悔不想改动了.那该怎么办呢? 处理步骤 回退本地的git版本 将本地 ...
- cifs
加入cifs 能把设备的目录挂载到我们的电脑上面 mount -t cifs -o username=Everyone //192.168.23.72/cifs /cif 如果当前是everyone ...
- BZOJ 2927: [Poi1999]多边形之战 (博弈)
题意 有一个凸多边形,顶点编号逆时针从0到n-1.现在这个n边形被剖分成n-2个三角形,给出这n-2个三角形的顶点,保证这是用n-3条不交叉的对角线划分出来的.现在第一个三角形是黑色,其他都是白色.两 ...
- BZOJ 2178: 圆的面积并 (辛普森积分)
code #include <set> #include <cmath> #include <cstdio> #include <cstring> #i ...
- ibatis查询列表跟总记录,都引用相同SQL
在查询记录集合跟查询记录总记录数的时候,我们需要所写的SQL要一样,那么可以都引用同一个SQL.写法如下: <sqlMap namespace="Server"> &l ...