久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

SQL數(shù)據(jù)庫中濫用臨時表和排序的解決優(yōu)化是怎樣的

159次閱讀
沒有評論

共計 3696 個字符,預計需要花費 10 分鐘才能閱讀完成。

這篇文章給大家介紹 SQL 數(shù)據(jù)庫中濫用臨時表和排序的解決優(yōu)化是怎樣的,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

游標、臨時表、觸發(fā)器、COLLATE 等等
無可厚非、這些都是好東西,我為什么今天要花時間來寫這些東西呢?
是因為我發(fā)現(xiàn)慢慢的很多人用久了這些東西之后會形成一種習慣,不管解決什么問題動不動都會把它們搬出來,由此我看到了很多漂亮的代碼在性能效率面前卻顯得不那么優(yōu)秀。

好了廢話不多說開始進入正題吧。

今天的案例

場景:

需要通過用戶輸入的姓名關鍵字來搜索用戶。用戶輸入關鍵字 x 來搜索用戶(數(shù)據(jù)來源于表 [Name 字段中] 或內存 [List UserInfo] 中)

要求:

得到的結果排序應為:

x

xia

xiao

yx

即:

包含 x 字母的結果均應顯示出來

首字母匹配的結果應該排在前面(如 x 開頭)

在條件 2 相同的前提下更短的結果應排在前面(如 x 排在 xia 前面)

各位大俠能否給出一套 C# 與 SQL Server(2008)的解決方案?

補充:

如果能一起解決中文問題最好,如搜索 x

得到的結果排序應為:

x

xiani

夏榮

肖小笑

楊星

即將漢字的拼音首字母納入在內,不知 SQL Server 是否支持這一特性的搜索?

感謝 [學習的腳步] 這位網(wǎng)友提出來的問題

其實要解決這個問題不難,無非就是漢字轉拼音首字母

———————————————————————————————

先給出解決方案一

——————— 準備工作 開始 ——————————-
if object_id(zhuisuos)is not null
drop table zhuisuos
go

create table zhuisuos
(
name varchar(100)
)
insert into zhuisuos values(追索)
insert into zhuisuos values(追索 2)
insert into zhuisuos values(xia)
insert into zhuisuos values(dxc)
insert into zhuisuos values(x)
insert into zhuisuos values(xx)
insert into zhuisuos values(xiani)
insert into zhuisuos values(yx)
insert into zhuisuos values(夏榮)
insert into zhuisuos values(肖小笑)
insert into zhuisuos values(楊星)
go
——————————————————————————-
– 建立漢字轉拼音首字母函數(shù)
if object_id(fn_getpy1)is not null
drop function fn_getpy1
go

GO
create function [dbo].fn_getpy1
(@str nvarchar(4000)) 
returns nvarchar(4000) 
as 
begin 
declare @str_len int,@result nvarchar(4000) 
declare @zhuisuo table
(firstspell nchar(1)  collate  Chinese_PRC_CI_AS,
letter nchar(1)) 
set @str_len=len(@str)
set @result=     
insert into @zhuisuo
(firstspell,letter) 
 select  吖   ,  A   union all select  八   ,  B   union all 
 select  嚓   ,  C   union all select  咑   ,  D   union all 
 select  妸   ,  E   union all select  發(fā)   ,  F   union all 
 select  旮   ,  G   union all select  鉿   ,  H   union all 
 select  丌   ,  J   union all select  咔   ,  K   union all 
 select  垃   ,  L   union all select  嘸   ,  M   union all 
 select  拏   ,  N   union all select  噢   ,  O   union all 
 select  妑   ,  P   union all select  七   ,  Q   union all 
 select  呥   ,  R   union all select  仨   ,  S   union all 
 select  他   ,  T   union all select  屲   ,  W   union all 
 select  夕   ,  X   union all select  丫   ,  Y   union all 
 select  帀   ,  Z   
 while @str_len  0 
 begin 
 select top 1 @result=letter+@result,@str_len=@str_len-1 
 from @zhuisuo 
 where  firstspell  =substring(@str,@str_len,1) 
 order by  firstspell desc 
 if @@rowcount=0 
 select @result=substring(@str,@str_len,1)+@result,@str_len=@str_len-1 
 end 
 return(@result) 
end
——————— 準備工作 結束 ——————————-

– 正式查詢
declare @str varchar(10)
set @str= x
create table #result
(name varchar(100) null,id int null,lens int null)

insert into #result 
select name,1,len(name) from zhuisuos
where name like @str+ %

insert into #result
select name,2,len(name) from zhuisuos
where name like  % +@str+ %  and name not like @str+ %

insert into #result
select name,3,len(name) from zhuisuos
where dbo.fn_getpy1 (name) like @str+ %  and name not like @str+ %  and name not like  % +@str+ %

insert into #result
select name,4,len(name) from zhuisuos
where dbo.fn_getpy1 (name) like  % +@str+ %  and dbo.fn_getpy1 (name) not like @str+ %
 and  name not like @str+ %  and name not like  % +@str+ %

select name from #result
order by id,lens
drop table #result

這個解決方案已經(jīng)滿足查詢要求

其它都不管 我們重點來看看這次寫的這個函數(shù)

象這樣的漢字轉拼音函數(shù)在網(wǎng)上一搜一大把 今天我就要舉例幾個方案讓大家對優(yōu)化及開銷有個清楚的概念

解決方案一寫的函數(shù)實在是太糟糕了(以上及接下來舉出的案例并無冒犯任何雷同及原創(chuàng)代碼之意,還請多多包涵)

為什么這么說呢

這是它的執(zhí)行計劃

它用了臨時表并且排序

表插入開銷 0.01  表掃描開銷 0.003 表排序 0.011

估計總開銷 0.0246

實際執(zhí)行:我拿 1 萬行數(shù)據(jù)調用此函數(shù)花了我 20 幾秒、一個查詢操作你愿意等 20 多秒嗎

所以看到這樣的執(zhí)行計劃實在很抱歉

解決方案二

create function [dbo].[fn_getpy2](@Str varchar(500)= )
returns varchar(500)
as
begin
declare @strlen int,@return varchar(500),@ii int
declare @n int,@c char(1),@chn nchar(1)

select @strlen=len(@str),

關于 SQL 數(shù)據(jù)庫中濫用臨時表和排序的解決優(yōu)化是怎樣的就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

正文完
 
丸趣
版權聲明:本站原創(chuàng)文章,由 丸趣 2023-07-19發(fā)表,共計3696字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網(wǎng)絡搜集發(fā)布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 寿光市| 福海县| 肇庆市| 白山市| 米林县| 阿瓦提县| 资中县| 南充市| 师宗县| 蒙阴县| 弥渡县| 朝阳市| 皋兰县| 牟定县| 太谷县| 房产| 江城| 重庆市| 信丰县| 清苑县| 黔西县| 肇东市| 拜城县| 扶绥县| 乌兰察布市| 万荣县| 太白县| 介休市| 密云县| 和静县| 丰原市| 织金县| 聊城市| 灵丘县| 巴里| 乐陵市| 兴海县| 西平县| 梁河县| 泾川县| 威信县|