码迷,mamicode.com
首页 > 数据库 > 详细

汉字转全拼音函数优化方案(SQLServer),值得你看看

时间:2015-04-25 21:10:04      阅读:471      评论:0      收藏:0      [点我收藏+]

标签:

函数要求实现功能

select 函数名 (‘你好,我是追索‘)

返回的结果(ni hao , wo shi zhui suo)

 

解决方案一:

技术分享
解决方案一 

/*
 根据汉字获取全拼
 1.生成所有读音临时表
 2.根据Chinese_PRC_CS_AS_KS_WS 排序获取读音
*/
create function dbo.fn_Getquanpin1(@str varchar(100))
returns varchar(8000)
as
begin
 declare @re varchar(8000)
 --生成临时表
 declare @t table(chr nchar(1) collate  Chinese_PRC_CS_AS_KS_WS,py nvarchar(20)) 
 insert into @t select,a 
 insert into @t select,aes 
 insert into @t select,ai 
 insert into @t select,an 
 insert into @t select,ang 
 insert into @t select,ao 
 insert into @t select,ba 
 insert into @t select,bai 
 insert into @t select,baike 
 insert into @t select,baiwa 
 insert into @t select,ban 
 insert into @t select,bang 
 insert into @t select,bao 
 insert into @t select,be 
 insert into @t select,bei 
 insert into @t select,ben 
 insert into @t select,beng 
 insert into @t select,bi 
 insert into @t select,bian 
 insert into @t select,uu 
 insert into @t select,biao 
 insert into @t select,bie 
 insert into @t select,bin 
 insert into @t select,bing 
 insert into @t select,bo 
 insert into @t select,bu 
 insert into @t select,ca 
 insert into @t select,cai 
 insert into @t select,cal 
 insert into @t select,can 
 insert into @t select,cang 
 insert into @t select,cao 
 insert into @t select,ce 
 insert into @t select,cen 
 insert into @t select,ceng 
 insert into @t select,ceok 
 insert into @t select,ceom 
 insert into @t select,ceon 
 insert into @t select,ceor 
 insert into @t select,cha 
 insert into @t select,chai 
 insert into @t select辿,chan 
 insert into @t select,chang 
 insert into @t select,chao 
 insert into @t select,che 
 insert into @t select,chen 
 insert into @t select,cheng 
 insert into @t select,chi 
 insert into @t select,chong 
 insert into @t select,chou 
 insert into @t select,chu 
 insert into @t select,chuai 
 insert into @t select,chuan 
 insert into @t select,chuang 
 insert into @t select,chui 
 insert into @t select,chun 
 insert into @t select,chuo 
 insert into @t select,ci 
 insert into @t select,cis 
 insert into @t select,cong 
 insert into @t select,cou 
 insert into @t select,cu 
 insert into @t select,cuan 
 insert into @t select,cui 
 insert into @t select,cun 
 insert into @t select,cuo 
 insert into @t select,chua 
 insert into @t select,da 
 insert into @t select,dai 
 insert into @t select,dan 
 insert into @t select,dang 
 insert into @t select,dao 
 insert into @t select,de 
 insert into @t select,dem 
 insert into @t select,den 
 insert into @t select,deng 
 insert into @t select,di 
 insert into @t select,dia 
 insert into @t select,dian 
 insert into @t select,diao 
 insert into @t select,die 
 insert into @t select,dei 
 insert into @t select,dim 
 insert into @t select,ding 
 insert into @t select,diu 
 insert into @t select,dong 
 insert into @t select,dou 
 insert into @t select,du 
 insert into @t select,duan 
 insert into @t select,dug 
 insert into @t select,dui 
 insert into @t select,dun 
 insert into @t select,duo 
 insert into @t select,e 
 insert into @t select,en 
 insert into @t select,eng 
 insert into @t select,eo 
 insert into @t select,eol 
 insert into @t select,eos 
 insert into @t select,er 
 insert into @t select,fa 
 insert into @t select,fan 
 insert into @t select,fang 
 insert into @t select,fei 
 insert into @t select,fen 
 insert into @t select,feng 
 insert into @t select,fenwa 
 insert into @t select,fiao 
 insert into @t select,fo 
 insert into @t select,fou 
 insert into @t select,fu 
 insert into @t select,fui 
 insert into @t select,ga 
 insert into @t select,gai 
 insert into @t select,gan 
 insert into @t select,gang 
 insert into @t select,gao 
 insert into @t select,ge 
 insert into @t select,gei 
 insert into @t select,gen 
 insert into @t select,geng 
 insert into @t select,geu 
 insert into @t select,gib 
 insert into @t select,go 
 insert into @t select,gong 
 insert into @t select,gongfen 
 insert into @t select,gongli 
 insert into @t select,gou 
 insert into @t select,gu 
 insert into @t select,gua 
 insert into @t select,guai 
 insert into @t select,guan 
 insert into @t select,guang 
 insert into @t select,gui 
 insert into @t select,gun 
 insert into @t select,guo 
 insert into @t select,ha 
 insert into @t select,hai 
 insert into @t select,hal 
 insert into @t select,han 
 insert into @t select,hang 
 insert into @t select,hao 
 insert into @t select,haoke 
 insert into @t select,he 
 insert into @t select,hei 
 insert into @t select,hen 
 insert into @t select,heng 
 insert into @t select,heui 
 insert into @t select,ho 
 insert into @t select,hol 
 insert into @t select,hong 
 insert into @t select,hou 
 insert into @t select,hu 
 insert into @t select,hua 
 insert into @t select,huai 
 insert into @t select,huan 
 insert into @t select,huang 
 insert into @t select,hui 
 insert into @t select,hun 
 insert into @t select,huo 
 insert into @t select,geo 
 insert into @t select,hwa 
 insert into @t select,ji 
 insert into @t select,jia 
 insert into @t select,jialun 
 insert into @t select,jian 
 insert into @t select,jiang 
 insert into @t select,jiao 
 insert into @t select,jie 
 insert into @t select,jin 
 insert into @t select,jing 
 insert into @t select,jiong 
 insert into @t select,jiu 
 insert into @t select,jou 
 insert into @t select,ju 
 insert into @t select,juan 
 insert into @t select,jue 
 insert into @t select,jun 
 insert into @t select,ka 
 insert into @t select,kai 
 insert into @t select,kal 
 insert into @t select,kan 
 insert into @t select,hem 
 insert into @t select,kang 
 insert into @t select,kao 
 insert into @t select,ke 
 insert into @t select,ken 
 insert into @t select,keng 
 insert into @t select,keo 
 insert into @t select,keol 
 insert into @t select,keos 
 insert into @t select,keum 
 insert into @t select,ki 
 insert into @t select,kong 
 insert into @t select,kos 
 insert into @t select,kou 
 insert into @t select,ku 
 insert into @t select,kua 
 insert into @t select,kuai 
 insert into @t select,kuan 
 insert into @t select,kuang 
 insert into @t select,kui 
 insert into @t select,kun 
 insert into @t select,kuo 
 insert into @t select,kweok 
 insert into @t select,la 
 insert into @t select,lai 
 insert into @t select,lan 
 insert into @t select,lang 
 insert into @t select,lao 
 insert into @t select,le 
 insert into @t select,lei 
 insert into @t select,leng 
 insert into @t select,li 
 insert into @t select,lia 
 insert into @t select,lian 
 insert into @t select,liang 
 insert into @t select,liao 
 insert into @t select,lie 
 insert into @t select,lin 
 insert into @t select,ling 
 insert into @t select,liu 
 insert into @t select,liwa 
 insert into @t select,lo 
 insert into @t select,long 
 insert into @t select,lou 
 insert into @t select,lu 
 insert into @t select,lv 
 insert into @t select,lue 
 insert into @t select,luan 
 insert into @t select,lun 
 insert into @t select,luo 
 insert into @t select,m 
 insert into @t select,ma 
 insert into @t select,hweong 
 insert into @t select,mai 
 insert into @t select,man 
 insert into @t select,mang 
 insert into @t select,mangmi 
 insert into @t select,mao 
 insert into @t select,mas 
 insert into @t select,me 
 insert into @t select,mei 
 insert into @t select,men 
 insert into @t select,meng 
 insert into @t select,meo 
 insert into @t select,mi 
 insert into @t select,mian 
 insert into @t select,miao 
 insert into @t select,mie 
 insert into @t select,miliklanm 
 insert into @t select,min 
 insert into @t select,lem 
 insert into @t select,ming 
 insert into @t select,miu 
 insert into @t select,mo 
 insert into @t select,mol 
 insert into @t select,mou 
 insert into @t select,mu 
 insert into @t select,myeo 
 insert into @t select,myeon 
 insert into @t select,myeong 
 insert into @t select,na 
 insert into @t select,nai 
 insert into @t select,nan 
 insert into @t select,nang 
 insert into @t select,keg 
 insert into @t select,nao 
 insert into @t select,ne 
 insert into @t select,nei 
 insert into @t select,nem 
 insert into @t select,nen 
 insert into @t select,neus 
 insert into @t select,ngag 
 insert into @t select,ngai 
 insert into @t select,ngam 
 insert into @t select,ni 
 insert into @t select,nian 
 insert into @t select,niang 
 insert into @t select,niao 
 insert into @t select,nie 
 insert into @t select,nin 
 insert into @t select,ning 
 insert into @t select,niu 
 insert into @t select,nong 
 insert into @t select,nou 
 insert into @t select,nu 
 insert into @t select,nv 
 insert into @t select,nue 
 insert into @t select,nve 
 insert into @t select,nuan 
 insert into @t select,nun 
 insert into @t select,nung 
 insert into @t select,nuo 
 insert into @t select,o 
 insert into @t select,oes 
 insert into @t select,ol 
 insert into @t select,on 
 insert into @t select,ou 
 insert into @t select,pa 
 insert into @t select,pai 
 insert into @t select,pak 
 insert into @t select,pan 
 insert into @t select,pang 
 insert into @t select,pao 
 insert into @t select,pei 
 insert into @t select,pen 
 insert into @t select,peng 
 insert into @t select,peol 
 insert into @t select,phas 
 insert into @t select,phdeng 
 insert into @t select,phoi 
 insert into @t select,phos 
 insert into @t select,pi 
 insert into @t select,pian 
 insert into @t select,piao 
 insert into @t select,pie 
 insert into @t select丿,pianpang 
 insert into @t select,pin 
 insert into @t select,ping 
 insert into @t select,po 
 insert into @t select,pou 
 insert into @t select,deo 
 insert into @t select,ppun 
 insert into @t select,pu 
 insert into @t select,qi 
 insert into @t select,qia 
 insert into @t select,qian 
 insert into @t select,qiang 
 insert into @t select,qianke 
 insert into @t select,qianwa 
 insert into @t select,qiao 
 insert into @t select,qie 
 insert into @t select,qin 
 insert into @t select,kem 
 insert into @t select,qing 
 insert into @t select,qiong 
 insert into @t select,qiu 
 insert into @t select,qu 
 insert into @t select,keop 
 insert into @t select,quan 
 insert into @t select,que 
 insert into @t select,qun 
 insert into @t select,ra 
 insert into @t select,ram 
 insert into @t select,ran 
 insert into @t select,rang 
 insert into @t select,rao 
 insert into @t select,re 
 insert into @t select,ren 
 insert into @t select,reng 
 insert into @t select,ri 
 insert into @t select,rong 
 insert into @t select,rou 
 insert into @t select嶿,ru 
 insert into @t select,ruan 
 insert into @t select,rui 
 insert into @t select,run 
 insert into @t select,ruo 
 insert into @t select,sa 
 insert into @t select,saeng 
 insert into @t select,sai 
 insert into @t select,sal 
 insert into @t select,san 
 insert into @t select,sang 
 insert into @t select,sao 
 insert into @t select,se 
 insert into @t select,sed 
 insert into @t select,sei 
 insert into @t select,sen 
 insert into @t select,seng 
 insert into @t select,seo 
 insert into @t select,seon 
 insert into @t select,sha 
 insert into @t select,shai 
 insert into @t select,shan 
 insert into @t select,shang 
 insert into @t select,shao 
 insert into @t select,she 
 insert into @t select,shen 
 insert into @t select,sheng 
 insert into @t select,shi 
 insert into @t select,shike 
 insert into @t select,shiwa 
 insert into @t select,shou 
 insert into @t select,shu 
 insert into @t select,shua 
 insert into @t select,shuai 
 insert into @t select,shuan 
 insert into @t select,shuang 
 insert into @t select,shei 
 insert into @t select,shui 
 insert into @t select,shun 
 insert into @t select,shuo 
 insert into @t select,si 
 insert into @t select,so 
 insert into @t select,sol 
 insert into @t select,song 
 insert into @t select,sou 
 insert into @t select,su 
 insert into @t select,suan 
 insert into @t select,sui 
 insert into @t select,sun 
 insert into @t select,suo 
 insert into @t select,ta 
 insert into @t select,tae 
 insert into @t select,tai 
 insert into @t select,tan 
 insert into @t select,tang 
 insert into @t select,tao 
 insert into @t select,tap 
 insert into @t select,te 
 insert into @t select,teng 
 insert into @t select,teo 
 insert into @t select,teul 
 insert into @t select,ti 
 insert into @t select,tian 
 insert into @t select,tiao 
 insert into @t select,tie 
 insert into @t select,ting 
 insert into @t select,tol 
 insert into @t select,tong 
 insert into @t select,tou 
 insert into @t select,tu 
 insert into @t select,tuan 
 insert into @t select,tui 
 insert into @t select,tun 
 insert into @t select,tuo 
 insert into @t select,wa 
 insert into @t select,wai 
 insert into @t select,wan 
 insert into @t select,wang 
 insert into @t select,wei 
 insert into @t select,wen 
 insert into @t select,weng 
 insert into @t select,wo 
 insert into @t select,wu 
 insert into @t select,xi 
 insert into @t select,ei 
 insert into @t select,xia 
 insert into @t select,xian 
 insert into @t select,xiang 
 insert into @t select,xiao 
 insert into @t select,xie 
 insert into @t select,xin 
 insert into @t select,xing 
 insert into @t select,xiong 
 insert into @t select,xiu 
 insert into @t select,xu 
 insert into @t select,xuan 
 insert into @t select,xue 
 insert into @t select,xun 
 insert into @t select,ya 
 insert into @t select,yan 
 insert into @t select,eom 
 insert into @t select,yang 
 insert into @t select,yao 
 insert into @t select,ye 
 insert into @t select,yen 
 insert into @t select,yi 
 insert into @t select,i 
 insert into @t select,yin 
 insert into @t select,ying 
 insert into @t select,yo 
 insert into @t select,yong 
 insert into @t select,you 
 insert into @t select,yu 
 insert into @t select,yuan 
 insert into @t select,yue 
 insert into @t select,yun 
 insert into @t select,za 
 insert into @t select,zai 
 insert into @t select,zan 
 insert into @t select,zang 
 insert into @t select,zao 
 insert into @t select,ze 
 insert into @t select,zei 
 insert into @t select,zen 
 insert into @t select,zeng 
 insert into @t select,zha 
 insert into @t select,gad 
 insert into @t select,zhai 
 insert into @t select,zhan 
 insert into @t select,zhang 
 insert into @t select,zhao 
 insert into @t select,zhe 
 insert into @t select,zhen 
 insert into @t select,zheng 
 insert into @t select,zhi 
 insert into @t select,zhong 
 insert into @t select,zhou 
 insert into @t select,zhu 
 insert into @t select,zhua 
 insert into @t select,zhuan 
 insert into @t select,zhuai 
 insert into @t select,zhuang 
 insert into @t select,zhui 
 insert into @t select,zhun 
 insert into @t select,zhuo 
 insert into @t select,zi 
 insert into @t select,zo 
 insert into @t select,zong 
 insert into @t select,zou 
 insert into @t select,zu 
 insert into @t select,zuan 
 insert into @t select,zui 
 insert into @t select,zun 
 insert into @t select,zuo 
 
 declare @strlen int 
 select @strlen=len(@str),@re=‘‘
 while @strlen>0
 begin     
      select top 1 @re=py+ +@re,@strlen=@strlen-1 
      from @t a where chr<=substring(@str,@strlen,1) 
      order by chr collate Chinese_PRC_CS_AS_KS_WS  desc 
      if @@rowcount=0
        select @re=substring(@str,@strlen,1)+@re,@strlen=@strlen-1
   end
 return(@re)
end
View Code

测试了一串字符这个案例确实能达到汉字转拼音要求,但当我看到此函数内declare table、insert、order by等等这些耗性能的关键词让我感到头疼

我们还是来看看它的执行开销

 

技术分享

估计子树大小大于5,这是多么惊人的一个数字。此执行计划后面跟着茫茫多的线条几乎全是表插入

技术分享

我用一万条数据测试该函数等了我1分多钟都没执行完毕,可见该解决方案性能差到极点。

 

接下来我们看看解决方案二

技术分享
解决方案二

create function [dbo].[fn_Getquanpin2](@words nvarchar(2000))   
returns varchar(8000)   
as 
begin  
    declare @word nchar(1)   
    declare @pinyin varchar(8000)   
    declare @i int 
    declare @words_len int 
    declare @unicode int 
    set @i = 1   
    set @words = ltrim(rtrim(@words))   
    set @words_len = len(@words)   
    while (@i <= @words_len) --循环取字符  
    begin  
    set @word = substring(@words, @i, 1)   
    set @unicode = unicode(@word)   
    set @pinyin = ISNULL(@pinyin +SPACE(1),‘‘)+    
    (case when unicode(@word) between 19968 and 19968+20901 then   
    (select top 1 py from (   
    select a as py,N as word   
    union all select ai,N 
    union all select an,N 
    union all select ang,N 
    union all select ao,N 
    union all select ba,N 
    union all select bai,N --韛兡瓸  
    union all select ban,N 
    union all select bang,N 
    union all select bao,N 
    union all select bei,N 
    union all select ben,N 
    union all select beng,N 
    union all select bi,N 
    union all select bian,N 
    union all select biao,N 
    union all select bie,N 
    union all select bin,N 
    union all select bing,N 
    union all select bo,N 
    union all select bu,N簿 
    union all select ca,N 
    union all select cai,N --縩乲  
    union all select can,N 
    union all select cang,N 
    union all select cao,N 
    union all select ce,N 
    union all select cen,N 
    union all select ceng,N --硛硳岾猠乽  
    union all select cha,N 
    union all select chai,N 
    union all select chan,N 
    union all select chang,N 
    union all select chao,N 
    union all select che,N 
    union all select chen,N 
    union all select cheng,N 
    union all select chi,N 
    union all select chong,N 
    union all select chou,N 
    union all select chu,N 
    union all select chuai,N 
    union all select chuan,N 
    union all select chuang,N 
    union all select chui,N 
    union all select chun,N 
    union all select chuo,N 
    union all select ci,N --賜嗭  
    union all select cong,N 
    union all select cou,N 
    union all select cu,N 
    union all select cuan,N 
    union all select cui,N 
    union all select cun,N籿 
    union all select cuo,N 
    union all select da,N 
    union all select dai,N 
    union all select dan,N 
    union all select dang,N 
    union all select dao,N 
    union all select de,N 
    union all select den,N 
    union all select deng,N 
    union all select di,N 
    union all select dia,N 
    union all select dian,N 
    union all select diao,N 
    union all select die,N --眰嚸  
    union all select ding,N 
    union all select diu,N 
    union all select dong,N 
    union all select dou,N 
    union all select du,N 
    union all select duan,N --籪叾  
    union all select dui,N 
    union all select dun,N 
    union all select duo,N 
    union all select e,N 
    union all select en,N 
    union all select eng,N 
    union all select er,N 
    union all select fa,N 
    union all select fan,N 
    union all select fang,N 
    union all select fei,N 
    union all select fen,N 
    union all select feng,N 
    union all select fo,N 
    union all select fou,N 
    union all select fu,N --鰒猤  
    union all select ga,N 
    union all select gai,N 
    union all select gan,N 
    union all select gang,N 
    union all select gao,N 
    union all select ge,N 
    union all select gei,N 
    union all select gen,N 
    union all select geng,N --亙堩啹喼嗰  
    union all select gong,N --熕贑兝兣  
    union all select gou,N 
    union all select gu,N 
    union all select gua,N詿 
    union all select guai,N 
    union all select guan,N 
    union all select guang,N 
    union all select gui,N 
    union all select gun,N 
    union all select guo,N 
    union all select ha,N 
    union all select hai,N 
    union all select han,N 
    union all select hang,N 
    union all select hao,N 
    union all select he,N 
    union all select hei,N 
    union all select hen,N 
    union all select heng,N --堼囍  
    union all select hong,N 
    union all select hou,N 
    union all select hu,N 
    union all select hua,N 
    union all select huai,N 
    union all select huan,N 
    union all select huang,N 
    union all select hui,N 
    union all select hun,N 
    union all select huo,N 
    union all select ji,N 
    union all select jia,N 
    union all select jian,N 
    union all select jiang,N 
    union all select jiao,N 
    union all select jie,N 
    union all select jin,N 
    union all select jing,N 
    union all select jiong,N 
    union all select jiu,N 
    union all select ju,N 
    union all select juan,N 
    union all select jue,N 
    union all select jun,N 
    union all select ka,N 
    union all select kai,N --鎎乫  
    union all select kan,N 
    union all select kang,N 
    union all select kao,N 
    union all select ke,N 
    union all select ken,N 
    union all select keng,N --巪乬唟厼怾  
    union all select kong,N 
    union all select kou,N 
    union all select ku,N 
    union all select kua,N 
    union all select kuai,N 
    union all select kuan,N 
    union all select kuang,N 
    union all select kui,N 
    union all select kun,N 
    union all select kuo,N 
    union all select la,N 
    union all select lai,N 
    union all select lan,N 
    union all select lang,N 
    union all select lao,N 
    union all select le,N 
    union all select lei,N --嘞脷  
    union all select leng,N 
    union all select li,N 
    union all select lia,N 
    union all select lian,N 
    union all select liang,N 
    union all select liao,N 
    union all select lie,N 
    union all select lin,N --轥拎  
    union all select ling,N 
    union all select liu,N --瓼甅囖咯  
    union all select long,N 
    union all select lou,N 
    union all select lu,N 
    union all select lv,N 
    union all select luan,N 
    union all select lue,N 
    union all select lun,N 
    union all select luo,N 
    union all select ma,N 
    union all select mai,N 
    union all select man,N 
    union all select mang,N 
    union all select mao,N 
    union all select me,N --癦呅  
    union all select mei,N 
    union all select men,N 
    union all select meng,N --霿踎  
    union all select mi,N 
    union all select mian,N 
    union all select miao,N 
    union all select mie,N --鱴瓱  
    union all select min,N 
    union all select ming,N 
    union all select miu,N 
    union all select mo,N --耱乮  
    union all select mou,N --麰蟱  
    union all select mu,N 
    union all select na,N 
    union all select nai,N 
    union all select nan,N 
    union all select nang,N 
    union all select nao,N 
    union all select ne,N 
    union all select nei,N --嫩焾  
    union all select nen,N 
    union all select neng,N --莻嗯鈪銰啱  
    union all select ni,N 
    union all select nian,N 
    union all select niang,N 
    union all select niao,N 
    union all select nie,N 
    union all select nin,N 
    union all select ning,N 
    union all select niu,N 
    union all select nong,N 
    union all select nou,N 
    union all select nu,N 
    union all select nv,N 
    union all select nue,N 
    union all select nuan,N --硸黁燶郍  
    union all select nuo,N 
    union all select o,N --毮夞乯鞰  
    union all select ou,N 
    union all select pa,N 
    union all select pai,N --鎃磗  
    union all select pan,N 
    union all select pang,N 
    union all select pao,N 
    union all select pei,N 
    union all select pen,N 
    union all select peng,N --浌巼闏乶喸  
    union all select pi,N 
    union all select pian,N 
    union all select piao,N 
    union all select pie,N 
    union all select pin,N 
    union all select ping,N 
    union all select po,N 
    union all select pou,N --兺哛  
    union all select pu,N 
    union all select qi,N 
    union all select qia,N 
    union all select qian,N 
    union all select qiang,N --羻兛瓩  
    union all select qiao,N 
    union all select qie,N 
    union all select qin,N 
    union all select qing,N 
    union all select qiong,N 
    union all select qiu,N 
    union all select qu,N 
    union all select quan,N 
    union all select que,N 
    union all select qun,N 
    union all select ran,N 
    union all select rang,N 
    union all select rao,N 
    union all select re,N 
    union all select ren,N 
    union all select reng,N 
    union all select ri,N 
    union all select rong,N 
    union all select rou,N嶿 
    union all select ru,N 
    union all select ruan,N 
    union all select rui,N 
    union all select run,N --橍挼  
    union all select ruo,N 
    union all select sa,N --櫒栍  
    union all select sai,N --簺虄  
    union all select san,N 
    union all select sang,N 
    union all select sao,N 
    union all select se,N --裇聓  
    union all select sen,N 
    union all select seng,N --閪縇  
    union all select sha,N 
    union all select shai,N 
    union all select shan,N 
    union all select shang,N 
    union all select shao,N 
    union all select she,N 
    union all select shen,N 
    union all select sheng,N 
    union all select shi,N --鰘齛兙瓧  
    union all select shou,N 
    union all select shu,N 
    union all select shua,N 
    union all select shuai,N 
    union all select shuan,N 
    union all select shuang,N 
    union all select shui,N 
    union all select shun,N 
    union all select shuo,N 
    union all select si,N --瀃螦乺  
    union all select song,N 
    union all select sou,N 
    union all select su,N 
    union all select suan,N 
    union all select sui,N 
    union all select sun,N 
    union all select suo,N 
    union all select ta,N --躢襨  
    union all select tai,N 
    union all select tan,N 
    union all select tang,N 
    union all select tao,N --討畓  
    union all select te,N 
    union all select teng,N --霯唞朰  
    union all select ti,N 
    union all select tian,N 
    union all select tiao,N 
    union all select tie,N 
    union all select ting,N --濎乭  
    union all select tong,N 
    union all select tou,N 
    union all select tu,N 
    union all select tuan,N 
    union all select tui,N 
    union all select tun,N 
    union all select tuo,N 
    union all select wa,N 
    union all select wai,N 
    union all select wan,N 
    union all select wang,N 
    union all select wei,N 
    union all select wen,N 
    union all select weng,N 
    union all select wo,N 
    union all select wu,N 
    union all select xi,N 
    union all select xia,N 
    union all select xian,N 
    union all select xiang,N 
    union all select xiao,N 
    union all select xie,N 
    union all select xin,N 
    union all select xing,N 
    union all select xiong,N 
    union all select xiu,N 
    union all select xu,N 
    union all select xuan,N 
    union all select xue,N 
    union all select xun,N 
    union all select ya,N 
    union all select yan,N 
    union all select yang,N 
    union all select yao,N 
    union all select ye,N --鸈膶岃  
    union all select yi,N 
    union all select yin,N 
    union all select ying,N 
    union all select yo,N 
    union all select yong,N 
    union all select you,N 
    union all select yu,N 
    union all select yuan,N 
    union all select yue,N 
    union all select yun,N 
    union all select za,N 
    union all select zai,N 
    union all select zan,N 
    union all select zang,N 
    union all select zao,N 
    union all select ze,N 
    union all select zei,N 
    union all select zen,N 
    union all select zeng,N 
    union all select zha,N 
    union all select zhai,N 
    union all select zhan,N 
    union all select zhang,N 
    union all select zhao,N 
    union all select zhe,N 
    union all select zhen,N 
    union all select zheng,N 
    union all select zhi,N 
    union all select zhong,N 
    union all select zhou,N 
    union all select zhu,N 
    union all select zhua,N 
    union all select zhuai,N 
    union all select zhuan,N 
    union all select zhuang,N 
    union all select zhui,N 
    union all select zhun,N 
    union all select zhuo,N 
    union all select zi,N --漬唨  
    union all select zong,N 
    union all select zou,N 
    union all select zu,N 
    union all select zuan,N 
    union all select zui,N 
    union all select zun,N 
    union all select zuo,N) t    
    where word >= @word collate Chinese_PRC_CS_AS_KS_WS    
    order by word collate Chinese_PRC_CS_AS_KS_WS ASC) else @word end)   
    set @i = @i + 1   
    end  
    return @pinyin   
END
View Code

测试了一串字符这个案例同样能达到汉字转拼音要求,该解决方案还好没让我看到表插入,这回性能应该大有提高

我们来看那看它的执行开销

技术分享

果然不出所料,估计子树大小在0.017左右, 其中排序开销最大

技术分享

我用一万条数据测试该函数花了10秒左右

既然排序开销最大那就优化该解决方案去掉order by。

技术分享

这时估计子树大小就在0.00001之下了,可你会发现此解决方案再怎么优化还是免不了需要常量扫描,where筛选等运算.

技术分享

我用一万条数据测试该优化后函数花了4~5秒左右

 

出于对性能要求的不满足,打开工具我自己写了一个汉字转拼音的函数

由于时间仓促未加任何注释,有问题可及时提出

最后给出的就是重写的解决方案

技术分享
解决方案三

create function [dbo].[fn_Getquanpin3](@str varchar(100))
returns varchar(8000)
as
begin
 declare @re varchar(8000),@crs varchar(10)
 declare @strlen int 
 select @strlen=len(@str),@re=‘‘
 while @strlen>0
 begin  
  set @crs= substring(@str,@strlen,1)
      select @re=
        case
        when @crs< then @crs
        when @crs<= then a
        when @crs<= then ai
        when @crs<= then an
        when @crs<= then ang
        when @crs<= then ao
        when @crs<= then ba
        when @crs<= then bai
        when @crs<= then ban
        when @crs<= then bang
        when @crs<= then bao
        when @crs<= then bei
        when @crs<= then ben
        when @crs<= then beng
        when @crs<= then bi
        when @crs<= then bian
        when @crs<= then biao
        when @crs<= then bie
        when @crs<= then bin
        when @crs<= then bing
        when @crs<= then bo
        when @crs<=簿 then bu
        when @crs<= then ca
        when @crs<= then cai
        when @crs<= then can
        when @crs<= then cang
        when @crs<= then cao
        when @crs<= then ce
        when @crs<= then cen
        when @crs<= then ceng
        when @crs<= then cha
        when @crs<= then chai
        when @crs<= then chan
        when @crs<= then chang
        when @crs<= then chao
        when @crs<= then che
        when @crs<= then chen
        when @crs<= then cheng
        when @crs<= then chi
        when @crs<= then chong
        when @crs<= then chou
        when @crs<= then chu
        when @crs<= then chuai
        when @crs<= then chuan
        when @crs<= then chuang
        when @crs<= then chui
        when @crs<= then chun
        when @crs<= then chuo
        when @crs<= then ci
        when @crs<= then cong
        when @crs<= then cou
        when @crs<= then cu
        when @crs<= then cuan
        when @crs<= then cui
        when @crs<=籿 then cun
        when @crs<= then cuo
        when @crs<= then da
        when @crs<= then dai
        when @crs<= then dan
        when @crs<= then dang
        when @crs<= then dao
        when @crs<= then de
        when @crs<= then den
        when @crs<= then deng
        when @crs<= then di
        when @crs<= then dia
        when @crs<= then dian
        when @crs<= then diao
        when @crs<= then die
        when @crs<= then ding
        when @crs<= then diu
        when @crs<= then dong
        when @crs<= then dou
        when @crs<= then du
        when @crs<= then duan
        when @crs<= then dui
        when @crs<= then dun
        when @crs<= then duo
        when @crs<= then e
        when @crs<= then en
        when @crs<= then eng
        when @crs<= then er
        when @crs<= then fa
        when @crs<= then fan
        when @crs<= then fang
        when @crs<= then fei
        when @crs<= then fen
        when @crs<= then feng
        when @crs<= then fo
        when @crs<= then fou
        when @crs<= then fu
        when @crs<= then ga
        when @crs<= then gai
        when @crs<= then gan
        when @crs<= then gang
        when @crs<= then gao
        when @crs<= then ge
        when @crs<= then gei
        when @crs<= then gen
        when @crs<= then geng
        when @crs<= then gong
        when @crs<= then gou
        when @crs<= then gu
        when @crs<=詿 then gua
        when @crs<= then guai
        when @crs<= then guan
        when @crs<= then guang
        when @crs<= then gui
        when @crs<= then gun
        when @crs<= then guo
        when @crs<= then ha
        when @crs<= then hai
        when @crs<= then han
        when @crs<= then hang
        when @crs<= then hao
        when @crs<= then he
        when @crs<= then hei
        when @crs<= then hen
        when @crs<= then heng
        when @crs<= then hong
        when @crs<= then hou
        when @crs<= then hu
        when @crs<= then hua
        when @crs<= then huai
        when @crs<= then huan
        when @crs<= then huang
        when @crs<= then hui
        when @crs<= then hun
        when @crs<= then huo
        when @crs<= then ji
        when @crs<= then jia
        when @crs<= then jian
        when @crs<= then jiang
        when @crs<= then jiao
        when @crs<= then jie
        when @crs<= then jin
        when @crs<= then jing
        when @crs<= then jiong
        when @crs<= then jiu
        when @crs<= then ju
        when @crs<= then juan
        when @crs<= then jue
        when @crs<= then jun
        when @crs<= then ka
        when @crs<= then kai
        when @crs<= then kan
        when @crs<= then kang
        when @crs<= then kao
        when @crs<= then ke
        when @crs<= then ken
        when @crs<= then keng
        when @crs<= then kong
        when @crs<= then kou
        when @crs<= then ku
        when @crs<= then kua
        when @crs<= then kuai
        when @crs<= then kuan
        when @crs<= then kuang
        when @crs<= then kui
        when @crs<= then kun
        when @crs<= then kuo
        when @crs<= then la
        when @crs<= then lai
        when @crs<= then lan
        when @crs<= then lang
        when @crs<= then lao
        when @crs<= then le
        when @crs<= then lei
        when @crs<= then leng
        when @crs<= then li
        when @crs<= then lia
        when @crs<= then lian
        when @crs<= then liang
        when @crs<= then liao
        when @crs<= then lie
        when @crs<= then lin
        when @crs<= then ling
        when @crs<= then liu
        when @crs<= then long
        when @crs<= then lou
        when @crs<= then lu
        when @crs<= then lv
        when @crs<= then luan
        when @crs<= then lue
        when @crs<= then lun
        when @crs<= then luo
        when @crs<= then ma
        when @crs<= then mai
        when @crs<= then man
        when @crs<= then mang
        when @crs<= then mao
        when @crs<= then me
        when @crs<= then mei
        when @crs<= then men
        when @crs<= then meng
        when @crs<= then mi
        when @crs<= then mian
        when @crs<= then miao
        when @crs<= then mie
        when @crs<= then min
        when @crs<= then ming
        when @crs<= then miu
        when @crs<= then mo
        when @crs<= then mou
        when @crs<= then mu
        when @crs<= then na
        when @crs<= then nai
        when @crs<= then nan
        when @crs<= then nang
        when @crs<= then nao
        when @crs<= then ne
        when @crs<= then nei
        when @crs<= then nen
        when @crs<= then neng
        when @crs<= then ni
        when @crs<= then nian
        when @crs<= then niang
        when @crs<= then niao
        when @crs<= then nie
        when @crs<= then nin
        when @crs<= then ning
        when @crs<= then niu
        when @crs<= then nong
        when @crs<= then nou
        when @crs<= then nu
        when @crs<= then nv
        when @crs<= then nue
        when @crs<= then nuan
        when @crs<= then nuo
        when @crs<= then o
        when @crs<= then ou
        when @crs<= then pa
        when @crs<= then pai
        when @crs<= then pan
        when @crs<= then pang
        when @crs<= then pao
        when @crs<= then pei
        when @crs<= then pen
        when @crs<= then peng
        when @crs<= then pi
        when @crs<= then pian
        when @crs<= then piao
        when @crs<= then pie
        when @crs<= then pin
        when @crs<= then ping
        when @crs<= then po
        when @crs<= then pou
        when @crs<= then pu
        when @crs<= then qi
        when @crs<= then qia
        when @crs<= then qian
        when @crs<= then qiang
        when @crs<= then qiao
        when @crs<= then qie
        when @crs<= then qin
        when @crs<= then qing
        when @crs<= then qiong
        when @crs<= then qiu
        when @crs<= then qu
        when @crs<= then quan
        when @crs<= then que
        when @crs<= then qun
        when @crs<= then ran
        when @crs<= then rang
        when @crs<= then rao
        when @crs<= then re
        when @crs<= then ren
        when @crs<= then reng
        when @crs<= then ri
        when @crs<= then rong
        when @crs<=嶿 then rou
        when @crs<= then ru
        when @crs<= then ruan
        when @crs<= then rui
        when @crs<= then run
        when @crs<= then ruo
        when @crs<= then sa
        when @crs<= then sai
        when @crs<= then san
        when @crs<= then sang
        when @crs<= then sao
        when @crs<= then se
        when @crs<= then sen
        when @crs<= then seng
        when @crs<= then sha
        when @crs<= then shai
        when @crs<= then shan
        when @crs<= then shang
        when @crs<= then shao
        when @crs<= then she
        when @crs<= then shen
        when @crs<= then sheng
        when @crs<= then shi
        when @crs<= then shou
        when @crs<= then shu
        when @crs<= then shua
        when @crs<= then shuai
        when @crs<= then shuan
        when @crs<= then shuang
        when @crs<= then shui
        when @crs<= then shun
        when @crs<= then shuo
        when @crs<= then si
        when @crs<= then song
        when @crs<= then sou
        when @crs<= then su
        when @crs<= then suan
        when @crs<= then sui
        when @crs<= then sun
        when @crs<= then suo
        when @crs<= then ta
        when @crs<= then tai
        when @crs<= then tan
        when @crs<= then tang
        when @crs<= then tao
        when @crs<= then te
        when @crs<= then teng
        when @crs<= then ti
        when @crs<= then tian
        when @crs<= then tiao
        when @crs<= then tie
        when @crs<= then ting
        when @crs<= then tong
        when @crs<= then tou
        when @crs<= then tu
        when @crs<= then tuan
        when @crs<= then tui
        when @crs<= then tun
        when @crs<= then tuo
        when @crs<= then wa
        when @crs<= then wai
        when @crs<= then wan
        when @crs<= then wang
        when @crs<= then wei
        when @crs<= then wen
        when @crs<= then weng
        when @crs<= then wo
        when @crs<= then wu
        when @crs<= then xi
        when @crs<= then xia
        when @crs<= then xian
        when @crs<= then xiang
        when @crs<= then xiao
        when @crs<= then xie
        when @crs<= then xin
        when @crs<= then xing
        when @crs<= then xiong
        when @crs<= then xiu
        when @crs<= then xu
        when @crs<= then xuan
        when @crs<= then xue
        when @crs<= then xun
        when @crs<= then ya
        when @crs<= then yan
        when @crs<= then yang
        when @crs<= then yao
        when @crs<= then ye
        when @crs<= then yi
        when @crs<= then yin
        when @crs<= then ying
        when @crs<= then yo
        when @crs<= then yong
        when @crs<= then you
        when @crs<= then yu
        when @crs<= then yuan
        when @crs<= then yue
        when @crs<= then yun
        when @crs<= then za
        when @crs<= then zai
        when @crs<= then zan
        when @crs<= then zang
        when @crs<= then zao
        when @crs<= then ze
        when @crs<= then zei
        when @crs<= then zen
        when @crs<= then zeng
        when @crs<= then zha
        when @crs<= then zhai
        when @crs<= then zhan
        when @crs<= then zhang
        when @crs<= then zhao
        when @crs<= then zhe
        when @crs<= then zhen
        when @crs<= then zheng
        when @crs<= then zhi
        when @crs<= then zhong
        when @crs<= then zhou
        when @crs<= then zhu
        when @crs<= then zhua
        when @crs<= then zhuai
        when @crs<= then zhuan
        when @crs<= then zhuang
        when @crs<= then zhui
        when @crs<= then zhun
        when @crs<= then zhuo
        when @crs<= then zi
        when @crs<= then zong
        when @crs<= then zou
        when @crs<= then zu
        when @crs<= then zuan
        when @crs<= then zui
        when @crs<= then zun
        when @crs<= then zuo
        else  @crs end+ +@re,@strlen=@strlen-1 
   end
 return(@re)
end
View Code

我们来看它的执行计划

技术分享

没有排序,没有常量扫描,没有筛选器,没有计算标量

估计子树大小为0

我用一万行测试数据花0~2秒就把所有汉字转换为拼音

汉字转全拼音函数优化方案(SQLServer),值得你看看

标签:

原文地址:http://www.cnblogs.com/jlove/p/4456543.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!