• 【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查询效率也不慢,至少没到分钟级别。

  • 相关阅读:
    Angularjs中的缓存以及缓存清理
    举例子来说明Python引用和对象
    对象关系映射ORM
    Apache Storm 核心概念
    Linux如何查看哪个进程占用的SWAP分区比较多?
    MySQL彻底清除slave信息
    监控MySQL的时候监控用户应该怎么授权?
    MySQL用户密码修改
    专职DBA-Zabbix 3.0 for percona-server TokuDB
    防止rm强制删除
  • 原文地址:https://www.cnblogs.com/heyang78/p/15132240.html
Copyright © 2020-2023  润新知