CREATE VIEW dbo.VeryImportantAndOftenQueriedViewForDataIntegration
AS
SELECT
ISNULL(t1.rowid, t2.rowid, t3.rowid) column1,
ISNULL(t1.column2, t2.column2, t3.column2) column2,
ISNULL(t1.column3, t2.column3, t3.column3) column3,
ISNULL(t1.column4, t2.column4, t3.column4) column4,
ISNULL(t1.column5, t2.column5, t3.column5) column5,
ISNULL(t1.column6, t2.column6, t3.column6) column6,
ISNULL(t1.column7, t2.column7, t3.column7) column7,
-- Column7 is the actual logical Id generated by another system
-- Always less than 20 chars long and always numeric
-- Is still datatype nvarchar(max) and thus not indexed
-- All joins in database refer to this column
-- Is unique across all three tables
ISNULL(t1.errorcode, t3.errorcode) errorcode,
ISNULL(t1.errorinfo, t3.errorinfo) errorinfo,
ISNULL(t1.column10, t2.column10, t3.column10) column10,
ISNULL(t1.column11, t2.column11, t3.column11) column11,
ISNULL(t1.column12, t2.column12, t3.column12) column12,
ISNULL(t1.column13, t2.column13, t3.column13) column13,
ISNULL(t1.TransferStatus, t2.TransferStatus, t3.TransferStatus) TransferStatus
-- int, 1 if already received by us, set by stored proc
FROM dbo.table1 t1
FULL OUTER JOIN dbo.table2 t2 ON t1.column7 = t2.column7
FULL OUTER JOIN dbo.table3 t3 ON t2.column7 = t3.column7
-- All these tables will have millions of rows
-- Spec states we receive the data with stored proc
-- The stored proc calls this view and after receiving all rows filters out rows where TransferStatus <> 1
-- More than 90% of rows will have TransferStatus 1 at any time
-- Spec states we call the stored proc every 5 minutes
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?
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...
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.
-- 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
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
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.
51
u/kahkoo Sep 18 '21
Same as text:
75
u/[deleted] Sep 18 '21
Ahh yes... VeryImportantAndOftenQueriedViewForDataIntegration
16
u/VIADOX Sep 18 '21
I’m so new to programming even this goes over my head.
3
u/DreadedEntity Sep 19 '21
Judging by these comments it sounds like you already know this probably isn’t going to happen
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
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
u/Own_Wolverine4773 Sep 18 '21
Lol
0
u/[deleted] Sep 18 '21
[deleted] — view deleted comment
0
u/[deleted] Sep 18 '21
[deleted] — view deleted comment
0
u/[deleted] Sep 18 '21
[deleted] — view deleted comment