Created
August 25, 2023 19:52
-
-
Save cheenamalhotra/40aa9c9d1b3fd452bd6bf2da2779cf88 to your computer and use it in GitHub Desktop.
Setup LargeColumnDataTable for testing
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
using System; | |
using Microsoft.Data.SqlClient; | |
namespace TestApp | |
{ | |
internal class SetupLargeColumnDataTable | |
{ | |
public static void Main() | |
{ | |
using SqlConnection sqlConnection = new("<conn_string_here>"); | |
sqlConnection.Open(); | |
string tableName = "LargeColumnDataTable"; | |
int cols = 500; | |
int rows = 10_000; // increase as needed - takes very long to insert. | |
// Create Large table | |
using SqlCommand cmd = sqlConnection.CreateCommand(); | |
cmd.CommandText = @$"create table {tableName}("; | |
for (int i = 1; i < cols; i += 2) | |
{ | |
cmd.CommandText += $"c{i} int,"; | |
cmd.CommandText += $"c{i + 1} VARCHAR(500),"; | |
} | |
cmd.CommandText += $"c{cols + 1} int)"; | |
cmd.ExecuteNonQuery(); | |
// Generate Insert command | |
string command = @$"Insert into {tableName} values ("; | |
for (int i = 1; i < cols + 1; i++) | |
{ | |
command += $"@p{i},"; | |
} | |
command += $"@p{cols + 1})"; | |
string longString = GenerateRandomLongString(100); | |
// Insert 500k rows of data | |
using SqlCommand sqlCommand = sqlConnection.CreateCommand(); | |
sqlCommand.CommandText = command; | |
for (int i = 1; i < cols; i += 2) | |
{ | |
sqlCommand.Parameters.Add(new SqlParameter($"p{i}", 1)); | |
sqlCommand.Parameters.Add(new SqlParameter($"p{i + 1}", longString)); | |
} | |
sqlCommand.Parameters.Add(new SqlParameter($"p{cols + 1}", 1)); | |
for (int i = 0; i < rows; i++) | |
{ | |
sqlCommand.ExecuteNonQuery(); | |
if (i % 100 == 0) | |
{ | |
Console.WriteLine($"Processed {i} rows."); | |
} | |
} | |
Console.WriteLine("Insert completed."); | |
} | |
private static string GenerateRandomLongString(int desiredLength) | |
{ | |
string longString = @"abcdefghijklmnopqrstuvwxyz#abcdefghijklmnopqrstuvwxyz#abcdefghijklmnopqrstuvwxyz#abcdefghijklmnopqrstuvwxyz#abcdefghijklmnopqrstuvwxyz#"; | |
while (true) | |
{ | |
longString += longString; | |
if (longString.Length > desiredLength) | |
{ | |
break; | |
} | |
} | |
return longString; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment