I am in the habit of using a stringbuilder to build up an Sql statement to execute, as this allows me to keep the statement formatted and readable in the code. However it can produce an inefficient statement with lots of extra whitespace:
sb.AppendLine (@"SELECT DISTINCT *");
sb.AppendLine (@" FROM ( SELECT col1");
sb.AppendLine (@" , col2");
sb.AppendLine (@" , col3 ");
sb.AppendLine (@" , col4");
sb.AppendLine (@" FROM (SELECT *");
sb.AppendLine (@" FROM TABLE )";
sb.AppendLine (@" WHERE col5= col1 ");
sb.AppendLine (@" AND col6 = col2 ");
sb.AppendLine (@" GROUP BY col1");
sb.AppendLine (@" , col2");
sb.AppendLine (@" ORDER BY col3");
this sql is an example of the issue and may not even be correct.
Should I strip the whitespace from this before I pass it to to a command object to be executed? Or will the command do this itself, or is this just a micro optimization which is not worth thinking about?
This is how I declare my SQL queries in the code:
Here are the pros:
No cons so far 🙂
Update
Oh, and, yes, I think worrying about extra white spaces is over-optimization.