日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

SQLServer优化二

發(fā)布時間:2025/3/15 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SQLServer优化二 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

建立合理的索引,避免掃描多余數(shù)據(jù),避免表掃描!幾百萬條數(shù)據(jù),照樣幾十毫秒完成查詢。關(guān)于SQL查詢效率,100w數(shù)據(jù),查詢只要1秒,與您分享!

查詢效率分析: 子查詢?yōu)榇_保消除重復(fù)值,必須為外部查詢的每個結(jié)果都處理嵌套查詢。在這種情況下可以考慮用聯(lián)接查詢來取代。 如果要用子查詢,那就用EXISTS替代IN、用NOT EXISTS替代NOT IN。因為EXISTS引入的子查詢只是測試是否存在符合子查詢中指定條件的行,效率較高。無論在哪種情況下,NOT IN都是最低效的。因為它對子查詢中的表執(zhí)行了一個全表遍歷。

建立合理的索引,避免掃描多余數(shù)據(jù),避免表掃描! 幾百萬條數(shù)據(jù),照樣幾十毫秒完成查詢.

機(jī)器情況 p4: 2.4 內(nèi)存: 1 G os: windows 2003 數(shù)據(jù)庫: ms sql server 2000 目的: 查詢性能測試,比較兩種查詢的性能

SQL查詢效率 step by step

-- setp 1. -- 建表

create table t_userinfo ( userid int identity(1,1) primary key nonclustered, nick varchar(50) not null default '', classid int not null default 0, writetime datetime not null default getdate() ) go

-- 建索引

create clustered index ix_userinfo_classid on t_userinfo(classid) go

-- step 2.

declare @i int declare @k int declare @nick varchar(10) set @i = 1 while @i<1000000 begin set @k = @i % 10 set @nick = convert(varchar,@i) insert into t_userinfo(nick,classid,writetime) values(@nick,@k,getdate()) set @i = @i + 1 end

-- 耗時 08:27 ,需要耐心等待

-- step 3.

select top 20 userid,nick,classid,writetime from t_userinfo where userid not in ( select top 900000 userid from t_userinfo order by userid asc )

-- 耗時 8 秒 ,夠長的

-- step 4.

select a.userid,b.nick,b.classid,b.writetime from ( select top 20 a.userid from ( select top 900020 userid from t_userinfo order by userid asc ) a order by a.userid desc ) a inner join t_userinfo b on a.userid = b.userid order by a.userid asc

-- 耗時 1 秒,太快了吧,不可以思議

-- step 5 where 查詢

select top 20 userid,nick,classid,writetime from t_userinfo where classid = 1 and userid not in ( select top 90000 userid from t_userinfo where classid = 1 order by userid asc )

-- 耗時 2 秒

-- step 6 where 查詢

select a.userid,b.nick,b.classid,b.writetime from ( select top 20 a.userid from ( select top 90000 userid from t_userinfo where classid = 1 order by userid asc ) a order by a.userid desc ) a inner join t_userinfo b on a.userid = b.userid order by a.userid asc

-- 查詢分析器顯示不到 1 秒.

轉(zhuǎn)載于:https://www.cnblogs.com/hx214blog/archive/2012/05/23/2514761.html

總結(jié)

以上是生活随笔為你收集整理的SQLServer优化二的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。