【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. 两者各有优劣.竞争激烈,其中一个比较重要 ...
随机推荐
- python3使用pytesseract进行验证码识别
pytesseract介绍 1.Python-tesseract是一个基于google's Tesseract-OCR的独立封装包: 2.Python-tesseract功能是识别图片文件中文字,并作 ...
- 如何在Marketing Cloud里创建extension field扩展字段
首先在Marketing Cloud里找到创建扩展字段的tile入口,搜索关键字extension: 这会进入Fiori应用"Custom fields",能看到系统里所有创建好的 ...
- Java检查异常和非检查异常,运行时异常和非运行时异常的区别
通常,Java的异常(包括Exception和Error)分为检查异常(checked exceptions)和非检查的异常(unchecked exceptions).其中根据Exception异常 ...
- impala 建表时报错,不支持中文
1.错误信息 (1366, "Incorrect string value: '\\xE6\\x8E\\x88\\xE6\\x9D\\x83...' for column 'search' ...
- 学java编程软件开发,非计算机专业是否能学
近几年互联网的发展越来越好,在国外,java程序员已经成为高薪以及稳定职业的代表,虽然国内的有些程序员很苦逼,但是那只是少数,按照国外的大方向来看,程序员还是一个很吃香的职业.根据编程语言的流行程度, ...
- Oracle笔记(十四) 用户管理
SQL语句分为三类:DML.DDL.DCL,之前已经讲解完了DML和DDL,现在就差DCL操作的,DCL主要表示的是数据库的控制语句,控制的就是操作权限,而在DCL之中,主要有两个语法:GRANT.R ...
- [六省联考2017]分手是祝愿——期望DP
原题戳这里 首先可以确定的是最优策略一定是从大到小开始,遇到亮的就关掉,因此我们可以\(O(nlogn)\)的预处理出初始局面需要的最小操作次数\(tot\). 然后容(hen)易(nan)发现即使加 ...
- httpclient发邮件
package com.chuanglan; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.L ...
- 题解 [CF525D] Arthur and Walls
题面 解析 首先考虑将一个\('*'\)变成\('.'\)后会形成什么, 显然至少是一个\(2\times 2\)的矩形. 因为\(1\times 1\)和\(1\times 2\)的改了没用啊, 而 ...
- Educational Codeforces Round 33 (Rated for Div. 2) B题
B. Beautiful Divisors Recently Luba learned about a special kind of numbers that she calls beautiful ...