2012-07-26 2 views
-1

그래서 Net Beans에서 실행 시간 등을 이해하는 데 도움이되는 클래스 용 MySQL/Java 프로그램을 개발 중입니다.MySQL/JAVA 삽입 속도

그래서 사용자가 6 개의 테이블 각각에 x 개의 튜플을 입력 할 수 있도록 java 프로그램을 만들었습니다. 그리고 우리는 너무 많은 125000 개의 튜플까지 데이터를 입력해야합니다. 글쎄, 60000 개의 튜플에 대해서만 문제가 있었고, 프로그램이 실행될 때 60000 개의 튜플을 삽입하는 데 약 30 분이 걸렸습니다.

필자는 결국 테이블에 125000 개의 튜플을 입력해야하고이 인서트를 더 빠르게 만드는 방법을 알아야 할 필요가 있을까요? 어떤 아이디어?

코드

/* 
* 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 
); 
+0

많은 이유로 인해 삽입 속도가 느려질 수 있습니다. 우리는 몇 가지 코드와 mySQL 데이터베이스가 어떻게 설정되어 있는지 살펴볼 필요가 있습니다. –

+0

자동 삽입을 사용하지 않도록 설정하여 각 삽입이 개별적으로 커밋되지 않도록 할 수 있습니다. http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/Connection.html#setAutoCommit(boolean) – Michael

답변

1

여기 당신이보고 싶을 수있는 몇 가지있어, 코드에 너무 가까이 모습을했다 :

  1. PreparedStatement 문이 컴파일 할 필요가 없기 때문에 아마 조금 도움이 될 것입니다 모든 요청.
  2. 일괄 처리로 명령문을 실행하십시오. PreparedStatement의 addBatch() 메소드를 살펴 보자. 예를 들어 1000 배치를 추가하면 executeBatch()를 사용하여 실행할 수 있습니다. 연결시 'rewriteBatchedStatements'설정과 함께이 작업을 수행하면 MySQL 드라이버가 몇 가지 최적화 작업을 수행 할 수 있습니다.
  3. 각 "배치"작업자를 소개합니다. 각 작업자는 지정된 크기의 배치를 삽입해야합니다. 단일 연결로만 작업하는 경우 한 번에 하나의 삽입 작업을 수행해야합니다. 다중 연결을 사용하여 작업하는 경우 (다중 스레드를 통해) 속도가 향상 될 가능성이 큽니다.
+0

예. 지금 당장 PreparedStatement를 살펴보고 있습니다. 대부분의 예제에서는 각 개별 항목에 대한 데이터를 입력합니다. 명령문에서 PreparedStatement로 전송할 링크 또는 예제 –

1

사용 대신의 PreparedStatement :

당신에게

브랜든

편집 감사 n Statement. SQL을 "사전 컴파일"하여 신속하게 호출합니다. 하지 않고

+0

을 참조하십시오. 어떻게 변경했으면 좋겠습니까? 부서 부서에있을 수도 있습니다. –

+0

이미 예제가 있습니다 : http://stackoverflow.com/questions/11633443/creating-multiple-tuples-in-multiple-tables. – SJuan76

+0

현재 : 몇 가지 피드백을 제공하고 올바른 답을 수락 한 것으로 표시하십시오. – SJuan76