Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Time Bomb Coding


Time Bomb Coding

Author
Message
CraigIW
CraigIW
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 235
nicholasw (2/10/2010)
Instead of:

IF EXISTS(SELECT 1 FROM ......WHERE....)

Would the following be quicker still?

IF EXISTS(SELECT TOP 1 1 FROM ......WHERE....)


No, as the "top 1" is only done after the "select 1 from".
peter-757102
peter-757102
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 2549
Outstanding article...clear in meaning, no clutter or distractions, straight to the point.

It is also enlightening to see some of the beginner mistakes I made so many years ago when I tried to use the relational database as storage for a perfect object model and used only a few generic tables holding all the data. Your article correctly describes the problems it had when soon after it wend into production...shockingly quickly I might add, even on brutal hardware. That everything was developed on a memory constraint laptop did not help either ofcourse, always make sure to also test on similar hardware it will eventually be running on. A memory constraint laptop running 5 applications next to SQL server will not correctly separate bad code/models from good code/models. It is like trying to find out which athlete can run faster and should be send to the olympic games, with the constraint that they all have to tug 800kg of metal behind them to find it out...uphill.

As for the bitfield problem (and similar ones), filtered indexes (SQL 2008) can be a big help here.
Sriram.RM
Sriram.RM
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1599 Visits: 808
Excellent article... Especially the list!!!!

For better assistance in answering your questions
Click Here

Sriram.RM
Sriram.RM
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1599 Visits: 808
Excellent article... Especially the list!!!!

For better assistance in answering your questions
Click Here

peter-757102
peter-757102
SSC-Addicted
SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)SSC-Addicted (459 reputation)

Group: General Forum Members
Points: 459 Visits: 2549
CraigIW (2/10/2010)
nicholasw (2/10/2010)
Instead of:

IF EXISTS(SELECT 1 FROM ......WHERE....)

Would the following be quicker still?

IF EXISTS(SELECT TOP 1 1 FROM ......WHERE....)


No, as the "top 1" is only done after the "select 1 from".


Funny enaugh, it is not always that simple. I seen cases where a:

select top 1 max(id)

outperformed

select max(id)

many times over, and I suspect top sometimes acts as a undocumented hint. I got some testing to do on it still, and if it pans out it might also be a trick to control table variables better by using top to provide an estimate other then the constant 1 and thus get better query plans. It could also be just a side effect of the optimizer assigning costs to each operation and in rare cases come to a different plan due to a slightly higher ** estimate ** of the costs. In any case, it does not hurt to test both methods if you see a query perform worse then you anticipated.
CraigIW
CraigIW
SSC Journeyman
SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)SSC Journeyman (95 reputation)

Group: General Forum Members
Points: 95 Visits: 235
peter-757102 (2/10/2010)
CraigIW (2/10/2010)
nicholasw (2/10/2010)
Instead of:

IF EXISTS(SELECT 1 FROM ......WHERE....)

Would the following be quicker still?

IF EXISTS(SELECT TOP 1 1 FROM ......WHERE....)


No, as the "top 1" is only done after the "select 1 from".


Funny enaugh, it is not always that simple. I seen cases where a:

select top 1 max(id)

outperformed

select max(id)

many times over, and I suspect top sometimes acts as a undocumented hint. I got some testing to do on it still, and if it pans out it might also be a trick to control table variables bestter by using top to provide an estimate other then the constant 1 and thus get better query plans.


Crazy! Good to know that though.
Craig Benson
Craig Benson
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 359
Well done-- very useful!
Samuel Vella
Samuel Vella
Old Hand
Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)Old Hand (393 reputation)

Group: General Forum Members
Points: 393 Visits: 2141
Catch all Lookup tables are the bane of my life.
I've lost count of how many times I've had to write queries which join to the same lookup table 10 or more times to retrieve some basic data items.
As your article points out, it makes enforcing business logic very difficult, sometimes these data values can be mandatory but there is no straight forward way of enforcing that.

It also hides the true data structure so when someone new to the database is trying to mine the information schema tables to try and find certain data items they find themselves unable to locate key columns which have been instead hidden into a lookup!

Finally, the performance hit. All the extra processing which has to go on to flatten the data from the lookup tables. Storage is cheap, CPU cycles wastes energy!
David.Poole
David.Poole
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3698 Visits: 3121
If you look throught he source of the built in system stored procs you will see quite a few cases of IF(SELECT COUNT(*)...)>0

Try sp_helptext 'sp_helptext' for an example!

LinkedIn Profile

Newbie on www.simple-talk.com
Sriram.RM
Sriram.RM
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1599 Visits: 808
"Catch all Lookup tables are the bane of my life.
I've lost count of how many times I've had to write queries which join to the same lookup table 10 or more times to retrieve some basic data items."


Iv come across the same, This becomes worse if proper documentation is not present.

But having lookup tables has its own advantages, There are scenarios when u r dealing with a huge DB and if its not normalized atleast to the 2nd level u face loads of performance related problems especially with data retrieval.

For better assistance in answering your questions
Click Here

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search