Python Connector ์ค์น
# pip install --egg mysql-connector-python-rf
ํ
์คํธ ์ฝ๋
from mysql.connector import connect
import time
p_list = []
current_time = time.time()
for i in range(100000):
p_list.append(
(
"Joshuaxx{}".format(i), "layoutyy{}".format(i),
"123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
"12345678901234567890123456789012345678901234567890", "nfc", current_time,
))
conn = connect(user='root', password='rainus!@#', host='localhost', database='infortab')
cur = conn.cursor()
cur_time = time.time()
cur.executemany('REPLACE INTO t_product (pr_code, layout_id, pr_info, page, nfc, last_modified) VALUES (%s, %s, %s, %s, %s, %s)', p_list)
diff_time = time.time() - cur_time
print('diff time : ', diff_time)
cur.close()
conn.commit()
conn.close()
Insert ํ๊ธฐ์ ์ DB ์ค์
# SET GLOBAL max_allowed_packet = 1073741824;
10๋ง๊ฐ ๋ฐ์ดํฐ ์
๋ ฅ
insert = 3.1s
replace = 10.94s
๊ฐ๊ฐ ๋ค๋ฅธ ํ
์ด๋ธ์ 10๋ง๊ฑด ์
๋ ฅ ์๋ ์ธก์ (REPLACE INTO)
1 stire : 10.45s
2 store : 10.47s
3 store : 11.18s
4 store : 11~13s
5 store : 15~17s
6 store : 17~19s
7 store : 22~24s
8 store : 24~26s
โป ์ฐ๊ฒฐ๋น Thread Pool์์ Thread๋ฅผ ํ ๋น ๋ฐ์ ์ฒ๋ฆฌ
1000๋ง๊ฑด ํ์ผ ์์ฑ (์๋ : 15.37s, ์ฉ๋ : 2.4G)
import time
p_list = []
current_time = time.time()
with open('/tmp/test.csv', 'w') as f:
for i in range(10000000):
f.write("Joshuaxx{},layoutyy{},123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,12345678901234567890123456789012345678901234567890,nfc,{}\n".format(i, i, current_time))
diff_time = time.time() - current_time
print('diff time : ', diff_time)
ํ์ผ ์
๋ ฅ ๋ฐ ์ถ๋ ฅ (/tmp ์์ ์ํ)
# SELECT * FROM t_product INTO OUTFILE '/tmp/test.csv'
# LOAD DATA INFILE '/tmp/test.csv' INTO TABLE t_product FIELDS TERMINATED BY ',';
๊ฒฐ๊ณผ : 1000๋ง๊ฑด 107.07์ด
๋ ์ฝ๋ ์ ์กฐํ
# SELECT COUNT(*) FROM ํ
์ด๋ธ
MyISAM ์ ํ
์ด๋ธ๋ณ ๋ ์ฝ๋์๋ฅผ ๊ธฐ์ตํ๊ณ ์์ด์ ๋น ๋ฆ
innoDB ๋ ๋๋ฆผ (ํธ๋ฆฌ๊ฑฐ๋ฅผ ์ด์ฉํ ์นด์ดํธ ํ
์ด๋ธ ๊ณ ๋ ค)
๋ ์ฝ๋ ์๋์ง๋ง ์ฒดํฌ ํ ๊ฒฝ์ฐ
# SELECT 1 FROM ํ
์ด๋ธ LIMIT 1
์นด์ดํธ ํ
์ด๋ธ (ํธ๋ฆฌ๊ฑฐ ์ฌ์ฉ)
# CREATE TABLE t_product_cnt (cnt INT UNSIGNED NOT NULL);
# INSERT INTO t_product_cnt VALUES (0);
# DELIMITER |
# CREATE TRIGGER t_product_insert AFTER INSERT ON t_product FOR EACH ROW BEGIN
UPDATE t_product_cnt SET cnt=cnt+1;
END|
# CREATE TRIGGER t_product_delete AFTER DELETE ON t_product FOR EACH ROW BEGIN
UPDATE t_product_cnt SET cnt=cnt-1;
END|
# DELIMITER ;