Last active
October 10, 2017 16:51
-
-
Save hosseinm1997/0ba46afdc27065bd515b1926974bf63a to your computer and use it in GitHub Desktop.
Create huge number of data into a sample table for benchmarking
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
USE [master] | |
GO | |
declare @dbname nvarchar(20) | |
set @dbname = 'HM_benchmark' -- => set db name here <= -- | |
IF OBJECT_ID('tempdb.dbo.#vars', 'U') IS NOT NULL | |
DROP TABLE #vars; | |
create table #vars (dbname varchar(20), create_script nvarchar(80),use_script nvarchar(80)) | |
insert #vars values (@dbname,'Create DATABASE ' + @dbname,'USE [' + @dbname + ']') | |
--//-------------Start Create Database--------------\\-- | |
IF (NOT EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE ('[' + name + ']' = @dbname OR name = @dbname))) | |
BEGIN | |
declare @tmp as nvarchar(80) | |
select @tmp = create_script from #vars | |
exec sp_executesql @tmp | |
END | |
--//-------------End Create Database--------------\\-- | |
--//-------------Start Table Database--------------\\-- | |
GO | |
declare @statement as nvarchar(Max) | |
select @statement = use_script from #vars | |
set @statement = @statement + ' | |
SET ANSI_NULLS ON | |
SET QUOTED_IDENTIFIER ON | |
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[dbo].[tbl_5million]'') AND type in (N''U'')) | |
BEGIN | |
CREATE TABLE [dbo].[tbl_5million]( | |
[id] [int] IDENTITY(1,1) NOT NULL, | |
[first_name] [nvarchar](50) NOT NULL, | |
[last_name] [nvarchar](50) NOT NULL, | |
[phone] [bigint] NOT NULL, | |
[active] [bit] NOT NULL, | |
[date] [datetime] NOT NULL, | |
CONSTRAINT [PK_tbl_5milion] PRIMARY KEY CLUSTERED | |
( | |
[id] ASC | |
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] | |
) ON [PRIMARY] | |
END' | |
exec sp_executesql @statement | |
--//-------------End Create Table--------------\\-- | |
--//-------------Start Create Procedure--------------\\-- | |
GO | |
SET ANSI_NULLS ON | |
GO | |
SET QUOTED_IDENTIFIER ON | |
GO | |
declare @statement as nvarchar(MAX) | |
select @statement = use_script from #vars | |
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[' + (select dbname from #vars) + '].[dbo].[generateRandomData]') AND type in (N'P', N'PC')) | |
BEGIN | |
set @statement = 'CREATE PROCEDURE [dbo].[generateRandomData] | |
@number int | |
AS | |
BEGIN | |
if (@number is null) or (@number = 0) | |
return 1 | |
declare @i int, | |
@first_name nvarchar(50), | |
@last_name nvarchar(50), | |
@date nvarchar(50), | |
@phone bigint, | |
@active bit, | |
@a int,@b int,@c int | |
declare @first_names table(id tinyint,title nvarchar(50)) | |
insert into @first_names(id,title)values | |
(1,''hossein''),(2,''farid''),(3,''reza''),(4,''yekta''),(5,''zahra''),(6,''sina''),(7,''mahdi''),(8,''super myth'') | |
declare @last_names table(id tinyint,title nvarchar(50)) | |
insert into @last_names(id,title)values | |
(1,''moshfegh''),(2,''khooyi''),(3,''samen''),(4,''sohaniyan''),(5,''esmaeiliyan''),(6,''yar''),(7,''bagheri''),(8,''ghasemi'') | |
declare @dates table(id tinyint,title nvarchar(50)) | |
insert into @dates(id,title)values | |
(1,''2017-05-09''),(2,''2014-06-12''),(3,''2013-02-20''),(4,''2017-01-10''),(5,''2017-10-14''),(6,''2017-08-15''),(7,''2017-07-02''),(8,''2017-12-12'') | |
begin transaction bench | |
begin try | |
set @i=1 | |
while @i<=@number | |
Begin | |
set @a=Abs(checksum(newid()) % 8) + 1 | |
select @first_name = title from @first_names where id=@a | |
set @b=Abs(checksum(newid()) % 8) + 1 | |
select @last_name = title from @last_names where id= @b | |
set @c=Abs(checksum(newid()) % 8) + 1 | |
select @date= title from @dates where id= @c | |
set @phone = 982530000000 + Abs(checksum(newid()) % 8999999) + 1111111 | |
set @active = (Abs(checksum(newid()) % 2) + 1)-1 | |
insert into tbl_5million(first_name,last_name,phone,active,[date])values | |
( | |
@first_name,@last_name,@phone,@active,@date | |
) | |
set @i=@i+1 | |
End | |
commit transaction bench | |
end try | |
begin catch | |
select @first_name,@a | |
select @last_name,@b | |
select @date,@c | |
rollback | |
end catch | |
END | |
' | |
declare @metasql nvarchar(MAX) | |
SET @metasql = (select use_script from #vars)+ | |
' | |
EXEC (''' + REPLACE(@statement, '''', '''''') + ''') | |
' | |
EXEC (@metasql) | |
END | |
--//-------------End Create Procedure--------------\\-- | |
DROP TABLE #vars; | |
GO | |
-- To Use just follow these | |
-- 1.Drag this file to your query editor or copy whole of this file into query editor and then run it. This will create a stored procedure in specified database | |
-- 2.Call the procedure like this: | |
-- 3.USE [HM_benchmark] //can be modified in line 4 | |
-- 4.exec dbo.generateRandomData 500000 // This is number of records would be created. Feel free to replace your own number. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To Use just follow these
1.Drag this file to your query editor or copy whole of this file into query editor and then run it. This will create a stored procedure in specified database
2.Call the procedure like this:
3.USE [HM_benchmark] -- Can be modified in line 4
4.exec dbo.generateRandomData 500000 -- This is number of records would be created. Feel free to replace your own number.