show PROCESSLIST ; 查看进程
kill 15 ; 杀掉进程
create index idx_bankid on table (c_bankid) 创建索引
1、查看数据库所有索引
SELECT * FROM mysql.`innodb_index_stats` a WHERE a.`database_name` = '数据库名';
2、查看某一表索引
SELECT * FROM mysql.`innodb_index_stats` a WHERE a.`database_name` = '数据库名' and a.table_name like '%表名%';
或者
通过命令窗口show index from tb1001_container;
drop index index_bankid_js_code on js_code; 删除索引
问题: 两张表 js_bank js_code , 将一张表js_code中数据加到 另一张表js_bank 中,有字段重复,有字段不重复
7万数据
DROP TABLE IF EXISTS `js_bank`;
CREATE TABLE `js_bank` (
`code` varchar(255) DEFAULT NULL,
`codename` varchar(255) DEFAULT NULL,
`c_bankid` varchar(255) DEFAULT NULL,
`c_bankname` varchar(255) DEFAULT NULL,
`c_province` varchar(255) DEFAULT NULL,
`c_city` varchar(255) DEFAULT NULL,
`province` varchar(255) DEFAULT NULL,
`city` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
14万数据
DROP TABLE IF EXISTS `js_code`;
CREATE TABLE `js_code` (
`c_bankcode` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_bankid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_bankname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
INDEX `index_bankid_js_code`(`c_bankid`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
开始 update 语句
update js_bank jb LEFT JOIN js_code jc
on jb.c_bankid=jc.c_bankid
set
jb.c_bankid = jc.c_bankcode ,
jb.c_bankid_two = jc.c_bankid,
jb.c_bankname = jc.c_bankname
特别慢 , 1个多小时没成功
改为查询 insert 新表, 再修改表名
第三张表
DROP TABLE IF EXISTS `js_bank1`;
CREATE TABLE `js_bank1` (
`code` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`codename` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_bankid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_bankname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`province` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`city` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`c_bankid_two` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
insert 方法
insert into js_bank1 (code ,codename,c_bankid,c_bankname,c_province,c_city, province,city,c_bankid_two )
select js.code ,js.codename ,jc.c_bankcode ,jc.c_bankname ,js.c_province,js.c_city ,js.province ,js.city,jc.c_bankid
from js_bank js , js_code jc
where js.c_bankid = jc.c_bankid
也是特别慢, 1个多小时没成功
然后同时 创建 两张表 c_bankid 字段 索引 ,特别慢,。
不能insert 同时 创建 索引 ,
改良方法。
杀掉所有进程
重建 js_bank1 表。
执行 再 js_bank 和js_code 表创建 c_bankid 索引
CREATE INDEX index_bankid ON js_code (c_bankid) ;
CREATE INDEX index_bankid ON js_bank (c_bankid) ;
再重新执行
insert into js_bank1 (code ,codename,c_bankid,c_bankname,c_province,c_city, province,city,c_bankid_two )
select js.code ,js.codename ,jc.c_bankcode ,jc.c_bankname ,js.c_province,js.c_city ,js.province ,js.city,jc.c_bankid
from js_bank js , js_code jc
where js.c_bankid = jc.c_bankid
几十秒 就结束了 。
创建索引重要性,谨记。