MySQL有一个很有意思的索引类型,叫做前缀索引,它可以给某个文本字段的前面部分单独做索引,从而降低索引的大小。
其实,Oracle也有类似的实现,对于文本,它可以通过substr的函数索引,实现同样甚至更多的功能。另外,经过探索,我们发现,原来数字和时间字段,在Oracle也可以实现类似的功能。
MySQL的前缀索引指的是对指定的栏位的前面几位建立的索引。
Altertable Table_Name add key(column_name(prefix_len));Createindex index_name on Table_Name(column_name(prefix_len));Select ..from table_name where column_name=’…’;前缀索引的最大的好处是降低索引的大小。另外,由于InnoDB单列索引长度不能超过767bytes,如果是text或者blob字段,直接建立索引可能会报错,而前缀索引可以绕过这一限制。
delimiter;;
dropFUNCTION if exists random_str;;
CREATEFUNCTION random_str(n int) RETURNS varchar(30000)
begin
declarereturn_str varchar(30000) default "";
declare iint default 0;
whilelength(return_str) < n do
setreturn_str=concat(return_str,md5(rand()));
endwhile;
returnsubstring(return_str,1,n);
end;;CREATETABLE TEST_PREFIX_IND (
ID INT(10) PRIMARY KEY AUTO_INCREMENT,
NORMAL_STR VARCHAR(20) ,
LONG_STR VARCHAR(1000),
TEXT_STR TEXT,
BLOB_STR BLOB
);drop procedure if exists init_test_prefix_ind;;
createprocedure init_test_prefix_ind(n int)
begin
declare iint default 0;
while i< n do
insertinto test_prefix_ind(NORMAL_STR,long_str, TEXT_STR,BLOB_STR)
values(random_str(20),random_str(rand()*1000+1),random_str(rand()*1000+1),random_str(rand()*300+1));
seti=i+1;
endwhile;
end;;
callinit_test_prefix_ind(10000);;尝试在类型为varchar(1000)的LONG_STR创建索引
altertable test_prefix_ind add key(LONG_STR);;成功了,但是Sub_part显示为767,表示系统自动创建了前缀长度为767的前缀索引;
mysql> alter table test_prefix_ind add key(text_str);
ERROR 1170 (42000): BLOB/TEXT column 'text_str' used in key specification without a key length
mysql> alter table test_prefix_ind add key(blob_str);;
ERROR 1170 (42000): BLOB/TEXT column 'blob_str' used in key specification without a key lengthalter table test_prefix_ind add key(text_str(30));;看看大小,528k(9520-8992), 远远小于LONG_STR的8992k.
alter table test_prefix_ind add key(blob_str(30));;Select count(distinct substr(long_str,1,5))/count(*) from test_prefix_ind;炫一点的写法,通过一些小技巧,可以在同一个SQL里遍历多个值,同时查看多个值的选择度。
select R,count(distinct substr(long_str,1,R))/count(*)
from
(SELECT @rownum:=ceil(@rownum*1.4) AS R
FROM (SELECT @rownum:=1) r,test_prefix_ind limit 1,10
) R,test_prefix_ind T
group by R;;alter table test_prefix_ind add key(long_str(5));看看大小,仅仅258k(10320-10064),远低于最早创建的8992k。
从前面的做法中,我们可以发现,前缀索引本质上就是把栏位的前N位作为索引,这个看起来,很像Oracle的函数索引。类似于:
Create index index_name on table_name(substr(column_name,1,<length>) );对于Oracle的函数索引,我们一个比较深的印象就是,where条件必须和函数索引里的表达式一致,才能利用上函数索引。但既然MySQL可以用前缀索引,作为老前辈的Oracle, 似乎应该也能实现才对。
Create table test_substr as
select object_id,object_name||dbms_random.string('x',dbms_random.value(1,1000) as object_name,created from all_objects ,
(select * from dual connect by level < 100)
where rownum < 10000;Create index test_substr_inx on test_substr(substr(object_name,1,5));神奇的事情发生了,的确走了索引,Oracle也支持前缀索引~~
select * from test_substr where object_name=:a;select * from test_substr where object_name=:a and substr(object_name,1,5)=substr(:a,1,5);有兴趣的,可以做个10053。Oracle内部实际进行执行计划解析的,就是这样一个SQL。
SELECT * FROM TEST_SUBSTR WHERE OBJECT_NAME=:A AND SUBSTR(OBJECT_NAME,1,5)=SUBSTR(:A,1,5);Create index test_substr_inx2 on test_substr(object_name);但Oracle仅止于此吗?我们在来试试看另一个SQL, 这次,我们在条件上也使用substr,但是长度不为5。
select * from test_substr
where substr(object_name,1,<N>)=:a;select * from test_substr
where substr(object_name,1,<N>)=:a and substr(object_name,1,5)=substr(:a,1,5);当然,如果把WHERE条件中substr换成小于5的值,就不再能用得上索引。因为无法直接换为等价的、又带有substr(object_name,1,5)的语句。
仅仅就这样吗?除了字符类型之外,数字类型和时间类型是否也支持?
Create index test_trunc_date_inx on test_substr(trunc(created));create index test_trunc_number on TEST_SUBSTR(trunc(object_id));create table test_scale (object_name varchar2(5));
insert into test_scale select substr(object_name,1,5) from all_objects;
create index test_scale_str_inx in test_scale(object_name);select * from test_scale where object_name = 'DBA_TABLES';神奇的事情再次发生,autotrace中db block gets/consistent gets都为0,这代表数据库根本就没去访问表。


















