SQLRx® Tip of the Month
 
May 2015
______________________________________________________________________________
wrench

SQL Server Development:  Recently I had to do some R&D for a project and needed to generate a large table to test performance on. Since it is important to test queries on large data sets, I thought that I would post how I populated a few data types in my table. To test query results I decided that I needed some values in the table that were not random so I simply salted my random data with known data at irregular intervals that would allow for testing. I ended up with over 4 million records in a table with 45 columns. You can take the code below and can expand it to fit your needs. Just change the value for the @Loop variable to control how many records you want to insert into your table.

 

CREATE TABLE RandomLoad([NumberColumn] int,[BitColumn] bit,[VarcharColumn] varchar(20),[CharColumn] char(1),[DateColumn] date)

 

-- This will populate the RandomLoad table with random records

DECLARE @Loop INT, @Num1 INT, @Length INT

DECLARE @Varchar1 VARCHAR(20)

DECLARE @Varbin1 VARBINARY(128)

DECLARE @Char1 CHAR(2)

DECLARE @Bit1 BIT

DECLARE @Date1 DATE

 

SET @Loop = 0

 

WHILE @Loop < 1000 -- number of records to generate

BEGIN

   -- Generate Number

   SET @Num1 = ROUND(RAND() * 10000, 0)

 

   -- Generate Bit

              SET @Bit1 = CRYPT_GEN_RANDOM(1)%2

 

   -- Generate Varchar

   SET @Varchar1 = ''

              SET @Length = CAST(RAND() * 20 AS INT) -- Up to 20 characters long

              WHILE @Length <> 0

              BEGIN

                     SET @Varchar1 = @Varchar1 + CHAR(CAST(RAND() * 96 + 32 AS INT))

                     SET @Length = @Length - 1

              END

 

   -- Generate Char

              SET @Char1 = LEFT(newid(),1)

   -- Generate Date

   SET @Date1 = CAST(GETDATE() +(365 * 2 * RAND() - 365) AS DATE)

   INSERT INTO [RandomLoad] VALUES (@Num1,@Bit1,@Varchar1,@Char1,@Date1)

   SET @Loop = @Loop + 1

END

GO


 

SQLRx has worked hard to minimize the impact of monitoring SQL Servers to the point that we are able to monitor high transaction systems with little impact (1% load) on the target system. Contact us today to help you monitor your SQL servers!    
As always, don't forget to regularly monitor the 11 VitalSigns  used by SQLRx to debug performance bottlenecks.

If you have a SQL question you'd like us to answer, and possibly use for the next SQLRx Tip of the Month, email it to us!  If you missed any of our previous tips, you can view them here.

For assistance or more information on optimizing your SQL Server Environment, visit us at www.sqlrx.com or email Lori Brown at lorib@isi85.com.



 

Phone: 214.526.7680 x 113

Toll free:  800.85.SQLRx  (x113)

https://sqlrx.wordpress.com/

@SQLRx


 


 

Integrated Services, Inc.                                    
2008 winner logo small
Turning Data Into Profit


 

30 Years of Credibility & Trust


 

Join our email list          Check out our events          SQLRx Downloads