日期:2014-05-19 浏览次数:22503 次
--用维度表存储URL,以压缩空间 CREATE TABLE tblURL( intURLKey int NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, strURL varchar(500) NOT NULL UNIQUE) GO --以下为事实表的方案: --方案一: CREATE TABLE tblWebVisitLog1( dtVisitTime datetime NOT NULL, binIP binary(4) NOT NULL, intURLKey int NOT NULL FOREIGN KEY REFERENCES tblURL(intURLKey)) CREATE CLUSTERED INDEX IX_tblWebVisitLog1 ON tblWebVisitLog1(dtVisitTime) --非唯一聚集索引 CREATE INDEX IX_tblWebVisitLog1_binIP ON tblWebVisitLog1(binIP) CREATE INDEX IX_tblWebVisitLog1_intURLKey ON tblWebVisitLog1(intURLKey) --方案二: CREATE TABLE tblWebVisitLog2( dtVisitTime datetime NOT NULL, binIP binary(4) NOT NULL, intURLKey int NOT NULL FOREIGN KEY REFERENCES tblURL(intURLKey)) CREATE INDEX IX_tblWebVisitLog2 ON tblWebVisitLog2(dtVisitTime) --表上无聚集索引 CREATE INDEX IX_tblWebVisitLog2_binIP ON tblWebVisitLog2(binIP) CREATE INDEX IX_tblWebVisitLog2_intURLKey ON tblWebVisitLog2(intURLKey) --方案三: CREATE TABLE tblWebVisitLog3( intLogSN int NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, --以自增的主键为唯一聚集索引 dtVisitTime datetime NOT NULL, binIP binary(4) NOT NULL, intURLKey int NOT NULL FOREIGN KEY REFERENCES tblURL(intURLKey)) CREATE INDEX IX_tblWebVisitLog3 ON tblWebVisitLog3(dtVisitTime) CREATE INDEX IX_tblWebVisitLog3_binIP ON tblWebVisitLog3(binIP) CREATE INDEX IX_tblWebVisitLog3_intURLKey ON tblWebVisitLog3(intURLKey)
--根据Nums表生成测试数据 INSERT INTO tblURL(strURL) SELECT strURL = '/' + RIGHT('00000' + CAST(n AS varchar(10)),5) + '.html' FROM Nums WHERE n BETWEEN 1 AND 10000 INSERT INTO tblWebVisitLog1 SELECT dtVisitTime = DATEADD(second,n / (n % 5 + 5),'20100601'), --同一时刻可能有5到10条访问记录,即VisitTime不唯一 binIP = CAST(ABS(CHECKSUM(NEWID())) % 50 + 200 AS binary(1)) + CAST(ABS(CHECKSUM(NEWID())) % 16777216 AS binary(3)), --限定IP地址在200.x.x.x到249.x.x.x范围内 intURIKey = ABS(CHECKSUM(NEWID())) % 10000 + 1 --限定有效的tblURL键值 FROM Nums WHERE n BETWEEN 1 AND 1000000 --只测试了100万条记录的情况 INSERT INTO tblWebVisitLog2 SELECT * FROM tblWebVisitLog1 INSERT INTO tblWebVisitLog3 SELECT * FROM tblWebVisitLog1
--存储空间 EXEC sp_spaceused 'tblWebVisitLog1' EXEC sp_spaceused 'tblWebVisitLog2' EXEC sp_spaceused 'tblWebVisitLog3' --按VisitTime字段查找 SELECT TOP(10) * FROM tblWebVisitLog1 WHERE dtVisitTime BETWEEN '20100601 03:00' AND '20100601 03:05' SELECT TOP(10) * FROM tblWebVisitLog2 WHERE dtVisitTime BETWEEN '20100601 03:00' AND '20100601 03:05' SELECT TOP(10) * FROM tblWebVisitLog3 WHERE dtVisitTime BETWEEN '20100601 03:00' AND '20100601 03:05' --按IP字段查找(按URL字段查找情况类似) SELECT TOP(10) * FROM tblWebVisitLog1 WHERE binIP BETWEEN 0xD5BC0000 AND 0xD5BCFFFF SELECT TOP(10) * FROM tblWebVisitLog2 WHERE binIP BETWEEN 0xD5BC0000 AND 0xD5BCFFFF SELECT TOP(10) * FROM tblWebVisitLog3 WHERE binIP BETWEEN 0xD5BC0000 AND 0xD5BCFFFF
这个有点艰难