Skip to content

Instantly share code, notes, and snippets.

@ymero
Forked from hoverruan/create_tables.sql
Created July 21, 2016 03:28
Show Gist options
  • Select an option

  • Save ymero/a1e2b689a187e600d0c504c1e81a5472 to your computer and use it in GitHub Desktop.

Select an option

Save ymero/a1e2b689a187e600d0c504c1e81a5472 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment