
-- 哈希索引: MySql中仅memory支持,InnoDB有自动哈希索引(当注意到某些索引用的频繁时开启) 对于不支持哈希的,可模拟创建如下: 假如本身以URL为索引如下: SELECT id FROM url WHERE url='http://www.mysql.com'; 以上做法存储大,查询性能慢,对此可额外添加一索引列(url_crc)用于伪哈希,并删除url上索引


②高性能索引策略: -- 独立的列:索引列单独放在比较符号一侧 SELECT id FROM test WHERE id+1=5; X -- 前缀索引:对于过大数据,模拟哈希支持不好,此时用前缀索引替代(如对BLOB、TEXT或过长VARCHAR必须用前缀索引) 首先判断索引建到多少合适:
select count(distinct left(time,5))/count(*) as no1,
count(distinct left(time,6))/count(*) as no2,
count(distinct left(time,7))/count(*) as no3,
count(distinct left(time,8))/count(*) as no4,
count(distinct left(time,9))/count(*) as no5,
count(distinct left(time,10))/count(*) as no6,
count(distinct time)/count(*) as no
from test.demo;

-- 多列索引:不少人会为每一个列建索引,这样联合查找时性能不佳(尽管mysql5.0以上会自动将独立索引合并为联合索引) 对于独立索引在OR时可用UNION ALL优化:

注:上面如果需要两个一起做条件,则选择较大值放前面(如no>no1,选no) -- 聚簇索引:暂不记录 -- 覆盖索引:select的数据列只用从索引中就能够取得,不必从数据表中读取,如下列子: EXPLAIN SELECTid,name FROM test.demo \G; #其中id,name列都为索引 有时覆盖索引很难达到,这时候就部分覆盖(延迟关联):select count(distinct id)/count(*) as no1,
count(distinct uid)/count(*) as no
from test.demo;


注:where尽可能将范围查询置于索引后面,如in、between.