SQL server 2005的表分區(qū)
下面來說下,在SQL SERVER 2005的表分區(qū)里,如何對(duì)已經(jīng)存在的有數(shù)據(jù)的表進(jìn)行分區(qū),其實(shí)道理和之前在http://www.cnblogs.com/jackyrong/archive/2006/11/13/559354.html說到一樣,只不過交換下順序而已,下面依然用例子說明:
依然在c盤的data2目錄下建立4個(gè)文件夾,用來做4個(gè)文件組,然后建立數(shù)據(jù)庫
use master
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'Data Partition DB3')
DROP DATABASE [Data Partition DB3]
GO
CREATE DATABASE [Data Partition DB3]
ON PRIMARY
(NAME='Data Partition DB Primary FG3',
FILENAME=
'C:\Data2\Primary\Data Partition DB Primary FG3.mdf',
SIZE=5,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [Data Partition DB3 FG1]
(NAME = 'Data Partition DB3 FG1',
FILENAME =
'C:\Data2\FG1\Data Partition DB3 FG1.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [Data Partition DB3 FG2]
(NAME = 'Data Partition DB3 FG2',
FILENAME =
'C:\Data2\FG2\Data Partition DB3 FG2.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [Data Partition DB3 FG3]
(NAME = 'Data Partition DB3 FG3',
FILENAME =
'C:\Data2\FG3\Data Partition DB3 FG3.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 ),
FILEGROUP [Data Partition DB3 FG4]
(NAME = 'Data Partition DB3 FG4',
FILENAME =
'C:\Data2\FG4\Data Partition DB3 FG4.ndf',
SIZE = 5MB,
MAXSIZE=500,
FILEGROWTH=1 )
然后建立一個(gè)數(shù)據(jù)表:
USE [Data Partition DB3]
go
CREATE TABLE MyTable
(ID INT NOT NULL,
Date DATETIME,
Cost money ) on [primary]
并建立一個(gè)索引
USE [Data Partition DB3]
go
CREATE UNIQUE CLUSTERED INDEX MyTable_IXC
ON MyTable(ID) on [PRIMARY]
接下來往表里增加數(shù)據(jù)
USE [Data Partition DB3]
go
declare @count int
set @count =-25
while @count <=100
begin
insert into MyTable select @count,getdate(),100.00
set @count=@count+1
end
set @count =101
while @count <=200
begin
insert into MyTable select @count,getdate(),200.00
set @count=@count+1
end
set @count =201
while @count <=300
begin
insert into MyTable select @count,getdate(),300.00
set @count=@count+1
end
set @count =301
while @count <=400
begin
insert into MyTable select @count,getdate(),400.00
set @count=@count+1
end
set @count =401
while @count <=800
begin
insert into MyTable select @count,getdate(),500.00
set @count=@count+1
end
此時(shí)查詢一下,可以看到數(shù)據(jù)都在一個(gè)表里select * from sys.partitions where object_name(object_id)='MyTable'
我們?cè)俳⒈矸謪^(qū)函數(shù)use [Data Partition DB3]
GO
CREATE PARTITION FUNCTION [Data Partition Range](int)
AS RANGE LEFT FOR VALUES (100,200,300)
這里表明分區(qū)的原則是四個(gè)分區(qū),從負(fù)數(shù)到100,101-200,201-300,大于300
當(dāng)然,如果用right for values的話,就是從負(fù)數(shù)到99,100到199,200-299,和大于300
最后,把表分區(qū)函數(shù)應(yīng)用到文件組里
USE [Data Partition DB3]
go
CREATE PARTITION SCHEME [Data Partition Scheme]
AS PARTITION [Data Partition Range]
TO ([Data Partition DB3 FG1], [Data Partition DB3 FG2], [Data Partition DB3 FG3],[Data Partition DB3 FG4]);
把原來建立好的表,移動(dòng)到這個(gè)表分區(qū)里
Drop index MyTable_IXC on MyTable with (Move To [Data Partition Scheme] (ID) )
最后看一看select * from sys.partitions where object_name(object_id)='MyTable'
可以看到,原來的表的數(shù)據(jù)被正確分拆到四個(gè)文件組里去了,實(shí)現(xiàn)了表分區(qū)
http://www.cnblogs.com/jackyrong/archive/2006/11/16/562514.html
相關(guān)文章
WordPress導(dǎo)入數(shù)據(jù)庫出現(xiàn)”Unknown collation: ‘utf8mb4_unicode_ci”錯(cuò)誤的解
這篇文章主要介紹了WordPress導(dǎo)入數(shù)據(jù)庫出現(xiàn)”Unknown collation: ‘utf8mb4_unicode_ci”錯(cuò)誤的解決辦法的相關(guān)資料,需要的朋友可以參考下2015-10-10
關(guān)于SQL注入繞過的一些知識(shí)點(diǎn)
網(wǎng)上關(guān)于SQL注入的繞過技巧有很多,最近正好空下來,想著整理下關(guān)于SQL注入繞過的一些姿勢(shì)。歡迎大牛補(bǔ)充,下面這篇文章主要介紹了關(guān)于SQL注入繞過的一些知識(shí)點(diǎn),總結(jié)的還是相對(duì)比較全面的,需要的朋友可以參考下。2017-03-03
最新Navicat?16??Mac版安裝永久激活教程(親測(cè)有效)
這篇文章主要介紹了最新Navicat?16??Mac版安裝永久激活教程(親測(cè)有效),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)Navicat?16?永久激活教程感興趣的朋友一起看看吧2022-08-08
分布式緩存Redis與Memcached的優(yōu)缺點(diǎn)區(qū)別比較
Redis和Memcached都是基于內(nèi)存key-value的數(shù)據(jù)存儲(chǔ)系統(tǒng)。兩者都可以通過緩存數(shù)據(jù)結(jié)果,HTML片段或其他可能產(chǎn)生成本很高的內(nèi)容來幫助加快應(yīng)用程序的速度。與memcached相比,Redis功能更強(qiáng)大,更受歡迎并且得到更好的支持。2022-12-12
OceanBase建表分區(qū)數(shù)超限報(bào)錯(cuò)解決分析
這篇文章主要為大家介紹了OceanBase建表分區(qū)數(shù)超限報(bào)錯(cuò)解決分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
SQL語句優(yōu)化之JOIN和LEFT JOIN 和 RIGHT JOIN語句的優(yōu)化
在數(shù)據(jù)庫的應(yīng)用中,我們經(jīng)常需要對(duì)數(shù)據(jù)庫進(jìn)行多表查詢,然而當(dāng)數(shù)據(jù)量非常大時(shí)多表查詢會(huì)對(duì)執(zhí)行效率產(chǎn)生非常大的影響,因此我們?cè)谑褂肑OIN和LEFT JOIN 和 RIGHT JOIN語句時(shí)要特別注意2018-03-03
數(shù)據(jù)庫分庫分表是什么,什么情況下需要用分庫分表
這篇文章主要介紹了數(shù)據(jù)庫分庫分表是什么,什么情況下需要用分庫分表,需要的朋友可以參考下2021-03-03

