关于索引失效那些事......
在执行SQL语句时,有时尽管设置了索引,但是在语句真正执行时却依然是全表扫描,并没有优化性能,这其实与我们所写的SQL语句有很大关系,以下简单列举几个导致索引失效的原因以及例子。
针对接下来的点,使用的例子,我们先简单建个book表,并插入一些数据,建立一个简单的索引。
-- 建立book表
create table if not exists book(
book_id int auto_increment,
book_name varchar(20) not null default '',
cat_id int not null,
book_price decimal(5,2) not null default 0.00,
cost decimal(5,2) not null default 0.00,
primary key(book_id)
)engine=innoDB charset utf8;
-- 插入数据
insert into book(book_name, cat_id, book_price,cost) values
('追风筝的人', 1, 25.33, 12.56),
('活着', 2, 20.45, 13.55),
('兄弟', 2, 20.36, 15.00),
('许三观卖血记', 2, 30.00, 24.56),
('喂,看见耳朵啦', 5, 24.23, 11.56),
('朝花惜时', 5, 20.56, 16.50),
('组词造句', 4, 40.99, 35.49),
('我会好好的', 3, 18.59, 10.46),
('花千骨', 3, 28.29, 25.36),
('撒野', 3, 45.88, 40.78),
(