在testing中,爲了模擬orders,有個要求給數據庫dba,如何經過後臺數據庫腳本快速批量生成orders。sql
站在數據庫角度,批量生成orders,也就是批量生成表中的行數據。數據庫
sql中,經過cross join 能夠把兩個table (如 A ,B )組合,造成一個笛卡爾積,如圖1spa
圖1code
若是,對圖1的組合結果,進行一次迭代組合,那麼就能夠獲得一個16行的結果,如圖2:blog
圖2it
在sql sever 中,經過下面的sql語句分析須要A, B表組合,迭代多少次能夠能生成上百萬行的記錄,io
use tempdb go declare @x bigint =2 declare @i int=1 while(1=1) begin set @x=square(@x) if @@ERROR<>0 break; print rtrim(@i)+' : '+rtrim(sqrt(@x))+' x '+rtrim(sqrt(@x))+' = '+rtrim(@x); set @i+=1; end
從這能夠看到A,B表組合,須要迭代5次就能夠生成上百萬行數據。table
SQL SERVER 代碼:class
;With a0 As(Select id=1 Union All Select id=1), a1 As(Select a.id From a0 a,a0 b), a2 As(Select a.id From a1 a,a1 b), a3 As(Select a.id From a2 a,a2 b), a4 As(Select a.id From a3 a,a3 b), t As(Select id=Row_number() Over(Order By a.id) From a4 a,a4 b) select top 1000000 id from t
到這裏,批量生成數據行,已能實現。根據實際的須要能夠附加其餘的條件或數據,便可知足開頭部分的需求。test