sql 生成100W條指定位數的隨機數的方法(只花費了不足1分鐘)(整理)
應用例子:比如飲料的中獎號碼,N多瓶每瓶有個隨機號碼
o覺得有些地方值得學習:
1.一次性插入儘可能多的記錄(不判斷是否已經存在該隨機數),而不是一次插入一條記錄(插入前要判斷是否已經存在該隨機數).這裡重複記錄通過設定忽略重複記錄的索引來實現的.
"一次性的插入儘可能多的記錄"和"忽略重複記錄的索引"節約的時間簡直太多了....我使用建立索引後的迴圈插入50W記錄都花了2-3個小時....
"一次性的插入儘可能多的記錄"和"忽略重複記錄的索引"節約的時間簡直太多了....我使用建立索引後的迴圈插入50W記錄都花了2-3個小時....
2.SET NOCOUNT ON 這樣也可以節約時間 ,但測試發現設定為OFF或ON,其時間幾乎相同的都是47或46秒左右
3.這裡雖然也使用了迴圈,但它的迴圈次數是不固定的.其次數取和每次插入失敗的記錄數有關.失敗的記錄數為0的時候,任務也就完成了.我測試需要迴圈50次左右就可以了.
4.syscolumns和sysobjects兩個表的作用是獲得足夠數量的隨機數,兩個表的交叉查詢記錄大於100W就可以了.
--生成8位長度的100W條記錄的隨機數,插入資料庫.
USE tempdb
GO
CREATE TABLE tb(id char(8))
CREATE UNIQUE INDEX IX_tb ON tb(id)
WITH IGNORE_DUP_KEY -- --忽略重複記錄的索引
GO
DECLARE @dt datetime
SET @dt = GETDATE()
SET NOCOUNT OFF
DECLARE @row int
SET @row = 1000000 --記錄總數為100W
WHILE @row >0
BEGIN
RAISERROR('need %d rows', 10, 1, @row) WITH NOWAIT
SET ROWCOUNT @row
INSERT tb SELECT
id = RIGHT(100000000 + CONVERT(bigint, ABS(CHECKSUM(NEWID()))), 8)
--FROM syscolumns c1, syscolumns c2 -- SET NOCOUNT ON 49 Second
FROM syscolumns c1, sysobjects o -- SET NOCOUNT ON 47 Second
SET @row = @row - @@ROWCOUNT --設定下次迴圈需要插入的記錄數量
END
SELECT BeginDate = @dt, EndDate = GETDATE(), Second = DATEDIFF(Second, @dt, GETDATE())
GO
SELECT COUNT(*) FROM tb
GO
DROP TABLE tb
USE tempdb
GO
CREATE TABLE tb(id char(8))
CREATE UNIQUE INDEX IX_tb ON tb(id)
WITH IGNORE_DUP_KEY -- --忽略重複記錄的索引
GO
DECLARE @dt datetime
SET @dt = GETDATE()
SET NOCOUNT OFF
DECLARE @row int
SET @row = 1000000 --記錄總數為100W
WHILE @row >0
BEGIN
RAISERROR('need %d rows', 10, 1, @row) WITH NOWAIT
SET ROWCOUNT @row
INSERT tb SELECT
id = RIGHT(100000000 + CONVERT(bigint, ABS(CHECKSUM(NEWID()))), 8)
--FROM syscolumns c1, syscolumns c2 -- SET NOCOUNT ON 49 Second
FROM syscolumns c1, sysobjects o -- SET NOCOUNT ON 47 Second
SET @row = @row - @@ROWCOUNT --設定下次迴圈需要插入的記錄數量
END
SELECT BeginDate = @dt, EndDate = GETDATE(), Second = DATEDIFF(Second, @dt, GETDATE())
GO
SELECT COUNT(*) FROM tb
GO
DROP TABLE tb
--測試結果:
BeginDate EndDate Second
2006-10-26 16:03:17.483 2006-10-26 16:04:04.093 47
BeginDate EndDate Second
2006-10-26 16:03:17.483 2006-10-26 16:04:04.093 47
need 1000000 rows
Duplicate key was ignored.
need 975756 rows
Duplicate key was ignored.
need 951522 rows
Duplicate key was ignored.
need 927292 rows
Duplicate key was ignored.
need 903082 rows
Duplicate key was ignored.
need 878862 rows
Duplicate key was ignored.
need 854648 rows
Duplicate key was ignored.
need 830435 rows
Duplicate key was ignored.
need 806230 rows
Duplicate key was ignored.
need 782030 rows
Duplicate key was ignored.
need 757841 rows
Duplicate key was ignored.
need 733669 rows
Duplicate key was ignored.
need 709495 rows
Duplicate key was ignored.
need 685307 rows
Duplicate key was ignored.
need 661143 rows
Duplicate key was ignored.
need 636988 rows
Duplicate key was ignored.
need 612827 rows
Duplicate key was ignored.
need 588674 rows
Duplicate key was ignored.
need 564527 rows
Duplicate key was ignored.
need 540378 rows
Duplicate key was ignored.
need 516240 rows
Duplicate key was ignored.
need 492132 rows
Duplicate key was ignored.
need 468030 rows
Duplicate key was ignored.
need 443902 rows
Duplicate key was ignored.
need 419780 rows
Duplicate key was ignored.
need 395707 rows
Duplicate key was ignored.
need 371617 rows
Duplicate key was ignored.
need 347528 rows
Duplicate key was ignored.
need 323431 rows
Duplicate key was ignored.
need 299338 rows
Duplicate key was ignored.
need 275260 rows
Duplicate key was ignored.
need 251171 rows
Duplicate key was ignored.
need 227119 rows
Duplicate key was ignored.
need 203050 rows
Duplicate key was ignored.
need 178982 rows
Duplicate key was ignored.
need 154966 rows
Duplicate key was ignored.
need 130920 rows
Duplicate key was ignored.
need 106881 rows
Duplicate key was ignored.
need 82863 rows
Duplicate key was ignored.
need 58852 rows
Duplicate key was ignored.
need 34807 rows
Duplicate key was ignored.
need 10777 rows
Duplicate key was ignored.
need 104 rows
Duplicate key was ignored.
need 1 rows
Duplicate key was ignored.
need 975756 rows
Duplicate key was ignored.
need 951522 rows
Duplicate key was ignored.
need 927292 rows
Duplicate key was ignored.
need 903082 rows
Duplicate key was ignored.
need 878862 rows
Duplicate key was ignored.
need 854648 rows
Duplicate key was ignored.
need 830435 rows
Duplicate key was ignored.
need 806230 rows
Duplicate key was ignored.
need 782030 rows
Duplicate key was ignored.
need 757841 rows
Duplicate key was ignored.
need 733669 rows
Duplicate key was ignored.
need 709495 rows
Duplicate key was ignored.
need 685307 rows
Duplicate key was ignored.
need 661143 rows
Duplicate key was ignored.
need 636988 rows
Duplicate key was ignored.
need 612827 rows
Duplicate key was ignored.
need 588674 rows
Duplicate key was ignored.
need 564527 rows
Duplicate key was ignored.
need 540378 rows
Duplicate key was ignored.
need 516240 rows
Duplicate key was ignored.
need 492132 rows
Duplicate key was ignored.
need 468030 rows
Duplicate key was ignored.
need 443902 rows
Duplicate key was ignored.
need 419780 rows
Duplicate key was ignored.
need 395707 rows
Duplicate key was ignored.
need 371617 rows
Duplicate key was ignored.
need 347528 rows
Duplicate key was ignored.
need 323431 rows
Duplicate key was ignored.
need 299338 rows
Duplicate key was ignored.
need 275260 rows
Duplicate key was ignored.
need 251171 rows
Duplicate key was ignored.
need 227119 rows
Duplicate key was ignored.
need 203050 rows
Duplicate key was ignored.
need 178982 rows
Duplicate key was ignored.
need 154966 rows
Duplicate key was ignored.
need 130920 rows
Duplicate key was ignored.
need 106881 rows
Duplicate key was ignored.
need 82863 rows
Duplicate key was ignored.
need 58852 rows
Duplicate key was ignored.
need 34807 rows
Duplicate key was ignored.
need 10777 rows
Duplicate key was ignored.
need 104 rows
Duplicate key was ignored.
need 1 rows
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/16436858/viewspace-609103/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- python生成隨機數的方法Python隨機
- 生成隨機整數方法隨機
- numpy各種生成隨機數的方法隨機
- javascript生成指定位數隨機數程式碼例項JavaScript隨機
- 幾種生成隨機數方法隨機
- 生成隨機數隨機
- [隨機數詳解]生成一個隨機數,生成指定範圍的隨機數及隨機陣列去重隨機陣列
- Python隨機數生成方法Python隨機
- 更安全的隨機數生成隨機
- PHP 生成更好的隨機數PHP隨機
- 偽隨機數是什麼?偽隨機數生成方法有哪些?隨機
- 大佬求助,生成6000條資料居然花費30分鐘?
- Matlab 隨機生成兩個數值之間的隨機數Matlab隨機
- C# 生成隨機數,呼叫Random方法C#隨機random
- JavaScript 生成隨機數JavaScript隨機
- mysql生成隨機數MySql隨機
- 【知識積累】隨機數生成的幾種方法隨機
- python生成隨機數、隨機字串Python隨機字串
- SQL Server當中生成一定範圍的隨機數SQLServer隨機
- 隨機數生成器隨機
- java隨機數生成原理Java隨機
- python 隨機數生成Python隨機
- C++生成隨機數C++隨機
- 【java】隨機生成6位的數字Java隨機
- 生成指定區間的隨機小數隨機
- 生成某個範圍的隨機數隨機
- 如何生成指定分佈的隨機數隨機
- java生成指定範圍的隨機數Java隨機
- Python如何隨機生成1到100的隨機數?Python隨機
- Solidity陷阱:以太坊的隨機數生成Solid隨機
- 微信小程式生成隨機數微信小程式隨機
- 使用Math類生成隨機數隨機
- matlab 生成隨機數序列Matlab隨機
- 無重複隨機數生成隨機
- 在oracle 中生成隨機數Oracle隨機
- iOS生成最大最小數之間隨機數iOS隨機
- Linux Shell 生成隨機數和隨機字串Linux隨機字串
- js隨機數生成器的擴充套件JS隨機套件