How to Insert 100000000 Data to Mysql in Fast Way

When you need to test perfermance of big size data.
How to insert 100000000 test data in a fast way.

Write a python script to do this:
For perfermance, create a mysql session will need some time, so if one time insert more data will less the time.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import mysql.connector
from datetime import datetime, timedelta
import time

TAG_ACTIVITY = 'tag_activity'

def connect(dictionary=True):
con = mysql.connector.connect(
user='root',
password='root',
database='root',
use_unicode=True
)
cursor = con.cursor(dictionary=dictionary, buffered=True)
return con, cursor

def release(con, cursor):
cursor.close()
con.close()

def sqlExecQuery(sql):
conn, cursor = connect()
cursor.execute(sql)
result = cursor.fetchall()
release(conn, cursor)
return result

def sqlExecNoQuery(sql):
conn, cursor = connect()
try:
cursor.execute(sql)
conn.commit()
except Exception as err:
conn.rollback()
print str(err)
release(conn, cursor)
return

def insert():
sql = "INSERT INTO `data` (`type`, `value`, `data`, `interval_ms`, `time`) VALUES ('', '', '0', '60000', '2018-06-04 07:18:05')"
for i in range(10000):
sql = sql + "," + " ('', '', '0', '60000', '2018-06-04 07:18:05')
return sqlExecNoQuery(sql)

def getCount():
sql = "select count(*) from data"
print sqlExecQuery(sql)

for i in xrange(10000):
insert()
if i%100 == 0:
getCount()

Btw, you can customize the data you want to insert.