MySQL/JAVA插入速度

MySQL/JAVA插入速度,java,mysql,insert,tuples,execution-time,Java,Mysql,Insert,Tuples,Execution Time,因此,我正在NetBeans中为类开发一个MySQL/Java程序,帮助我们理解执行时间等等 因此,我创建了java程序,以便用户可以在6个表中的每个表中输入x个元组。所以我们必须输入125000个元组的数据。我只讨论了60000个元组的问题,当程序执行时,插入60000个元组大约需要30分钟 不用说,最终我将不得不在一个表中输入125000个元组,我需要知道如何使这个插入更快?有什么想法吗 多谢各位 布兰登 编辑: 代码 使用PreparedStatement代替语句。它“预编译”SQL,以更

因此,我正在NetBeans中为类开发一个MySQL/Java程序,帮助我们理解执行时间等等

因此,我创建了java程序,以便用户可以在6个表中的每个表中输入x个元组。所以我们必须输入125000个元组的数据。我只讨论了60000个元组的问题,当程序执行时,插入60000个元组大约需要30分钟

不用说,最终我将不得不在一个表中输入125000个元组,我需要知道如何使这个插入更快?有什么想法吗

多谢各位

布兰登

编辑:

代码


使用
PreparedStatement
代替
语句
。它“预编译”SQL,以更快的方式对其进行连续调用。

在不仔细查看代码的情况下,您可能需要了解以下几点:

  • PreparedStatement
    可能会有所帮助,因为不必为每个请求编译语句
  • 成批执行语句。请查看PreparedStatement上的addBatch()方法。添加(比如)1000个批次后,可以使用executeBatch()执行它们。如果将此操作与连接上的设置“”结合使用,则MySQL驱动程序可能能够进行一些优化
  • 引入几个“插入”工人,每个工人负责插入给定大小的批次。如果只使用单个连接,则需要一次插入一个连接。如果您使用多个连接(通过多个线程),您很可能会看到速度的提高

  • 由于许多原因,您可能会遇到插入速度慢的问题。我们需要了解一些代码以及mySQL数据库的设置。您可以尝试禁用autommit以避免每次插入都被单独提交。看,你会如何改变我所拥有的。也许只是在部门部分。你已经有了一个例子:。是的,我现在正在调查预处理的报表。我看到的大多数示例都为每个独立列输入数据。要从语句传输到PreparedStatement的任何链接或示例
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package program2;
    
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    import java.util.Random;
    
    
    public class Program2 {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    
                // Variables
                int takesnum=0;
                int instructornum=0;
                int coursenum = 0;
                int studentnum = 0;
                int count = 0;
                int departmentnum =0;
                int counts= 0;
                int minimum=0;
                int x=0;
                int teachesnum=0;
                // Variables
    
                //Connection to the database
                Connection conn = null;
                String url = "jdbc:mysql://localhost:3306/";
                String dbName = "university2";
                String Driver = "com.mysql.jdbc.Driver";
                // Change the userName & password to what ever your credentials are.
                String userName = "root"; 
                String password = "121089bn";
                //Connection to the database
    
           try {
    
                InputStreamReader istream = new InputStreamReader(System.in);
                BufferedReader MyReader = new BufferedReader(istream);
    
                Class.forName(Driver).newInstance();
                conn = DriverManager.getConnection(url+dbName,userName,password);
                System.out.println("Connected");
    
                // Ask the user how many tuples in department table.
                System.out.println("How many tuples would you like to create in Department Table?");
    
                // Takes in as string the number then parse it to an int.
                String dept = MyReader.readLine();
                departmentnum = Integer.parseInt(dept);
    
    // ****************** Department Table ******************//  
                while (count < departmentnum)
                {
                    Statement st = conn.createStatement();
                    // Counts keeps the counter so the Primary Key is unique.
                    st.executeUpdate("Insert into department (dept_name, building, budget) values ('Dept "+counts+"', 'Voigt', '1200')");
                    count++;
                    counts++;
                }
    
    // ****************** Student Table ******************//                
                count=0;
                counts=0;
    
                System.out.println("How many tuples would you like to create in Student Table?");
                String student = MyReader.readLine();
                studentnum = Integer.parseInt(student);
    
                while (count < studentnum)
                {
                    int z=0;
                    int credit=128;
                    Random ran = new Random(); 
                    int range = departmentnum - minimum; 
                    x =  ran.nextInt(range) + minimum; 
    
                    Random random = new Random(); 
                    int totcred = credit - minimum; 
                    z =  random.nextInt(totcred) + minimum; 
    
                    Statement st = conn.createStatement();
                    st.executeUpdate("Insert into student (id, name, dept_name,tot_cred) select '"+counts+"', 'Student "+counts+"', dept_name, '"+z+"' from department where dept_name='Dept "+x+"'");
                    count++;
                    counts++;
                }
    
    // ****************** Course Table ******************//                 
                x=0;
                count=0;
                counts=0;   
    
                System.out.println("How many tuples would you like to create in Course Table?");
                String course = MyReader.readLine();
                coursenum = Integer.parseInt(course);
    
                while (count < coursenum)
                {
                    Random ran = new Random(); 
                    int range = departmentnum - minimum; 
                    x =  ran.nextInt(range) + minimum; 
                    int credit=5;
                    int z=0;
    
                    Random random = new Random(); 
                    int totcred = credit - minimum; 
                    z =  random.nextInt(totcred) + minimum; 
    
                    Statement st = conn.createStatement();
                    st.executeUpdate("Insert into course (course_id, title, dept_name,credits) select '"+counts+"', 'Computer Science "+counts+"', dept_name, '"+z+"' from department where dept_name='Dept "+x+"'");
                    count++;
                    counts++;
                }
    
    // ****************** Instructor Table ******************//                   
                x=0;
                count=0;
                counts=0;   
    
                System.out.println("How many tuples would you like to create in Instructor Table?");
                String instructor = MyReader.readLine();
                instructornum = Integer.parseInt(instructor);
    
                while (count < instructornum)
                {
                    Random ran = new Random(); 
                    int range = departmentnum - minimum; 
                    x =  ran.nextInt(range) + minimum; 
    
                    Statement st = conn.createStatement();
                    st.executeUpdate("Insert into instructor (id, name, dept_name,salary) select '"+counts+"', 'Instructor "+counts+"', dept_name, '10000' from department where dept_name='Dept "+x+"'");
                    count++;
                    counts++;
    
                }
    
    // ****************** Teaches Table ******************//                    
                x=0;
                count=0;
                counts=0;
    
                System.out.println("How many tuples would you like to create in Teaches Table?");
                String teaches = MyReader.readLine();
                teachesnum = Integer.parseInt(teaches);
    
                while (count < teachesnum)
                {
                    Random ran = new Random(); 
                    int range = instructornum - minimum; 
                    x =  ran.nextInt(range) + minimum; 
    
                    Random random = new Random(); 
                    int courserange = coursenum - minimum; 
                    int y =  random.nextInt(courserange) + minimum; 
    
                    int g = 100;
                    Random r = new Random();
                    int f = g - minimum;
                    int h = r.nextInt(f) + minimum;
    
                    int l = 100;
                    Random random1 = new Random();
                    int j = l - minimum;
                    int k = random1.nextInt(j) + minimum;
    
    
                    Statement st = conn.createStatement();
                    st.executeUpdate("Insert into teaches (id, course_id, semester, year) select id, course_id, 'Spr "+h+"', '20"+k+"' from course, instructor where instructor.id='"+x+"' and course.course_id='"+y+"'");
                    count++;
                    counts++;
                }
    
    // ****************** Takes Table ******************//                    
                x=0;
                count=0;
                counts=0;
    
                System.out.println("How many tuples would you like to create in Takes Table?");
                String takes = MyReader.readLine();
                takesnum = Integer.parseInt(takes);
    
                while (count < takesnum)
                {
                    Random ran = new Random(); 
                    int range = studentnum - minimum; 
                    x =  ran.nextInt(range) + minimum; 
    
                    Random random = new Random(); 
                    int courserange = coursenum - minimum; 
                    int y =  random.nextInt(courserange) + minimum;
    
                    int g = 100;
                    Random r = new Random();
                    int f = g - minimum;
                    int h = r.nextInt(f) + minimum;
    
                    int l = 100;
                    Random random1 = new Random();
                    int j = l - minimum;
                    int k = random1.nextInt(j) + minimum;
    
                    Statement st = conn.createStatement();
                    st.executeUpdate("Insert into takes (id, course_id, semester, year, grade) select id, course_id, 'Spr "+h+"', '20"+k+"', 'B' from course, student where student.id='"+x+"' and course.course_id='"+y+"'");
                    count++;
                    counts++;
                }
    
                conn.close();
           }
    
                catch (Exception e) {
                System.err.println("Error");
                System.err.println(e.getMessage());
                }
    }
    }
    
    drop database university2;
    create database university2;
    
    use university2;
    
    create table department
    (dept_name      varchar(20) primary key, 
     building       varchar(15), 
     budget             numeric(12,2)
    );
    
    create table student
    (ID             int,
     name           varchar(20) not null, 
     dept_name      varchar(20), 
     tot_cred       numeric(10,0),
     primary key (ID),
     foreign key (dept_name) references department(dept_name)
    
    );
    
    create table course
    (course_id      int, 
     title          varchar(50), 
     dept_name      varchar(15),
     credits        numeric(2,0),
     primary key (course_id),
     foreign key (dept_name) references department(dept_name)
    );
    
    
    
    create table instructor
    (ID         int, 
     name           varchar(20) not null, 
     dept_name      varchar(20), 
     salary         numeric(8,2),
     primary key (ID),
     foreign key (dept_name) references department(dept_name)
    );
    
    create table teaches
    (ID         int, 
     course_id      int,
     semester       varchar(6),
     year           numeric(4,0),
     primary key (ID, course_id, semester, year),
     foreign key (course_id) references course(course_id)
        on delete cascade,
     foreign key (ID) references instructor(ID)
        on delete cascade
    );
    
    
    create table takes
    (ID         int, 
     course_id      int,
     semester       varchar(6),
     year           numeric(4,0),
     grade              varchar(2),
     primary key (ID, course_id, semester, year),
     foreign key (course_id) references course(course_id)
        on delete cascade,
     foreign key (ID) references student(ID)
        on delete cascade
    );