标签:搜索 字符 font 出现 连续 根据 建立 substr sel
Select id from t where num is null 可以对null值设置默认值为-1(根据自己的实际情况设置) 判断null可以使用 select id from t where num = -1
select id from t where num = 1 or num = 5 /*可以优化为*/ select id from t where num = 1 unicon all select id form t where num = 5
/*连续条件*/ select id from t where num in (1,2,3) /*可以使用 between and */ select id from t where num between 1 and 3 /*更多可以使用exists 代替 in*/ select num from a where num in (select num from t) /*替换语句*/ select num from a where EXISTS (select num from b where a.num = b.num)
/*正常情况下,百分号在后面可以使用索引*/ select nickname from t where nickname like ‘DBA%‘ /*百分号在前面,不能使用索引,解决方案.改写sql,添加reverse索引*/ create index idx_t1_name on t1(reverse(name)) select name from t1 where reverse(name) like reverse(‘%adc‘); /*前后都有百分号,这种情况一般不能使用索引.*/ /*1.搜索条件字符串始终在字符串开始的固定位置出现,可以创建函数索进行优化,先创建subStr 函数索引,再使用like ‘abc%‘示例:*/ create index idx_substr_t1_name on t1 (substr(name,5,10)); select id, name ,name_type from t1 where substr(name, 5,10) like ‘abc%‘; /*2.搜索条件始终在字符串结尾的某个固定位置出现,可以创建函数组合索引进行优化,先创建reverse + substr 组合函数索引,再使用like reverse ‘%abc‘*/ create index idx_t1_reverse_name on t1(reverse(substr(name,1,length(name)-4))); select id,name,name_type from t1 where reverse(substr(name,1,length(name)-4)) like reverse(‘%abc‘) /*3.搜索字符串再不固定位置出现,优化方案,先建立普通索引列,改写sql*/ create index idx_t1_name on t1(name) select id,name,name_type from t1 where name in (select name from t1 where name like ‘%abc%‘)
标签:搜索 字符 font 出现 连续 根据 建立 substr sel
原文地址:https://www.cnblogs.com/shar-wang/p/11617855.html