当我们往表中插入数据时,如果表中有索引,则会给插入操作增加更多的工作量。带来的好处是可以提升查询效率。但是这种优劣该如何权衡,则需要通过数据对比来提供佐证。本文我们将对比没有索引、有一个普通索引、有一个唯一索引的性能差距。
结论
- 在只有一个索引(无论普通索引还是唯一索引)和无索引时性能差距不大。(在千万级数据量时,性能差距不超过一倍)。
- 有无索引会随着数据量增加,性能差异也会随之增加。(下面三条线中,有索引的线的斜率要大于无索引的线,所以随着数据量增加,性能差距也会增加)
实验数据
上图中,“无索引”是指导入目标表没有索引;“普通索引”是指导入目标表只有一个普通索引;“唯一索引”是指导入目标表只有一个唯一索引。
可以看到在当前测试场景下,“普通索引”和“唯一索引”的性能接近。
“无索引”和“有索引”都是呈线性变化,即不会因为数量增加,每个方案会变得更加糟糕。(比如指数型变化,那就意味着更加糟糕)
有无索引的性能差异会随着数据量增加而增加,因为两者斜率不同,即喇叭口会越来越大。
测试环境
见《Mysql使用中的性能优化——搭建Mysql的监测服务》
测试方法解读
上图中无索引表结构
DROP TABLE IF EXISTS test_insert_no_index;
CREATE TABLE test_insert_no_index (id SERIAL PRIMARY KEY,name TEXT
) engine=InnoDB;
有普通索引表结构
CREATE TABLE test_insert_normal_index (id SERIAL PRIMARY KEY,name TEXT,INDEX name_index (name(32))
) engine=InnoDB;
有唯一索引表结构
CREATE TABLE test_insert_unique_index (id SERIAL PRIMARY KEY,name TEXT,UNIQUE INDEX name_index (name(32))
) engine=InnoDB;
然后使用类似下面的指令进行差距插入
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 1000000);
完整测试代码
-- 通过从其他表中读取数据源,写入到目标表中,测试索引的比较操作
DROP DATABASE IF EXISTS testdb;
CREATE DATABASE IF NOT EXISTS testdb;
USE testdb;DROP TABLE IF EXISTS test_insert_src;
CREATE TABLE test_insert_src (id SERIAL PRIMARY KEY,name TEXT
) engine=InnoDB;DROP PROCEDURE IF EXISTS test_insert_proc_batch_bulk;
DELIMITER //
CREATE PROCEDURE test_insert_proc_batch_bulk(IN name TEXT, IN fromNum INT, IN toNum INT)
BEGINDECLARE i INT DEFAULT fromNum;DECLARE new_names TEXT DEFAULT '';SET @sql = 'INSERT INTO test_insert_src (name) VALUES ';WHILE i < toNum DOSET new_names = CONCAT(name, i);SET i = i + 1;SET @sql = CONCAT(@sql, '("', new_names, '"),');END WHILE;SET @sql = LEFT(@sql, LENGTH(@sql) - 1);PREPARE stmt FROM @sql;EXECUTE stmt;DEALLOCATE PREPARE stmt;COMMIT;
END //
DELIMITER ;DROP PROCEDURE IF EXISTS test_insert_proc_batch;
DELIMITER //
CREATE PROCEDURE test_insert_proc_batch(IN name TEXT, IN count INT, IN step INT)
BEGINDECLARE i INT DEFAULT 0;WHILE i < count DOCALL test_insert_proc_batch_bulk(name, i, i + step);SET i = i + step;END WHILE;COMMIT;
END //
DELIMITER ;DROP TABLE IF EXISTS test_insert_no_index;
CREATE TABLE test_insert_no_index (id SERIAL PRIMARY KEY,name TEXT
) engine=InnoDB;DROP TABLE IF EXISTS test_insert_normal_index;
CREATE TABLE test_insert_normal_index (id SERIAL PRIMARY KEY,name TEXT,INDEX name_index (name(32))
) engine=InnoDB;DROP TABLE IF EXISTS test_insert_unique_index;
CREATE TABLE test_insert_unique_index (id SERIAL PRIMARY KEY,name TEXT,UNIQUE INDEX name_index (name(32))
) engine=InnoDB;TRUNCATE TABLE test_insert_src;
CALL test_insert_proc_batch('test', 10000000, 5000);TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 1000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 2000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 3000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 4000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 5000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 6000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 7000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 8000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src LIMIT 9000000);
TRUNCATE TABLE test_insert_no_index;
INSERT INTO test_insert_no_index (SELECT * FROM test_insert_src);TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 1000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 2000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 3000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 4000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 5000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 6000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 7000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 8000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src LIMIT 9000000);
TRUNCATE TABLE test_insert_normal_index;
INSERT INTO test_insert_normal_index (SELECT * FROM test_insert_src);TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 1000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 2000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 3000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 4000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 5000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 6000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 7000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 8000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src LIMIT 9000000);
TRUNCATE TABLE test_insert_unique_index;
INSERT INTO test_insert_unique_index (SELECT * FROM test_insert_src);