create table test(
a int,
b int,
c int,
KEY a(a,b,c)
);mysql
优: select * from test where a=10 and b>50
差: select * from test where a50sql
优: select * from test order by a
差: select * from test order by b
差: select * from test order by c数组
优: select * from test where a=10 order by a
优: select * from test where a=10 order by b
差: select * from test where a=10 order by cide
优: select * from test where a>10 order by a
差: select * from test where a>10 order by b
差: select * from test where a>10 order by cspa
优: select * from test where a=10 and b=10 order by a
优: select * from test where a=10 and b=10 order by b
优: select * from test where a=10 and b=10 order by c排序
优: select * from test where a=10 and b=10 order by a
优: select * from test where a=10 and b>10 order by b
差: select * from test where a=10 and b>10 order by c索引
索引原则
1.索引越少越好
缘由:主要在修改数据时,第个索引都要进行更新,下降写速度。
2.最窄的字段放在键的左边
3.避免file sort排序,临时表和表扫描.table
MySQL能够为多个列建立索引。一个索引能够包括15个列。多列索引能够视为包含经过链接索引列的值而建立的值的排序的数组。MySQL按这样的方式使用多列索引:当你在WHERE子句中为索引的第1个列指定已知的数量时,查询很快,即便你没有指定其它列的值。ast
假定表具备下面的结构:test
CREATE TABLE test (
id INT NOT NULL,
last_name CHAR(30) NOT NULL,
first_name CHAR(30) NOT NULL,
PRIMARY KEY (id),
INDEX name (last_name,first_name)
);
name索引是一个对last_name和first_name的索引。索引能够用于为last_name,或者为last_name和first_name在已知范围内指定值的查询。所以,name索引用于下面的查询:
SELECT * FROM test WHERE last_name='Widenius';
SELECT * FROM test WHERE last_name='Widenius' AND first_name='Michael';
SELECT * FROM test WHERE last_name='Widenius' AND (first_name='Michael' OR first_name='Monty');
SELECT * FROM test WHERE last_name='Widenius' AND first_name >='M' AND first_name < 'N'; 然而,name索引不用于下面的查询:
SELECT * FROM test WHERE first_name='Michael';
SELECT * FROM test WHERE last_name='Widenius' OR first_name='Michael';