首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> SQL Server 資料庫 >> 關於SqlServer

SQL Server 批次插入資料實戰:單筆、迴圈、時間序列測試腳本

     編輯:關於SqlServer

測試情境:BI 軟體效能測試

測試 CUBE 採 ROLAP 下 PA 並發與穩定性。涉及事實表 sales_fact_1997、維度表 time_by_day、CUBE sales。修改內容:刪除原 Time 維度、新建 Time 維度、修改 Sales CUBE。

資料插入策略

  • 事實資料:DTS + 排程,每分鐘複製 10,000 筆(SELECT TOP 10000 * FROM sales_fact_1997)
  • 維度資料:SQL 腳本插入 time_by_day

四種插入腳本範例

1. 單筆 INSERT VALUES

INSERT INTO time_by_day (time_id, the_date, the_year, month_of_year, quarter, day_of_month)
VALUES ('1101', '1999-10-1', '1999', '10', 'Q4', '1');

2. 單筆 INSERT SELECT(遞增邏輯)

INSERT INTO time_by_day (time_id, the_date, the_year, month_of_year, quarter, day_of_month)
SELECT TOP 1 time_id + 1, the_date + 1, YEAR(the_date + 1), MONTH(the_date + 1),
       { fn QUARTER(the_date + 1) }, DAY(the_date + 1)
FROM time_by_day ORDER BY time_id DESC;

3. WHILE 迴圈批次插入(含延遲)

DECLARE @MyCounter INT; SET @MyCounter = 0;
WHILE (@MyCounter < 2)
BEGIN
  WAITFOR DELAY '00:00:10';
  INSERT INTO time_by_day (...) -- 同方式 2 的 SELECT
  SET @MyCounter = @MyCounter + 1;
END

4. 以時間變數驅動的大量插入

DECLARE @MyCounter INT, @the_date DATETIME;
SET @MyCounter = 0; SET @the_date = '1999-1-4';
WHILE (@MyCounter < 200000)
BEGIN
  WAITFOR DELAY '00:00:10';
  INSERT INTO time_by_day (time_id, the_date) VALUES ('371', @the_date);
  SET @the_date = @the_date + 1;
  SET @MyCounter = @MyCounter + 1;
END
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved