下面几种情况 mysql如何加索引1 select * from a where id=1 and email='[email protected]' order by name
2 select * from a where id=1 and email='[email protected]' group by firstname, secondname order by name
3 select * from a where id=1 and email='[email protected]' group by firstname, secondname
4 另外一般情况下 已经有索引的字段最好不要有null值,但是历史遗留的很多表实际上有索引的字段值中存在大量的nulll值
现在不能修改缺省的null, 清问在这些字段上sql语句会走索引吗?
2 select * from a where id=1 and email='[email protected]' group by firstname, secondname order by name
3 select * from a where id=1 and email='[email protected]' group by firstname, secondname
4 另外一般情况下 已经有索引的字段最好不要有null值,但是历史遗留的很多表实际上有索引的字段值中存在大量的nulll值
现在不能修改缺省的null, 清问在这些字段上sql语句会走索引吗?
2. ID一共有多少个不同的,分布如何,是否均匀分布? select count (distinct id)
3. email 一共有多少个不同的,分布如何,是否均匀分布NULL 值到是无谓
2. ID 全部不同 ,5万 , 他是自增长字段
3 email有4万不同容, 非均匀分布都是非均匀分布
如果这样,什么额外的索引都不需要了。1 select * from a where id=1 and email='[email protected]' order by name
2 select * from a where id=1 and email='[email protected]' group by firstname, secondname order by name
3 select * from a where id=1 and email='[email protected]' group by firstname, secondname这三个SQL语句均只返回一条记录。ID为自增,建表的时候就应该已经有索引了。
1 select * from a where email='[email protected]' order by name
2 select * from a where email='[email protected]' group by firstname, secondname order by name
3 select * from a where email='[email protected]' group by firstname, secondname
这里email有重复的 , name firstname, secondname 等都有大量重复
* 对不同的索引键做ORDER BY: Select * FROM t1 orDER BYkey1,key2;
* 在非连续的索引键部分上做ORDER BY: Select * FROM t1 Wherekey2=constantorDER BYkey_part2;
* 同时使用了ASC和DESC: Select * FROM t1 orDER BYkey_part1DESC,key_part2ASC;
* 用于搜索记录的索引键和做ORDER BY的不是同一个: Select * FROM t1 Wherekey2=constantorDER BYkey1;
* 有很多表一起做连接,而且读取的记录中在ORDER BY中的字段都不全是来自第一个非常数的表中(也就是说,在EXPLAIN分析的结果中的第一个表的连接类型不是const)。
* 使用了不同的ORDER BY和GROUP BY表达式。
* 表索引中的记录不是按序存储。例如,HASH和HEAP表就是这样。 通过执行EXPLAIN Select ... orDER BY,就知道MySQL是否在查询中使用了索引。如果Extra字段的值是Using filesort,则说明MySQL无法使用索引。详情请看"7.2.1EXPLAINSyntax (Get Information About aSelect)"。当必须对结果进行排序时,MySQL 4.1 以前它使用了以下filesort算法:
1. 根据索引键读取记录,或者扫描数据表。那些无法匹配Where分句的记录都会被略过。
2. 在缓冲中每条记录都用一个‘对’存储了2个值(索引键及记录指针)。缓冲的大小依据系统变量sort_buffer_size的值而定。
3. 当缓冲慢了时,就运行 qsort(快速排序)并将结果存储在临时文件中。将存储的块指针保存起来(如果所有的‘对’值都能保存在缓冲中,就无需创建临时文件了)。
4. 执行上面的操作,直到所有的记录都读取出来了。
5. 做一次多重合并,将多达MERGEBUFF(7)个区域的块保存在另一个临时文件中。重复这个操作,直到所有在第一个文件的块都放到第二个文件了。
(email,firstname,secondname,name)
这两个索引。