这次直接使用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&lt;#{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&lt;#{date,jdbcType=TIMESTAMP}) as tb)
    </delete>

    <delete id="deleteEmpByDate">
        delete from emp where cdate&lt;#{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秒的更多相关文章

  1. 【MyBatis】【SQL】删除最快纪录诞生,从一千万条记录中删除八百万条仅用2分6秒

    在 https://www.cnblogs.com/xiandedanteng/p/11669629.html 里我做个一个循环按时间查ID并删除之的程序,运行时间是4分7秒. 但是这个程序走了很多次 ...

  2. 【MyBatis】从一千万记录中批量删除八百万条,耗时4m7s

    批量删除主要借助了MySql的limit函数,其次用了in删除. 代码如下: package com.hy.action; import java.io.Reader; import java.uti ...

  3. SQL 从100万条记录中的到 成绩最高的记录

    从100万条记录中的到 成绩最高的记录 问题分析:要从一张表中找到成绩最高的记录并不难,有很多种办法,最简单的就是利用TOP 1 select top 1 * from student order b ...

  4. sql 查询某个条件多条数据中最新的一条数据或最老的一条数据

    sql 查询某个条件下多条数据中最新的一条数据或最老的一条数据 test_user表结构如下: 需求:查询李四.王五.李二创建的最初时间或者最新时间 1:查询最初的创建时间: SELECT * FRO ...

  5. 快还要更快,让PHP 7 运行更加神速

    导读 PHP 7 比5.x 快上很多,即使只有单纯的版本升级就已经很有感,不过大家还是希望它变得越来越快,这时再做些小调整就会更有fu,Let's try it! 事前准备 说到PHP 7,那一定跑不 ...

  6. SQL查找TCar表中同一辆车前后两条记录的CarId,两条记录中有多个字段值一样

    查询同一个表中某一字段值相同的记录 select * from 表名 where 字段 in(select 字段 from 表名 group by 字段 having count(1)>1) s ...

  7. 前端通信:ajax设计方案(八)--- 设计请求池,复用请求,让前端通信快、更快、再快一点

    直接进入主题,本篇文章有点长,包括从设计阶段,到摸索阶段,再到实现阶段,最后全面覆盖测试阶段(包括数据搜集清洗),还有与主流前端通信框架进行对比PK阶段. 首先介绍一下一些概念: 1. 浏览器的并发能 ...

  8. Sql server 中count(1) 与 sum(1) 那个更快?

    上一篇中,简单的说明了下 count() 与 sum() 的区别,虽然count 函数是汇总行数的,不过我汇总行数的时候经常是使用SUM(1) ,那么问题来了,count(1) 与 sum(1)  那 ...

  9. MyBatis 与 Hibernate 到底哪个更快?

    前言 由于编程思想与数据库的设计模式不同,生出了一些ORM框架. 核心都是将关系型数据库和数据转成对象型.当前流行的方案有Hibernate与myBatis. 两者各有优劣.竞争激烈,其中一个比较重要 ...

随机推荐

  1. 未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - 无法打开到 SQL Server 的连接) (.Net SqlClient Data Provider)

    今天连接服务器的SQL Server 遇到了一个很经典的问题 之前也曾多次遇到过 这次记录一下 按照之前经验 首先 开启了服务中的 SQL Server(MSSQLSERVER)和ASP.NET St ...

  2. Image Processing and Computer Vision_Review:A survey of recent advances in visual feature detection(Author's Accepted Manuscript)——2014.08

    翻译 一项关于视觉特征检测的最新进展概述(作者已被接受的手稿) 和A survey of recent advances in visual feature detection——2014.08内容相 ...

  3. Python 内置函数进制转换的用法(十进制转二进制、八进制、十六进制)

    ↓ 2进制 8进制 10进制 16进制 2进制 - bin(int(x, 8)) bin(int(x, 10)) bin(int(x, 16)) 8进制 oct(int(x, 2)) - oct(in ...

  4. linux中部署django项目

    通过Nginx部署Django(基于ubuntu) Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器 ...

  5. Java&Selenium借助AutoIt 实现非Input类型自动化上传文件

    通常情况下实现自动化上传文件,都是通过sendKeys函数直接将文件全路径传给页面空间就能完成,然而这种情况只能对Input类型的控件有效,对于非Input类型的控件可以借助AutoIt来完成 下载地 ...

  6. 关于C++ Builder Codegurad 问题的排查。

    关于C++ BUILDER6 我目前不知道有什么特别好的内存排查工具.尤其为了对付memory leak, (Eurekalog 这个工具内存泄漏主要针对delphi,BCB配置比较繁琐). 除了BC ...

  7. Mysql中查询索引和创建索引

    查询索引   show index from table_name 1.添加PRIMARY KEY(主键索引) ALTER TABLE `table_name` ADD PRIMARY KEY ( ` ...

  8. [Svelte 3] Use an onMount lifecycle method to fetch and render data in Svelte 3

    Every Svelte component has a lifecycle that starts when it is created, and ends when it is destroyed ...

  9. Redis:RedisHelper(5)

    /// <summary> /// Redis 助手 /// </summary> public class RedisHelper { /// <summary> ...

  10. PHP mysqli_options() 函数

    定义和用法 mysqli_options() 函数设置额外的连接选项,用于影响连接行为. mysqli_options() 函数可以被调用若干次来设置若干个选项. <?php $con=mysq ...