CREATE DATABASE `ijiangtao_local_db_mysql` /*!40100 DEFAULT CHARACTER SET utf8 */; USE ijiangtao_local_db_mysql; DROP TABLE IF EXISTS t_user_action_log; CREATE TABLE `t_user_action_log` ( `id` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '主键id', `name` VARCHAR(32) DEFAULT NULL COMMENT '用户名', `ip_address` VARCHAR(50) DEFAULT NULL COMMENT 'IP地址', `action` INT4 DEFAULT NULL COMMENT '操作:1-登录,2-登出,3-购物,4-退货,5-浏览', `create_time` TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 1, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.1', 2, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 1, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.3', 1, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 2, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.4', 1, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 2, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.1', 1, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 2, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 1, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 3, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 5, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 2, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 2, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 3, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 3, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 5, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 3, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 3, CURRENT_TIMESTAMP); INSERT INTO t_user_action_log (name, ip_address, `action`, create_time) values ('LiSi', '8.8.8.2', 4, CURRENT_TIMESTAMP);
SELECT id, name, ip_address FROM t_user_action_log WHERE `action`=2;
EXPLAIN SELECT id, name, ip_address FROM t_user_action_log WHERE `action`=2;
ALTER TABLE t_user_action_log ADD INDEX (`action`);
CREATE INDEX indexName ON tableName (columnName(length));
CREATE INDEX index_ip_addr ON t_user_action_log (ip_address(16));
ALTER TABLE tableName ADD INDEX indexName(columnName);
ALTER TABLE t_user_action_log ADD INDEX ip_address_idx (ip_address(16)); SHOW INDEX FROM t_user_action_log;
id INT NOT NULL, columnName columnType, INDEX [indexName] (columnName(length)) );
SHOW INDEX FROM t_user_action_log;
ALTER TABLE t_user_action_log DROP INDEX index_ip_addr;
select ip_address from t_user_action_log where name='LiSi' group by action order by create_time;
CREATE INDEX indexName ON tableName (column1 DESC, column2 DESC, column3 ASC);
update ijiangtao_local_db_mysql.t_user_action_log set name='c1' where id<5;
首先创建数据库连接1,开启事务,并执行update语句
set autocommit=0; begin; update ijiangtao_local_db_mysql.t_user_action_log set name='c1' where id<5;
然后开启另外一个连接2,分别执行下面几个update语句
-- 没有被锁 update ijiangtao_local_db_mysql.t_user_action_log set name='c2' where id=6; -- 被锁 update ijiangtao_local_db_mysql.t_user_action_log set name='c2' where id=5;
连接1提交事务,连接2的id=1
和id=5
的数据行可以update成功了。
-- 在连接1提交事务 commit;
如果不使用索引
set autocommit=0; begin; update ijiangtao_local_db_mysql.t_user_action_log set name='c1' where ip_address='8.8.8.1';
explain select `action` from ijiangtao_local_db_mysql.t_user_action_log;
B树索引
Hash索引
CREATE TABLE shirts ( name VARCHAR(40), size ENUM('x-small', 'small', 'medium', 'large', 'x-large') ); INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'), ('polo shirt','small'); SELECT name, size FROM shirts WHERE size = 'medium';