You are not logged in.

#1 2011-02-01 21:55:08

delacruz
Member
From: /home/houston
Registered: 2007-12-09
Posts: 102

ERD & SQL: The most horrbile !

I am the lucky individual who has the work with the following sad

One SQL query

hp2Iw.jpg

Last edited by delacruz (2011-02-01 22:08:43)

Offline

#2 2011-02-01 22:01:40

schuay
Package Maintainer (PM)
From: Austria
Registered: 2008-08-19
Posts: 564

Re: ERD & SQL: The most horrbile !

I don't want to sound condescending, but that's a rather small database and not a particularly large query tongue
Also, are you sure this isn't sensitive / confidential information (even if names are blanked out)?

Last edited by schuay (2011-02-01 22:04:14)

Offline

#3 2011-02-01 22:10:21

delacruz
Member
From: /home/houston
Registered: 2007-12-09
Posts: 102

Re: ERD & SQL: The most horrbile !

schuay wrote:

I don't want to sound condescending, but that's a rather small database and not a particularly large query tongue
Also, are you sure this isn't sensitive / confidential information (even if names are blanked out)?

You are right i took off the query. 

There is 25 other pages that goes with the above diagram.  The problem with the query is that it takes like 50 seconds to shoot out like 30 tuples.  It repeats a lot of data over and over.

Offline

#4 2011-02-01 22:45:23

schuay
Package Maintainer (PM)
From: Austria
Registered: 2008-08-19
Posts: 564

Re: ERD & SQL: The most horrbile !

SQL does have the tendency to get messy (at least visually) as the codebase grows. I'd much rather write SQL than use SSIS though smile

Often, there's also huge potential for optimization - correct table indices, storing subresults in indexed temporary tables, join hints if necessary. 50 seconds does seem a bit much, unless your server is running on a netbook or the table has > 50 million rows..

Looking at the query you posted, it seems a bit weird to use both DISTINCT while doing a GROUP BY over the same columns (DISTINCT is slower than GROUP BY). Judging from the column names, the ones ending in _num look like ints. Why are you doing LTRIM(RTRIM()) on those? Why use any functions / string concatenation at all in the GROUP BY clause (bypassing any indices you might have) instead of grouping by the actual columns? The subquery (again using DISTINCT) on vCurrentTimesheet also look unnecessary to me.

Have you checked execution plans? These should display precisely where performance bottlenecks are located.

Of course, if the views that are being used in these queries are already slow, optimizing this part won't help alot. And yes, it certainly looks like many (if not all) of these statements could be merged into a single one smile

Last edited by schuay (2011-02-01 23:06:15)

Offline

#5 2011-02-01 23:00:38

delacruz
Member
From: /home/houston
Registered: 2007-12-09
Posts: 102

Re: ERD & SQL: The most horrbile !

schuay wrote:

SQL does have the tendency to get messy (at least visually) as the codebase grows. I'd much rather write SQL than use SSIS though smile

Often, there's also huge potential for optimization - correct table indices, storing subresults in indexed temporary tables, join hints if necessary..

50 seconds does seem a bit much, unless your server is running on a netbook or the table has > 50 million rows..

The database is ten years old and is not optimized at ALL!!! To make matters worst it was made by a chemical engineer.  Hey if you can develope software and not be a CS then go for it.  But if you are making garbage, I just dont know what the original "developer" was thinking.

Offline

#6 2011-02-01 23:46:43

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: ERD & SQL: The most horrbile !

I hate to be a one-upper, but until last year one of import scripts ran an SQL query that was about 250 lines long, had about 20 CASE WHEN statements to work around crappy foreign data, and was written as the UNION of two giant SELECT clauses that JOINed on about 20 tables each.  I kept a copy of the query to remind myself to never let my code look like that.  Our database's schema diagram, printed in about the same font size as yours, covers my boss's wall.  Oh, and our codebase has nearly a million lines of C# and PHP.

So don't complain, someone's always got it worse than you smile.  I know someone must have it worse than me.

Last edited by tavianator (2011-02-01 23:47:10)

Offline

#7 2011-02-02 00:18:19

delacruz
Member
From: /home/houston
Registered: 2007-12-09
Posts: 102

Re: ERD & SQL: The most horrbile !

tavianator wrote:

million lines of C# and PHP.

This is very rare.  Most programs are no where near one million lines.  In fact software engineering technics break down around 2 and above million lines.

Offline

#8 2011-02-02 04:42:47

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: ERD & SQL: The most horrbile !

delacruz wrote:
tavianator wrote:

million lines of C# and PHP.

This is very rare.  Most programs are no where near one million lines.  In fact software engineering technics break down around 2 and above million lines.

Tell that to the kernel devs smile

However, I should've said "nearly half a million," because I just totalled the line counts for all source files on the whole project and it's 453,302 SLOC.

Offline

#9 2011-02-02 05:26:29

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,965
Website

Re: ERD & SQL: The most horrbile !

Pah, that's nothing.

I once had to work with a bajillion lines of COBOL and obfuscated Perl written by a half-blind street vendor. The code retrieved data from a database using a query string that was longer than the human genome, and the schema diagram, printed in about the same font size as yours, depleted the rain forest and covered the entire Sahara.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB