(以項目中實際使用的GNSS庫為例) 背景:數據量巨大,定時創建月表存放數據,月表中數據存放在不同的文件組中來提高查詢效率 一、創建資料庫,添加文件組 除了邏輯文件和物理文件的分離之外,SQL Server使用文件組還有一個優勢,那就是分散IO負載,其實現的原理是: 對於單分區表,數據只能存到一個文 ...
(以項目中實際使用的GNSS庫為例) 背景:數據量巨大,定時創建月表存放數據,月表中數據存放在不同的文件組中來提高查詢效率 一、創建資料庫,添加文件組 除了邏輯文件和物理文件的分離之外,SQL Server使用文件組還有一個優勢,那就是分散IO負載,其實現的原理是:
- 對於單分區表,數據只能存到一個文件組中。如果把文件組內的數據文件分佈在不同的物理硬碟上,那麼SQL Server能同時從不同的物理硬碟上讀寫數據,把IO負載分散到不同的硬碟上。
- 對於多分區表,每個分區使用一個文件組,把不同的數據子集存儲在不同的磁碟上,SQL Server在讀寫某一個分組的數據時,能夠調用不同的硬碟IO。
CREATE DATABASE [GNSS] CONTAINMENT = NONE ON PRIMARY ( NAME = N'GNSS', FILENAME = N'D:\Databases\GNSS\GNSS.mdf' , SIZE = 6144KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), FILEGROUP [GNSSFG0] DEFAULT ( NAME = N'GNSSFile0', FILENAME = N'D:\Databases\GNSS\GNSSFile0.ndf' , SIZE = 287744KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB ), FILEGROUP [GNSSFG1] ( NAME = N'GNSSFile1', FILENAME = N'D:\Databases\GNSS\GNSSFile1.ndf' , SIZE = 778240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG10] ( NAME = N'GNSSFile10', FILENAME = N'D:\Databases\GNSS\GNSSFile10.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG11] ( NAME = N'GNSSFile11', FILENAME = N'D:\Databases\GNSS\GNSSFile11.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG12] ( NAME = N'GNSSFile12', FILENAME = N'D:\Databases\GNSS\GNSSFile12.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG13] ( NAME = N'GNSSFile13', FILENAME = N'D:\Databases\GNSS\GNSSFile13.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG14] ( NAME = N'GNSSFile14', FILENAME = N'D:\Databases\GNSS\GNSSFile14.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG15] ( NAME = N'GNSSFile15', FILENAME = N'D:\Databases\GNSS\GNSSFile15.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG16] ( NAME = N'GNSSFile16', FILENAME = N'D:\Databases\GNSS\GNSSFile16.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG17] ( NAME = N'GNSSFile17', FILENAME = N'D:\Databases\GNSS\GNSSFile17.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG18] ( NAME = N'GNSSFile18', FILENAME = N'D:\Databases\GNSS\GNSSFile18.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG19] ( NAME = N'GNSSFile19', FILENAME = N'D:\Databases\GNSS\GNSSFile19.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG2] ( NAME = N'GNSSFile2', FILENAME = N'D:\Databases\GNSS\GNSSFile2.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG20] ( NAME = N'GNSSFile20', FILENAME = N'D:\Databases\GNSS\GNSSFile20.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG21] ( NAME = N'GNSSFile21', FILENAME = N'D:\Databases\GNSS\GNSSFile21.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG22] ( NAME = N'GNSSFile22', FILENAME = N'D:\Databases\GNSS\GNSSFile22.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG23] ( NAME = N'GNSSFile23', FILENAME = N'D:\Databases\GNSS\GNSSFile23.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG24] ( NAME = N'GNSSFile24', FILENAME = N'D:\Databases\GNSS\GNSSFile24.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG25] ( NAME = N'GNSSFile25', FILENAME = N'D:\Databases\GNSS\GNSSFile25.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG26] ( NAME = N'GNSSFile26', FILENAME = N'D:\Databases\GNSS\GNSSFile26.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG27] ( NAME = N'GNSSFile27', FILENAME = N'D:\Databases\GNSS\GNSSFile27.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG28] ( NAME = N'GNSSFile28', FILENAME = N'D:\Databases\GNSS\GNSSFile28.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG29] ( NAME = N'GNSSFile29', FILENAME = N'D:\Databases\GNSS\GNSSFile29.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG3] ( NAME = N'GNSSFile3', FILENAME = N'D:\Databases\GNSS\GNSSFile3.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG30] ( NAME = N'GNSSFile30', FILENAME = N'D:\Databases\GNSS\GNSSFile30.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG31] ( NAME = N'GNSSFile31', FILENAME = N'D:\Databases\GNSS\GNSSFile31.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG4] ( NAME = N'GNSSFile4', FILENAME = N'D:\Databases\GNSS\GNSSFile4.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG5] ( NAME = N'GNSSFile5', FILENAME = N'D:\Databases\GNSS\GNSSFile5.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG6] ( NAME = N'GNSSFile6', FILENAME = N'D:\Databases\GNSS\GNSSFile6.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG7] ( NAME = N'GNSSFile7', FILENAME = N'D:\Databases\GNSS\GNSSFile7.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG8] ( NAME = N'GNSSFile8', FILENAME = N'D:\Databases\GNSS\GNSSFile8.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ), FILEGROUP [GNSSFG9] ( NAME = N'GNSSFile9', FILENAME = N'D:\Databases\GNSS\GNSSFile9.ndf' , SIZE = 10240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 51200KB ) LOG ON ( NAME = N'GNSS_log', FILENAME = N'D:\Databases\GNSS\GNSS_log.ldf' , SIZE = 10240KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)View Code
二、創建分區函數和分區方案
分區函數定義了用於分區的數據邊界,而分區方案指定了符合分區邊界的數據存放在哪個文件組。因此,分區方案中指定的文件組個數應該是比分區函數中指定的邊界數大1的。
USE [GNSS] GO /****** Object: PartitionFunction [PF201808] ******/ CREATE PARTITION FUNCTION [PF201808](datetime) AS RANGE RIGHT FOR VALUES (N'2018-08-02T00:00:00.000', N'2018-08-03T00:00:00.000', N'2018-08-04T00:00:00.000', N'2018-08-05T00:00:00.000', N'2018-08-06T00:00:00.000', N'2018-08-07T00:00:00.000', N'2018-08-08T00:00:00.000', N'2018-08-09T00:00:00.000', N'2018-08-10T00:00:00.000', N'2018-08-11T00:00:00.000', N'2018-08-12T00:00:00.000', N'2018-08-13T00:00:00.000', N'2018-08-14T00:00:00.000', N'2018-08-15T00:00:00.000', N'2018-08-16T00:00:00.000', N'2018-08-17T00:00:00.000', N'2018-08-18T00:00:00.000', N'2018-08-19T00:00:00.000', N'2018-08-20T00:00:00.000', N'2018-08-21T00:00:00.000', N'2018-08-22T00:00:00.000', N'2018-08-23T00:00:00.000', N'2018-08-24T00:00:00.000', N'2018-08-25T00:00:00.000', N'2018-08-26T00:00:00.000', N'2018-08-27T00:00:00.000', N'2018-08-28T00:00:00.000', N'2018-08-29T00:00:00.000', N'2018-08-30T00:00:00.000', N'2018-08-31T00:00:00.000') GO USE [GNSS] GO /****** Object: PartitionScheme [PS201808] ******/ CREATE PARTITION SCHEME [PS201808] AS PARTITION [PF201808] TO ([GNSSFG1], [GNSSFG2], [GNSSFG3], [GNSSFG4], [GNSSFG5], [GNSSFG6], [GNSSFG7], [GNSSFG8], [GNSSFG9], [GNSSFG10], [GNSSFG11], [GNSSFG12], [GNSSFG13], [GNSSFG14], [GNSSFG15], [GNSSFG16], [GNSSFG17], [GNSSFG18], [GNSSFG19], [GNSSFG20], [GNSSFG21], [GNSSFG22], [GNSSFG23], [GNSSFG24], [GNSSFG25], [GNSSFG26], [GNSSFG27], [GNSSFG28], [GNSSFG29], [GNSSFG30], [GNSSFG31]) GOView Code
三、創建分區表
分區表跟普通表創建有點不一樣,分區表的創建還需要指定這個分區需要使用哪個分區方案下的分區欄位,那麼這裡就是[PS201808]中的[SignalDateTime]欄位。
CREATE TABLE [dbo].[201808]( [VIN] [char](17) NOT NULL, [TerminalCode] [varchar](20) NOT NULL, [Latitude] [float] NOT NULL, [Longitude] [float] NOT NULL, [Direction] [smallint] NOT NULL, [Speed] [float] NOT NULL, [Elevation] [int] NOT NULL, [StateFlag] [bigint] NOT NULL, [AlarmFlag] [bigint] NOT NULL, [ServerDateTime] [datetime] NOT NULL, [SignalDateTime] [datetime] NOT NULL, [IsBlind] [bit] NOT NULL, [ACCState] [bit] NOT NULL, [PositioningState] [bit] NOT NULL, [ServerCode] [varchar](20) NOT NULL, [TerminalSIM] [varchar](13) NULL, [PlateNumber] [nvarchar](8) NULL, [ExtraStateFlag] [bigint] NULL, [ExtraStateFlag2] [bigint] NULL, [ExtraAlarmFlag] [bigint] NULL, [ExtraAlarmFlag2] [bigint] NULL, [RollerState] [tinyint] NULL, [Electricity] [float] NULL, [Temperature] [varchar](20) NULL, [OilHeight] [float] NULL, [Mileage] [float] NULL, [OilVolume] [float] NULL, [DRSpeed] [float] NULL, [SignalStrength] [tinyint] NULL, [SatelliteCount] [tinyint] NULL, [ExtendedState] [bigint] NULL, [IOState] [int] NULL, [OverspeedLocationType] [tinyint] NULL, [OverspeedAreaOrLineID] [bigint] NULL, [InAndOutAreaOrLineLocationType] [tinyint] NULL, [InAndOutAreaOrLineID] [bigint] NULL, [InAndOutAreaOrLineDirection] [bit] NULL, [LineDrivingTimeTooLongOrNotEnoughID] [bigint] NULL, [LineDrivingTime] [int] NULL, [LineDrivingResult] [bit] NULL ) ON [PS201808]([SignalDateTime])View Code