Tempdb的作用
sybase server端內(nèi)部使用
排序
創(chuàng)建worktables
reformatting
存儲臨時表和index
Tempdb用途分類
Turely temp tables
Regular user tables
worktables
(1)Truly temporary tables
這種表是正在的臨時表, 通過create table #tablename或select into #tablename創(chuàng)建,只在當(dāng)前session中有效,session結(jié)束時,這些表或index自動被drop
create table #temptable (...)
or:
select select_list
into #temptable ...
(2)Regular user tables
通過tempdb..tablename 可以創(chuàng)建RUT,
create table tempdb..temptable
or:
select select_list
into tempdb..temptable
在什么情況下使用RUT表呢?
要在不同session間共享數(shù)據(jù),通過grant permission共享
用于bulk copy
用完以后用戶必須顯示的drop掉,否則會一直存在,知道sybase server重啟
(3)Worktables
在sybase處理join, sort或其他內(nèi)部操作是創(chuàng)建,這種表不能共享,命令處理完后馬上自動被drop
tempdb的大小
默認(rèn)2MB,存儲在master device上。
我們可以用sp_helpdb系統(tǒng)存儲過程查看tempdb的大小
isql>sp_helpdb tempdb
name db_size owner dbid created status
--------- -------- ------ ------ ----------- --------------------
tempdb 2.0 MB sa 2 May 22, 1999 select into/bulkcopy
device_frag size usage free kbytes
------------ -------- ------------ ---------
master 2.0 MB data and log 1248
用戶量越大,并發(fā)越多,將tempdb大小設(shè)置越大越好。通常情況下,設(shè)置tempdb的大小為你的數(shù)據(jù)庫大小的20%~25%
tempdb和lock
在tempdb中create或drop表,索引會引起system table上鎖爭用,這些system table包括sysobjects, syscolumns和sysindexes。
如果在tempdb重復(fù)create或drop表和索引,那么最好的辦法是在程序啟動時將臨時表建好,結(jié)束時truncate table來刪除數(shù)據(jù)以減少system table上鎖的爭用。
tempdb性能優(yōu)化
(1) 減少log,多用select into,少用create table #xxx and insert,select into產(chǎn)生最少的log
(2) 避免在tempdb中創(chuàng)建大的表,如果創(chuàng)建了大的表,記得建index
(3) 用嵌套SQL,嵌套調(diào)用Stored proc 代替大的表