自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

SQL查詢效率:100w數(shù)據(jù)查詢只需要1秒鐘

數(shù)據(jù)庫
當代是一個高速發(fā)展的社會,什么事都講求效率,SQL查詢更是追求高效的效率,那么如何實現(xiàn)SQL查詢的高效呢?下文中就為大家介紹一種提高效率的方法。

關(guān)于SQL查詢效率,100w數(shù)據(jù),查詢只要1秒,您相信嗎?大家都知道,現(xiàn)在的數(shù)據(jù)庫儲存容量是越來越大,數(shù)據(jù)庫的查詢效率可能在一定程度上有所降低,現(xiàn)在的技術(shù)是飛速發(fā)展進步,實現(xiàn)100w數(shù)據(jù)查詢只需要1秒鐘已經(jīng)是可能的啦,下文中就為大家介紹這種方法,希望對大家有幫助。

機器情況:

p4: 2.4

內(nèi)存: 1 G

os: windows 2003

數(shù)據(jù)庫:

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 秒.

查詢效率分析:

子查詢?yōu)榇_保消除重復(fù)值,必須為外部查詢的每個結(jié)果都處理嵌套查詢。在這種情況下可以考慮用聯(lián)接查詢來取代。

如果要用子查詢,那就用EXISTS替代IN、用NOT EXISTS替代NOT IN。因為EXISTS引入的子查詢只是測試是否存在符合子查詢中指定條件的行,效率較高。無論在哪種情況下,NOT IN都是***效的。因為它對子查詢中的表執(zhí)行了一個全表遍歷。

建立合理的索引,避免掃描多余數(shù)據(jù),避免表掃描!

幾百萬條數(shù)據(jù),照樣幾十毫秒完成查詢。

【編輯推薦】

  1. 提高MySQL數(shù)據(jù)庫查詢效率的技巧(三)
  2. 如何在MySQL查詢結(jié)果集中得到記錄行號
  3. 實例講解如何配置MySQL數(shù)據(jù)庫主從復(fù)制
責(zé)任編輯:迎迎 來源: 賽迪網(wǎng)
相關(guān)推薦

2009-04-09 16:52:47

LinuxUbuntu 9.04

2021-06-23 06:48:42

秒殺Java電商

2019-09-16 09:34:39

2018-03-07 10:03:40

2010-03-16 16:47:25

Ubuntu 9.04

2023-10-11 12:45:49

Windows系統(tǒng)

2021-02-03 11:20:41

Docker架構(gòu)容器

2010-09-25 09:12:44

SQL Server

2019-08-23 09:03:04

盤口數(shù)據(jù)數(shù)據(jù)庫緩存

2019-11-27 09:48:04

數(shù)據(jù)ESHBase

2010-09-25 16:47:51

SQL查詢

2024-04-26 00:03:00

計算機CPU關(guān)系

2013-09-08 22:40:38

EF Code Fir數(shù)據(jù)查詢架構(gòu)設(shè)計

2020-04-20 15:10:19

Redis內(nèi)存數(shù)據(jù)庫

2017-01-16 09:14:35

2009-07-06 15:50:01

微軟Windows 7操作系統(tǒng)

2020-11-26 15:51:11

SQL數(shù)據(jù)庫大數(shù)據(jù)

2023-11-28 07:48:23

SQL Server數(shù)據(jù)庫

2024-12-20 16:41:22

2021-04-09 23:00:12

SQL數(shù)據(jù)庫Pandas
點贊
收藏

51CTO技術(shù)棧公眾號