본문 바로가기

DB/Mysql

mysql insert 속도에 대하여 (Speed of INSERT Statements)


다른 mysql에서 대량의 row를 insert하기위해서 insert의 capacity를 알아보려고 한다.

mysql sql 사이트의 insert 속도에 대한 페이지이다. 
http://dev.mysql.com/doc/refman/5.0/en/insert-speed.html

  • Connecting: (3)

  • Sending query to server: (2)

  • Parsing query: (2)

  • Inserting row: (1 × size of row)

  • Inserting indexes: (1 × number of indexes)

  • Closing: (1)


위에서 괄호 안의 숫자는 차지하는 비율에 대한 숫자이다. 즉, Connection을 맺는 작업이 Closing하는 작업 보다 3배의 시간이 든다는 것이다. 
결국, 넣으려는 row의 size가 크면 저 비용들은 거의 문제가 되지 않게 되겠지만, 그래도 connection등의 작업을 줄일 수 있다면 좋긴 할 것이다. 


If you are inserting many rows from the same client at the same time, use INSERT statements with multiple VALUESlists to insert several rows at a time. This is considerably faster (many times faster in some cases) than using separate single-row INSERT statements. If you are adding data to a nonempty table, you can tune thebulk_insert_buffer_size variable to make data insertion even faster. See Section 5.1.3, “Server System Variables”.

insert에서 muti values를 지정하므로써 한 쿼리로 여러개의 row를 insert하는 방법이 속도를 높일 수 있다고 한다. 
당연하겠지. connection이나, query parsing, sending query 등의 비용이 줄어들 테니까...
또 Server 변수 bulk_insert_buffer_size를 조작해 봄으로 써 insert속도를 높일 수 있다고 한다. 

하지만 위의 방법은 현재 내가 진행해야하는 작업같이 row가 클 수 있는 경우에는 진행이 어려울 수 있어 보인다. 
작은 row들을 한꺼번에 buffering해서 보낸다고 봤을 때는 의미가 있겠다만 말이다. 

INSERT DELAYED Syntax


insert만 하는 것이 아니라 현재 select와 update가 같이 있는 경우에는 insert delayed를 사용하므로써 더 빠른 insert를대할 수 있다는 말도 있다. 하지만 내가 알기로 이경우는 read 등의 lock이 지속 적으로 존재하면 언제 insert 작업이 완료될지 보장할 수 없다는 단점이 있다. 즉, update가 바로 반영되어야하는 서비스에서는 문제가 될 수 있다. 

그리고 지금 내가 진행하려고 하는 select, insert가 없는 상황에서 insert의 속도를 높여주는데는 어떤 도움도 되지 않는다. 그냥 한번 읽어본 것을 정리 해본다. 

여러 mysql DB에서 일부분을 select하여 하나의 DB로 inserting하는 작업은 어떻게 하는 것이 가장 빠를 까? 
고민 중이다.