【MySQL】实验:在一张有六百多万条条记录的表中按部门分组需要多少秒?答案3~4秒

有张emp表,填充了6258002条记录,主要字段有id,name,deptid,次要字段有17个。

按部门分组,统计每组数量,用3~4秒就能出结果。

SQL如下:

mysql> select count(*),deptid from emp group by deptid;
+----------+--------+
| count(*) | deptid |
+----------+--------+
|   313182 |      1 |
|   312314 |      2 |
|   313267 |      3 |
|   313192 |      4 |
|   312657 |      5 |
|   311752 |      6 |
|   313111 |      7 |
|   312870 |      8 |
|   312271 |      9 |
|   312546 |     10 |
|   312728 |     11 |
|   312665 |     12 |
|   313255 |     13 |
|   312911 |     14 |
|   312824 |     15 |
|   312838 |     16 |
|   312802 |     17 |
|   313176 |     18 |
|   313942 |     19 |
|   313699 |     20 |
+----------+--------+
20 rows in set (4.16 sec)

这表的建表语句如下:

CREATE TABLE emp
(
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(60) not null,
    deptid int not null,
    col03 int ,
    col04 int ,
    col05 int ,
    col06 int ,
    col07 int ,
    col08 int ,
    col09 int ,
    col10 int ,
    col11 int ,
    col12 int ,
    col13 int ,
    col14 int ,
    col15 int ,
    col16 int ,
    col17 int ,
    col18 int ,
    col19 int ,
    col20 int ,
    PRIMARY KEY (id)
)

使用如下程序给它填值:

package com.mq;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;

public class Test {

    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    private static final String DB_URL = "jdbc:mysql://127.0.0.1:3306/test";
    private static final String USER = "root";
    private static final String PSWD = "hy";

    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;

        try {
            Class.forName(JDBC_DRIVER).newInstance();
            conn = DriverManager.getConnection(DB_URL, USER, PSWD);
            conn.setAutoCommit(true);
            
            stmt = conn.createStatement();
            
            for(int j=0;j<10000;j++) {
                StringBuilder sb=new StringBuilder();
                sb.append("insert into emp(name,deptid) values");
                
                for(int i=0;i<1000;i++) {
                    String raw = "(''{0}'',{1}),";
                    Object[] arr = { "anonymous", rnd(1,20) };
                    String outStr = MessageFormat.format(raw, arr);
                    sb.append(outStr);
                }
                String sql=sb.toString();
                sql=sql.substring(0, sql.length()-1);
                System.out.println(sql);
                
                stmt.execute(sql);
            }
            


        } catch (Exception e) {
            System.out.print("DB/SQL ERROR:" + e.getMessage());
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException e) {
                System.out.print("Can't close stmt/conn because of " + e.getMessage());
            }
            
        }
    }
    
    private static int rnd(int min,int max) {
        return (int)(min+Math.random()*(max-min+1));
   }
}

一千六百万记录的Oracle表执行类似查询需要6~7秒,看来MySQL查询效率也不慢,至少没到分钟级别。