Using Cross Join to insert millions of dummy/fake records

cross join

Sometimes we need fake or dummy data to test our queries or test performance or configuration of database server. There is a very neat way to generate millions of records very quickly using Cross Join.

A cross join is used when you wish to create combination of every row from two tables. All row combinations are included in the result. This is commonly called cross product join. A common use for a cross join is to create obtain all combinations of items, such as sizes and colors.

cross join

You can use sys.columns with Cross Join to generate millions of records instantly. Trick is to do a CROSS JOIN between sys.columns and use that same 2, 3, 4 or more times. Have a TOP, with as many rows as you want. Use that as a base for INSERT INTO.

Here is a sample query:

CREATE TABLE dummytable (c1 int identity(1, 1), c2 char(10));

INSERT INTO dummytable
SELECT TOP(1000) 'DummyData'
FROM sys.columns AS a
CROSS JOIN sys.columns AS b
CROSS JOIN sys.columns AS c

The cross join is a classic trick. Do a cross join between two tables that has 1000 rows each and you have 1,000,000 rows in the result. With 3 tables, you have 1,000,000,000 rows. You can have some “numbers” table you create for these purposes, with for instance 1000 rows in it. Or, do a quick and dirty and use some view that you know exists with a decent amount or rows in it (like sys.columns).