Skip to content

Instantly share code, notes, and snippets.

@hoverruan
Created September 14, 2012 14:42
Show Gist options
  • Select an option

  • Save hoverruan/3722326 to your computer and use it in GitHub Desktop.

Select an option

Save hoverruan/3722326 to your computer and use it in GitHub Desktop.
MySQL性能测试
use test;
create table rs_myisam (
follower_id bigint(20) not null,
target_id bigint(20) not null,
created_at datetime not null,
primary key(follower_id,target_id)
) engine=MyISAM default charset=utf8;
create table rs_innodb (
follower_id bigint(20) not null,
target_id bigint(20) not null,
created_at datetime not null,
primary key(follower_id,target_id)
) engine=InnoDB default charset=utf8;
import MySQLdb as mysql
import sys
def insert_data(table):
try:
my_conn = mysql.connect(host='localhost', user='root', passwd='', db='test')
cursor = my_conn.cursor()
my_conn.autocommit(False)
for i in range(1000000):
id = i + 1
cursor.execute(
'insert into %s (follower_id, target_id, created_at) values(%d, %d, now())' %
(table, id, id))
if (id % 500) == 0:
my_conn.commit()
my_conn.commit()
finally:
my_conn.close()
print 'Done'
if __name__ == '__main__':
insert_data(sys.argv[1])

插入数据 autocommit = True

python insert_data.py

100000000 (一亿) 记录,文件大小:

$ ls -l
total 9404584
-rw-rw----  4 _mysql  wheel  2500000000  9 15 10:04 relationship.MYD
-rw-rw----  4 _mysql  wheel  2315130880  9 15 10:04 relationship.MYI
-rw-rw----  1 _mysql  wheel        8656  9 14 22:41 relationship.frm

创建index对文件大小的影响

创建 follower_id 的索引:

mysql> create index follower_idx on relationship (follower_id);
Query OK, 100000000 rows affected (17 min 21.25 sec)
Records: 100000000  Duplicates: 0  Warnings: 0

创建索引后的文件大小:

$ ls -l
total 12197000
-rw-rw----  1 _mysql  wheel  2500000000  9 15 23:57 relationship.MYD
-rw-rw----  1 _mysql  wheel  3744849920  9 16 00:03 relationship.MYI
-rw-rw----  1 _mysql  wheel        8656  9 15 23:49 relationship.frm
@hoverruan
Copy link
Author

一百万条记录插入比较 (MyISAM vs InnoDB)

从结果看来差别很小

$ time python insert_data.py rs_myisam
Done

real 1m32.043s
user 0m23.568s
sys 0m13.586s

$ time python insert_data.py rs_innodb
Done

real 1m31.275s
user 0m23.733s
sys 0m13.749s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment