<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www2.sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Adam Machanic : software development</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/software+development/default.aspx</link><description>Tags: software development</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Anti-Patterns and Malpractices, Volume 1: Tumbling Data</title><link>http://www2.sqlblog.com/blogs/adam_machanic/archive/2007/12/15/anti-patterns-and-malpractices-volume-1-tumbling-data.aspx</link><pubDate>Sat, 15 Dec 2007 05:10:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:3982</guid><dc:creator>Adam Machanic</dc:creator><slash:comments>17</slash:comments><comments>http://www2.sqlblog.com/blogs/adam_machanic/comments/3982.aspx</comments><wfw:commentRss>http://www2.sqlblog.com/blogs/adam_machanic/commentrss.aspx?PostID=3982</wfw:commentRss><description>&lt;blockquote&gt;
&lt;p&gt;&lt;i&gt;"Lonely but free I'll be found&lt;br&gt;
Drifting along with the tumbling tumbleweeds"&lt;/i&gt;&lt;/p&gt;
&lt;p&gt;&lt;i&gt;&amp;nbsp;- Supremes, "Tumbling Tumble Weeds"&lt;/i&gt;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Welcome to the first installment of what I hope will be a regular feature on this blog, Anti-Patterns and Malpractices. As a consultant, I get the honor of seeing a lot of different systems, with a lot of different code. Some of it is good, and some of it -- well -- I'll be featuring that which is not so good here. No names will be named, and code will be changed to protect the not-so-innocent; my goal is not to call out or embarrass anyone, but rather to expose those misguided patterns and practices which inevitably lead to problems (and a subsequent call to a consultant; perhaps if I post enough of these I'll have fewer less-than-appealing encounters in my work!)&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The topic du jour is the Tumbling Data Anti-Pattern, a name coined by my friend Scott Diehl. Much like the tumbleweed lazily blowing around in the dust, data which exhibits this pattern is slowly and painstakingly moved from place to place, gaining little value along the way.&lt;/p&gt;
&lt;p&gt;So what exactly typifies this particular anti-pattern? Consider the following block of T-SQL, designed to count all married employees in the AdventureWorks HumanResources.Employee table and bucket them into age ranges of 20-35 and 36-50, grouped by gender. Employees older than 50 should be disregarded:&lt;/p&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;--Find all married employees&lt;br&gt;SELECT *&lt;br&gt;INTO #MarriedEmployees&lt;br&gt;FROM HumanResources.Employee&lt;br&gt;WHERE &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MaritalStatus = 'M'&lt;br&gt;&lt;br&gt;/*&lt;br&gt;select * from #marriedemployees where employeeid = 20&lt;br&gt;*/&lt;br&gt;&lt;br&gt;--Find employees between 20 and 35&lt;br&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId&lt;br&gt;INTO #MarriedEmployees_20_35&lt;br&gt;FROM #MarriedEmployees&lt;br&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATEDIFF(year, birthdate, getdate()) BETWEEN 20 AND 35&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;--Find employees between 36 and 50&lt;br&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId&lt;br&gt;INTO #MarriedEmployees_36_50&lt;br&gt;FROM #MarriedEmployees&lt;br&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATEDIFF(year, birthdate, getdate()) BETWEEN 36 AND 50&lt;br&gt;&lt;br&gt;&lt;br&gt;--Remove the employees older than 50&lt;br&gt;DELETE&lt;br&gt;FROM #MarriedEmployees&lt;br&gt;WHERE&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId NOT IN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT EmployeeId&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM #MarriedEmployees_20_35&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; EmployeeId NOT IN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT EmployeeId&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM #MarriedEmployees_36_50&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; )&lt;br&gt;&lt;br&gt;--Count the remaining employees&lt;br&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Gender,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; COUNT(*) AS theCount&lt;br&gt;INTO #Employee_Gender_Count_20_35&lt;br&gt;FROM #MarriedEmployees e&lt;br&gt;JOIN #MarriedEmployees_20_35 m ON e.EmployeeId = m.EmployeeId&lt;br&gt;GROUP BY&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Gender&lt;br&gt;&lt;br&gt;--select * from #Employee_Gender_Count_20_35&lt;br&gt;&lt;br&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Gender,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; COUNT(*) AS theCount&lt;br&gt;INTO #Employee_Gender_Count_36_50&lt;br&gt;FROM #MarriedEmployees e&lt;br&gt;JOIN #MarriedEmployees_36_50 m ON e.EmployeeId = m.EmployeeId&lt;br&gt;GROUP BY&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Gender&lt;br&gt;&lt;br&gt;--Get the final answer&lt;br&gt;SELECT &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a.Gender,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a.theCount AS [20 to 35],&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; b.theCount AS [36 to 50]&lt;br&gt;FROM #Employee_Gender_Count_20_35 a&lt;br&gt;JOIN #Employee_Gender_Count_36_50 b ON b.Gender = a.Gender&lt;br&gt;&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;br&gt;&lt;/p&gt;&lt;p&gt;This kind of code tells us several things about the person who wrote it. Rather than thinking upfront and designing a complete solution or at least a game plan before typing, this person appears to have thought through the problem at hand in a step-by-step manner, coding along the way. A bit of debugging was done along the way, but the real goal was to spit out an answer as quickly as possible (or so it seemed at the time). No attempt was made to go back and fix the extraneous code or do any cleanup; why bother, when we already have an answer?&lt;/p&gt;&lt;p&gt;It's important to mention that this is a simple example. I generally see this anti-pattern exploited when developers are tasked with producing large, complex reports against data sources that aren't quite as well-designed as they could be. In an attempt to preserve sanity, the developer codes each tiny data transformation in a separate statement, slowly morphing the data into the final form he wishes to output. The resultant scripts are often thousands of lines long and take hours to run, during which time the server crawls (and throughout the office you can hear people muttering "the server sure is slow today"). &lt;/p&gt;&lt;p&gt;The solution to this problem is simple, of course, and the best software engineers do it automatically: Before writing a line of code sit back for just a moment and consider your end goal. Do you need to work in steps, or will a single query suffice? Can various join conditions and predicates be merged? Perhaps a Google search is a good idea; what is the best way to produce a histogram without a temp table?&lt;/p&gt;&lt;p&gt;The hurried atmosphere of many companies leads to a "get it done right now--even if it's far from perfect" attitude that ends up wasting a lot more time than it saves. The above example code block took me around 10 minutes to put together. A single-query version took me under two minutes to code. It is less than a third of the length, runs approximately 500 times faster, and uses 0.4% of the resources. All because I spent a couple of moments reflecting on where I was going before I took the first step.&lt;/p&gt;&lt;p&gt;If you find yourself exploiting this anti-pattern, step back and question whether this code will have a life beyond the current query window. If it will ever be checked into source control, it's probably a good idea to go back and do some cleanup. &lt;/p&gt;&lt;p&gt;If you find yourself tasked with maintaining code that looks like what I've posted, my suggestion is to simply re-write it from scratch. I was recently faced with a script containing over 2000 lines of this kind of thing, and I spent almost two days slowly working my way through the mess trying to make sense of it. On the evening of the second day, after talking with some of the shareholders, I realized that it was actually a simple problem to solve. One hour later and I had a new, totally functional solution -- a couple of hundred lines long, and several orders of magnitude faster. Sometimes it's best not to wade through a muddy puddle, when you can simply hop right over.&lt;/p&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://www2.sqlblog.com/aggbug.aspx?PostID=3982" width="1" height="1"&gt;</description><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/Performance/default.aspx">Performance</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/malpractices/default.aspx">malpractices</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/software+development/default.aspx">software development</category><category domain="http://www2.sqlblog.com/blogs/adam_machanic/archive/tags/anti-patterns/default.aspx">anti-patterns</category></item></channel></rss>