在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