sqlserver 中charindex/patindex/like 的比較,大家可以看下根據(jù)需要自行選擇。
一、測(cè)試環(huán)境:
1。數(shù)據(jù)庫(kù):Sql Server 2008
2。測(cè)試表:15000記錄,char類型主鍵,無其他索引
3。測(cè)試字段:ntext 類型,最大數(shù)據(jù)長(zhǎng)度12000
二、測(cè)試語句:
1。select * from ProductTemp where ProductDesc like ‘%192.168.70.236%’
2。select * from ProductTemp where charindex(‘192.168.70.236’,ProductDesc)>0
3。select * from ProductTemp where patindex(‘%192.168.70.236%’,ProductDesc)>0
三、測(cè)試結(jié)果:
1。效率測(cè)試結(jié)果: charindex > like > patindex ,效率差大概20%
2。適用性測(cè)試結(jié)果:
1)charindex對(duì)于檢索結(jié)果最大位移有要求,經(jīng)測(cè)試,Sql2008中的ntext字段,位移不能超過3987,否則返回0;
2)patindex 和 like 無位移限制;
3)patindex 支持匹配表達(dá)式,可以應(yīng)用正則;
4)like可以用’%oldstring%’進(jìn)行模糊匹配;
5)charindex只能匹配固定字符串