mysql性能测试(索引)
首先,使用Talend随机生成一千万条数据:
数据库表中现在有1千万+的数据:
mysql> select count(*) from zhangchao; +----------+ | count(*) | +----------+ | 10040005 | +----------+ 1 row in set (2.50 sec)
不加索引的情况下,查询一条记录的时间为3.02s
mysql> select * from zhangchao where x = "bq2i2T"; +--------+--------+ | x | y | +--------+--------+ | bq2i2T | DRt5Mx | +--------+--------+ 1 row in set (3.02 sec) mysql> show create table zhangchao; +-----------+------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-----------+------------------------------------------------------------------------------------------------------------------------------------+ | zhangchao | CREATE TABLE `zhangchao` ( `x` varchar(100) DEFAULT NULL, `y` varchar(100) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-----------+------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
给表zhangchao的x列创建索引,耗时45.43s:
mysql> alter table zhangchao add index (x); Query OK, 0 rows affected (45.43 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table zhangchao; +-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------+ | zhangchao | CREATE TABLE `zhangchao` ( `x` varchar(100) DEFAULT NULL, `y` varchar(100) DEFAULT NULL, KEY `x` (`x`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
在执行同样的查询,耗时0.09s:
mysql> select * from zhangchao where x = "bq2i2T"; +--------+--------+ | x | y | +--------+--------+ | bq2i2T | DRt5Mx | +--------+--------+ 1 row in set (0.09 sec)
和没有加索引的性能差别:3.02/0.09 = 33.555倍