Is there a more elegant/faster way to write the code below? Currently taking about 45seconds.
query.sql is 200,000 lines long and has SQL in it exactly like this on each line:
SELECT N'+dave' AS [AccountName], N'20005' AS [EmployeeID], N'-6' AS [PlatformID] UNION ALL
I found that by chunking into blocks of 1000 that things were much quicker than waiting until the end and using WriteAllText (which took about 20 minutes to run)
static void Main(string[] args)
{
var s = new Stopwatch();
s.Start();
string textToWrite = "";
string[] lines = File.ReadAllLines(@"e:\temp\query.sql");
int i = 0;
foreach (var line in lines)
{
var bits = line.Split('\'');
var value1 = bits[1];
var value2 = bits[3];
var value3 = bits[5];
var message = "INSERT [PreStaging].[Import_AccountEmployeeMapping] ([AccountName], [EmployeeID], [PlatformID]) VALUES (N" +
"'" + value1 + "', "
+ value2 + ", "
+ value3 + ")";
textToWrite += message + Environment.NewLine;
if (i % 1000 == 0)
{
Console.WriteLine(i + " " + DateTime.Now.ToLongTimeString());
File.AppendAllText(@"e:\temp\query2.sql", textToWrite);
textToWrite = "";
}
i++;
}
//File.WriteAllText(@"e:\temp\query2.sql", textToWrite);
File.AppendAllText(@"e:\temp\query2.sql", textToWrite);
s.Stop();
TimeSpan ts = s.Elapsed;
Console.WriteLine("Timespan: {0}m", ts.TotalMinutes);
Console.WriteLine("Total records: " + i);
Console.ReadLine();
}
Edit: StringBuilder Solution (1000ms):
static void Main2(string[] args)
{
var s = new Stopwatch();
s.Start();
var textToWrite = new StringBuilder();
string[] lines = File.ReadAllLines(@"e:\temp\query.sql");
int i = 0;
foreach (var line in lines)
{
var bits = line.Split('\'');
var value1 = bits[1];
var value2 = bits[3];
var value3 = bits[5];
var message = "INSERT [PreStaging].[Import_AccountEmployeeMapping] ([AccountName], [EmployeeID], [PlatformID]) VALUES (N" +
"'" + value1 + "', "
+ value2 + ", "
+ value3 + ")"
+ Environment.NewLine;
textToWrite.Append(message);
// Buffering
if (i % 1000 == 0)
{
Console.WriteLine(i + " " + DateTime.Now.ToLongTimeString());
File.AppendAllText(@"e:\temp\query2.sql", textToWrite.ToString());
textToWrite = new StringBuilder();
}
i++;
}
File.AppendAllText(@"e:\temp\query2.sql", textToWrite.ToString());
s.Stop();
TimeSpan ts = s.Elapsed;
Console.WriteLine("Timespan: {0}ms", ts.TotalMilliseconds);
Console.WriteLine("Total records: " + i);
Console.ReadLine();
}
Edit: StreamWriter solution (450ms)
static void Main(string[] args)
{
var s = new Stopwatch();
s.Start();
string[] lines = File.ReadAllLines(@"e:\temp\query.sql");
int i = 0;
using (StreamWriter writer = File.AppendText(@"e:\temp\query2.sql"))
{
foreach (var line in lines)
{
var bits = line.Split('\'');
var value1 = bits[1];
var value2 = bits[3];
var value3 = bits[5];
writer.WriteLine("INSERT [PreStaging].[Import_AccountEmployeeMapping] ([AccountName], [EmployeeID], [PlatformID]) VALUES (N'{0}', {1}, {2})",
value1, value2, value3);
i++;
}
}
s.Stop();
TimeSpan ts = s.Elapsed;
Console.WriteLine("Timespan: {0}ms", ts.TotalMilliseconds);
Console.WriteLine("Total records: " + i);
Console.ReadLine();
}