zoukankan      html  css  js  c++  java
  • 【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查询效率也不慢,至少没到分钟级别。

  • 相关阅读:
    The Water Problem HDU-5443
    约瑟夫环1
    迪杰斯特拉算法1 HDU-1874
    [20200729NOIP提高组模拟T2]学数数——坎坷
    [20200728NOIP提高组模拟T4]有趣的有趣的家庭菜园——自闭了
    [20200728NOIP提高组模拟T1]Copy
    [20200728NOIP提高组模拟T2]愉快的logo设计
    [20200727NOIP提高组模拟T3]计算几何
    [20200727NOIP提高组模拟T2]走路
    [CQOI2015]任务查询系统
  • 原文地址:https://www.cnblogs.com/heyang78/p/15132240.html
Copyright © 2011-2022 走看看