首页 > 解决方案 > Why am I seeing an equal number of SELECTs in MySQL Workbench, when I am only making INSERTs from a Java program?

问题描述

I am generating random test data and INSERT-ing them into a MySQL (8.0.20) database. I have a simple Java program that creates 100 threads, each of which will insert 100,000 records into a simple table.

Table Description

mysql> desc item;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int          | NO   | PRI | NULL    |       |
| name  | varchar(255) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

DataGenerator.java

public class DataGenerator implements Runnable {
    private static final String URL = "jdbc:mysql://localhost:3306/todo";
    private static final String USR = "root";
    private static final String PWD = "root";
    private static final AtomicInteger itemId = new AtomicInteger(1);

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new DataGenerator()).start();
        }
    }

    @Override
    public void run() {
        try (Connection con = DriverManager.getConnection(URL, USR, PWD)) {
            PreparedStatement ps = con.prepareStatement("insert into item values (?, ?)");
            for (int i = 0; i < 100000; i++) {
                ps.setInt(1, itemId.getAndIncrement());
                ps.setString(2, String.valueOf(new Random().nextInt()));
                ps.execute();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

I am using the MySQL Workbench Performance Dashboard to see what's happening. It shows an INSERT rate of around 18K/second. But it also shows a SELECT rate of 18K/second!

Screenshot showing equal number of INSERTs and SELECTs

Why are so many SELECTs happening, when I am only doing INSERTs?

Notes about the environment

标签: mysqlmysql-workbench

解决方案


推荐阅读