- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The only situation I can think of where this would occur would be contention on the database schema stability lock, or blocking on tempdb's data files being extended. The schema stability lock would apply whether using tempdb or your real database. The downside of using your real database is that you get junk in the transaction log, which increases the size of the log backup and reduces the number of real transactions that can be recorded.
The default for tempdb's size can be altered, I believe. This KB article suggests that the size used for tempdb at startup is the size last configured. To configure, it appears you use 'ALTER DATABASE tempdb MODIFY FILE ...'
Admin
"we can't possibly risk altering the code for this procedure; the risk of failure is just too high."
Translation:
"my code is crappy and I don't know what I'm doing."
Besides, if it's erroring out and not cleaning up its temp tables, it's already failing!
Admin
In a large DB, the order you insert records can affect where they get written on disk. We do stuff like this so that we can align storage with expected future queries. We're dealing with a little over 1TB...I doubt their sales commissions data is nearly as big.
Of course, nobody in their right mind would allow a developer to submit code that used "order by 2". Besides being unecessarily confusing syntax (some people don't know what it is, even if you do you still have to look back to the FROM and JOIN clauses to figure out what column that actually is, it creates maintenance headaches when somebody comes along and modifies the query in the future.
Admin
BOFH style, but effective.
Maybe you _should_ void your support :D At my current workplace I am trying to rely as less as possible on the 3rd party support (even if that means more hours spent in my cubicle), not to mention that many in-house improvements were fed back at us (for some real money) as "upgrades" and/or "patches".
Mike, you're good :) I have a similar problem with "tempdb is full" because of a poorly written SP (one of my coleagues wrote it and I was too lazy to rewrite it) but now I have a serious incentive to look at it :D After all, I wouldn't like to be responsible for another WTF...
The KB article is relevant, but how many monkeys read KBs? Not to mention that sharing knowledge implies _having_ it in the first place.
Peace
captcha = java (oh, yeah)
Admin
Hi guys, this is Mike Rod
And yes, everything posted here was real, it was extemely painful to debug this code. One thing I didn't tell Alex is the (incredibly high) number of cursors: basically each Inner Join/Left Outer Join required:
- One select into a "temp" table for the left side of the join
- One select into a "temp" table for the right side of the join
- One, maybe two cursor(s)
- A third "temp" table to hold the result
A simple UNION statement also required three temp tables in this code (this is real).
They asked me to find a bug (which happened to be a variable inside a cursor that wasn't reset on every loop) but I found the whole 5000+ LOC procedure a gargantual, hellish bug. You can only imagine the pain and frustration to even understand a part of this code, I was like:
(after undertanding 50 lines of code)
"Gaaa! this is only a inner join implemented with a cursor"
(after reading the union with three "temp" tables)
"Holy cow! I am getting nowhere... I need a smoke"
I finished all my smokes before lunch =(
Mike Rod
Admin
That's exactly what I dealt with at my last job. I inherited a database system designed by a cobol developer. All the logic in the system was in the stored procedures which he wrote, and the design of that system was the first time he'd ever done anything with SQL. I won't even get started on the table layout. I get nauseous when I think about it.
Admin
Oh, no no no," the developer replied, "we can't possibly risk altering the code for this procedure; the risk of failure is just too high."
The thing that bothers me, is the fact that it appears the developer no longer know how it works, and is afraid to make changes. If an organization no longer has anyone that can understand the problem, much less the solution.... it's screwed.
Admin
Imagine If we are able to built inmortal robots with wrong Rules Of Robotic, and no one remenber how to recode these. We will be really screwed. Except all Inmortal Robots will die in 2038 :/
Note: I will be away with images of female bot-sex
Admin
Oh, sabotage. Great idea.
Admin
A 5700 line stored procedure is pretty ugly in and of itself. Having stored procedures in production that can error off with incomplete results and not, at least, produce an alert to the operations staff is a serious problem.
Sometimes you can't just add # to the begining of table names. Temp tables have limited visibility. They would not be visible witnin any stored procedures that the big one called, unless you used the ## form.
Admin
Oh, no. Let's not start that flame war again: http://thedailywtf.com/forums/permalink/29028/28969/ShowThread.aspx#28969
Admin
Temp tables (at least in MS-SQL 2000/5) can be accessed by any proc called by the proc that created them. I have a couple of systems that use that functionality to make the code more modular. They don't need the ##<tablename> format for that, just #<tablename>.
I only use temporary tables in these situations where I have to pass a table of data from one proc to another, since you can't have a table variable as an input parameter, but you can pass a temp table's control from proc to proc, so long as one proc calls the other (that keeps it in the same scope). In a situation like the one outlined in this WTF, if I had to use "temp tables" at all, I'd use table variables, since it's all one giant proc. That's assuming I couldn't do the whole thing with a few set-based statements that would take the place of the whole proc (which is quite likely the real solution).
Admin
Developer: "Tell me again, why are you moving me to Kazakstan?"
Boss: "Don't worry, it's only a temp. position."
Admin
What I get from PostgreSQL 8.1 is:
ERROR: column "<tablename>.<columnname>" must appear in the GROUP BY clause or be used in an aggregate function
I have worked with a lot of database products and have never seen one which would return the desired results if you drop GROUP BY as previously suggested. Some would return huge result sets which would not be very useful; most have not taken to generating an error.
The most common SQL WTF I see is programmers who don't get set logic. Many times I have replaced a set of statements which laboriously wended through many tables with a single statement which joined the tables. It generally runs several times as fast as a single statement, partly because of the reduced overhead in parsing and planning, and partly because an application programmer can't generally use (for example) bitmaps and hashes the same way a database engine can.
Someone asked whether the optimizer wouldn't do the same thing the application programmer did here. Hard to say from available information, but the odds are extremely high that if the optimizer was given a description of the RESULT NEEDED using set logic, it would have come up with a better way to navigate the data to get there.
Admin
Er, that should read "most have now taken"...
Admin
That's NOT the same. You are now inserting the overall MAX instead of the per item max. (I am not a DBA, but that's what I'm seeing)
Admin
One of my close friends never took any database classes in college, but he learned set theory. He became a competent database programmer after about 2-3 weeks of working with SQL. The system architect at my last job had been working with SQL for 3 years when I started. I had to rewrite almost everything he wrote. One of the first things he said to me was, "Forget everything you learned in college. It's completely useless."
Maybe if he would have remembered how to measure the order of magnitude of an algorithm he would have realized why his queries that used cursors to iterate over records in a table were so much slower then ones that did updates in a single SQL statement.
Admin
Well, of course you could use views and proper queries and stuff, but the risk of failure would be too high.
Admin
Actually, the "ORDER BY #" syntax (where # is equal to a counting number) is frequently needed when doing a UNION query. At times, the column name may be different across two tables, so ordering by a column name is invalid.
For example:
You might get away with column aliases, but not always. Sometimes, depending on the database, the syntax above is the only one that works.
-- Kluge Doctor
Admin
In an ANSI compliant database product, you use the alias of the column in the first SELECT statement of the UNION, or the unqualified name if there is no alias. In your example, and alias would be required, but only on the first SELECT. I haven't worked with a database that fails to comply with this standard in a while, although I'm sure they're out there. (Of course, the ordinal column number is also valid ANSI syntax, it's just a bit more fragile in the face of modifications to the query.) Most products also extend the standard to allow the ORDER BY clause to reference any column or expression which would be valid in the context of the result column declarations, but that is non-standard and subject to problems in a UNION.
Admin
And I thought this would be about TempDB in MSSQL 2005 growing 50GB large on occasion...
Oh noes! Not the nested stored procedure nightmare! Been there, done that. It's a real PITA to maintain something like this. Repeat: TSQL is not a proper programming language!
Admin
<40
Admin
Where I work, this would be followed by a DROP DBA.
Admin
i've been reading these in reverse order, so i can't help but think of a later story where some idiot wanted to clean up temp files by deleting "temp"...this would have also deleted the TEMPerature warnings, and important financial TEMPlate files...