mysql和Java中自动创建时间的效率对比

1、目的

mysql可以自动生成一些默认值,但是如果用Java先生成再插入会不会更加快。

2、sql表

CREATE TABLE IF NOT EXISTS `created_time_test` (
  `created_time1` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `created_time2` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `created_time3` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `created_time4` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
  `created_time5` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8

3、数据来源

根据当前时间戳创造一系列时间

  • 一、插入100万条数据(即生成500万个时间)
  • 二、使用批处理减少交互时间

4、Java代码

import java.sql.*;

public class Main {

    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/test_base?serverTimezone=UTC";

    static final String USER = "root";
    static final String PASS = "123456";

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        long start = System.currentTimeMillis();
        insertByJavaCreate();
        long end = System.currentTimeMillis();
        System.out.println("运行时间为:"+ (end-start) + "ms");
    }

    public static void insertByMysqlCreate() throws ClassNotFoundException, SQLException {
        Class.forName(JDBC_DRIVER);
        Connection con = DriverManager.getConnection(DB_URL,USER,PASS);
        con.setAutoCommit(false);
        String sql = "insert into created_time_test values()";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        for(int i = 1; i <= 1000000; i++){
            preparedStatement.addBatch();
            if(i % 1000 == 0){
                preparedStatement.executeBatch();
                con.commit();
            }
        }
    }
    
    public static void insertByJavaCreate() throws ClassNotFoundException, SQLException {
        Class.forName(JDBC_DRIVER);
        Connection con = DriverManager.getConnection(DB_URL,USER,PASS);
        con.setAutoCommit(false);
        String sql = "insert into created_time_test values(?,?,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        for(int i = 1; i <= 1000000; i++){
            for(int j = 1; j <= 5; j++){
                Timestamp timestamp = new Timestamp(System.currentTimeMillis());
                preparedStatement.setTimestamp(j, timestamp);
            }
            preparedStatement.addBatch();
            if(i % 1000 == 0){
                preparedStatement.executeBatch();
                con.commit();
            }
        }
    }
}

5、mysql自动生成时间

第一次: 运行时间为:189901ms

第二次:运行时间为:191258ms

第三次:运行时间为:192170ms

平均时间:191109ms

5、使用Java自动生成时间

第一次: 运行时间为:225758ms

第二次: 运行时间为:220434ms

第三次: 运行时间为:214352ms

平均时间: 220181ms

6、结果分析

  • 1、可以看出使用Java自动生成时间比mysql自动生成时间慢,相差半分钟左右
  • 2、可能的原因(个人猜测)
    • a、字符串拼接需要时间
    • b、拼接后,sql语句更长,传输到mysql花的时间更多
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值