标签:echo default declare wait 执行 bsp inno val utf8
为了回答索引对数据库性能的影响,做了一次简单的实验。
MySQL 5.7.10 innodb 100万随机数据
CREATE TABLE `table1` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `col1` varchar(1000) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1000000 DEFAULT CHARSET=utf8;
myql中 执行call insert_calues(10000);
CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(1000) CHARSET utf8 BEGIN DECLARE chars_str VARCHAR(100) DEFAULT ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘; DECLARE return_str VARCHAR(1000) DEFAULT ‘‘; DECLARE i INT DEFAULT 0; WHILE i < n DO SET return_str = concat(return_str,substring(chars_str , FLOOR(1 + RAND()*62 ),1)); SET i = i +1; END WHILE; RETURN return_str; END;
DELIMITER ;; CREATE PROCEDURE `insert_values`( m INT) BEGIN SET @i=1; WHILE @i<m DO INSERT INTO TABLE1(col1) SELECT rand_string(10) ; SET @i=@i+1; END WHILE; END;; DELIMITER ;
shell 并发查询
ALTER TABLE table1 ADD INDEX col1(col1(10));
#time bash sql_test.sh Time: 0 real 0m0.313s user 0m0.308s sys 0m0.129s
删除索引并测试ALTER TABLE table1 DROP INDEX col1;
#time bash sql_test.sh Time: 10 real 0m9.989s user 0m0.322s sys 0m0.130s
测试使用的sql_test.sh内容如下
trap "exec 203>&-;exec 203<&-;exit 0" 2 mkfifo 203fifo exec 203<>203fifo rm -rf 203fifo #控制并发数量,如n<=20代表并发20次 for((n=1;n<=20;n++)) do echo >&203 done start=`date "+%s"` for i in {1..100} ; do read -u203 { key=$(openssl rand -base64 9) mysql test1 -e ‘select SQL_NO_CACHE id from table1 where col1 ="${key}"‘ echo >&203 }& done wait end=`date "+%s"` echo "Time: `expr $end - $start`" exec 203>&- exec 203<&-
总结
使用索引时,能极大提高SQL性能。
标签:echo default declare wait 执行 bsp inno val utf8
原文地址:http://www.cnblogs.com/firmament/p/7066906.html