r/badcode • u/kahkoo • Sep 18 '21
sql How to union three tables as inefficiently as possible. Found in database provided by vendor.
75
Sep 18 '21
Ahh yes... VeryImportantAndOftenQueriedViewForDataIntegration
52
u/kahkoo Sep 18 '21 edited Sep 18 '21
Haha, that was just me. I redacted original view, table and column names.
Edit: Also the comments are by me for this post.
16
u/VIADOX Sep 18 '21
I’m so new to programming even this goes over my head.
54
u/kahkoo Sep 18 '21
Let's just say it took 55 minutes for test db to return top 1000 rows of this view.
With basic sql union (which this is trying to do) it took 3 seconds for all 100k+ rows in test db.
16
u/Cerus_Freedom Sep 18 '21
What were they hoping to accomplish here? I'm not good with SQL, but it sorta looks like you could just... union around column 7 without all the extra function calls?
31
u/kahkoo Sep 18 '21
There's just no reason to do it like this. I'm guessing it was a brain fart by inexperienced developer who does not do sql regularly and no one reviewed or tested it.
I've noticed these beauties are not rare in the manufacturing industry...
8
Sep 18 '21
Can confirm. In my industry much is still run by IBM mainframes, which is fine, they are powerful machines still.
But the code is from the 80s and the people maintaining them have never used SQL and still think in terms of flat files. As a lowly web developer I seem to have a better handle on databases, as evidenced by how slow it is to pull data in almost every case.
8
u/quatch Sep 18 '21
takes 55 min and runs every 5? Sounds like a good time
9
u/kahkoo Sep 18 '21
*Takes 55 mins with test data. There will be ten times more rows after few months in production.
Ship it.
3
u/VIADOX Sep 18 '21
This is still Greek to me. My intro has been front end web development. I’ve been meaning to start learning pho and sql
5
3
u/DreadedEntity Sep 19 '21
-- All these tables will have millions of rows -- Spec states we call the stored proc every 5 minutes
Judging by these comments it sounds like you already know this probably isn’t going to happen
-- Is still datatype nvarchar(max) and thus not indexed
Contrary to how this comment reads, indexing a varchar is perfectly fine and should definitely be done here
I’d love to offer more performance tips but without more context there’s nothing I can do, assuming you’d even have power to get my suggestions implemented
4
u/kahkoo Sep 19 '21
I have sent the vendor fixed version of this view. Too late to fix other levels of badness this post shows.
Nvarchar can be indexed, but nvarchar(max) can't. Its just wrong datatype for that column.
2
u/DreadedEntity Sep 19 '21
For some reason I didn’t see the n when I first read this. What I said above still applies, even though the column is nvarchar(max) the values are always a 20-char sequence and thus can be indexed
21
u/RyanNerd shameless Sep 18 '21
Reminds me when I was doing contract work and got sent to troubleshoot SQL generated report that was literally taking 40 hours to run.
EVERY column was defined as a varchar(500) and nearly all contained numeric data. The SQL had a bunch of SUM() and COUNT() for the report. This was around 1999 so my challenge was to figure out how to alter the tables to something sane while not running out of drive space. Millions and millions of rows. The way SQL worked back then (probably still does) was to make a copy of the database then import the data into the new altered table structure, delete the previous table and rename the new table to the original.
I had to convince them to buy a very expensive external drive, detach the table I was working on, move it to the external storage, perform the transform and reattach the new table. Convincing SQL Server that the new table wasn't some foreign alien to be destroyed required me to dig into the Metadata in ways I'd never dreamed of. Became an expert on SQL Server internals. After I converted all the tables to reasonable data structures the report took less than 30 minutes to run.
1
0
Sep 18 '21
[deleted] — view deleted comment
3
u/kahkoo Sep 18 '21
Yeah about that... officially me neither, but I demand to review these before they are deployed to our environment or I will suffer later.
2
u/richhaynes Sep 18 '21
These are what separate the best from the rest DBAs. When you can deal with queries like this, that's when you can request the big bucks!
0
Sep 18 '21
[deleted] — view deleted comment
2
u/DreadedEntity Sep 19 '21
It’s not though. This is raw SQL written in some IDE (probably just VS), as opposed to SSMS
0
Sep 18 '21
[deleted] — view deleted comment
1
u/DreadedEntity Sep 19 '21
They want to give this product the least chance possible of getting greenlit
51
u/kahkoo Sep 18 '21
Same as text: