July 19, 2010 at 12:48 am
Its a general question regarding writing fatest performing tsql codes.
Whats better ?
To perform whole task using many small and simple queries
or
to perform task using big, compact and probably complex queries.
Please share your experience and views.
Thanks
Sanjay
July 19, 2010 at 1:28 am
This all depends on each individual situation. Many small updates will be slower than one mass update statement, but for a complex query, most of the performance impact is probably finding out what value should be assigned, rather than the actual update.
On the other hand, small queries may be easier to understand and maintain. They might take an extra 10% extra time to run, but that might be OK.
So, I'm not going to say one way or the other as to which is better. Performance can go either way, but, unless it's a speed-critical query, maintainability is important.
July 19, 2010 at 1:40 am
Sanjay-940444 (7/19/2010)
Whats better ?To perform whole task using many small and simple queries
or
to perform task using big, compact and probably complex queries.
It depends.
Seriously, there's no one single answer to that question. Sometimes doing everything in one query is faster, sometimes splitting things up is faster. Depends on too many things to give an always-right single answer.
If you're in a scenario where you're deciding between those, try both, see which is faster and by how much, then decide which way to go.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 19, 2010 at 1:41 am
I understand that upto a large extent it depends on scenerio on hand but sometimes task is so big and complicated that it becomes difficult to decide which approach to adopt specially when database is badly designed and unnormalised. (It is the case with me currently.)
If we start to write small queries it gives understandiblity and seems efficient untill someone experienced comes and tells u that it cud be done in a single query which takes little time!!
Otherwise writing big queries becomes unmanageable later on and leads to 'patch work'.
Anyway thanks for your views.
Looking for more insights into this matter.
Thanks
Sanjay
July 19, 2010 at 1:52 am
Sanjay-940444 (7/19/2010)
If we start to write small queries it gives understandiblity and seems efficient untill someone experienced comes and tells u that it cud be done in a single query which takes little time!!Otherwise writing big queries becomes unmanageable later on and leads to 'patch work'.
Then do it in small queries and, if performance is unacceptably bad, evaluate rewriting as larger queries. If performance is acceptable, there's no reason to worry about rewriting.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 19, 2010 at 2:06 am
Thanks Gail
strating with simple and smaller queries seems to be right approach.
July 19, 2010 at 2:17 am
Sanjay-940444 (7/19/2010)
I understand that upto a large extent it depends on scenerio on hand but sometimes task is so big and complicated that it becomes difficult to decide which approach to adopt specially when database is badly designed and unnormalised. (It is the case with me currently.)If we start to write small queries it gives understandiblity and seems efficient untill someone experienced comes and tells u that it cud be done in a single query which takes little time!!
Otherwise writing big queries becomes unmanageable later on and leads to 'patch work'.
Sanjay,
My personal preference would be to go with the smaller, more understandable, queries unless or until performance becomes unacceptable. The performance advantage of any complex monolithic query replacement would have to be significant to offset the disadvantages of that approach.
Aside from the maintainability issues with a large and complex query (which should not be understated!) large, complex queries are vulnerable to poor plan stability. By that, I mean that small changes to statistics and other information used by the query optimiser, might cause a sub-optimal plan to be produced. A single, large query is much harder for the optimiser because there are so many plan alternatives. These sorts of issues can be transient and very tricky to debug.
Since your life is already tough enough with the poor design, you probably don't want to add to your potential problems by introducing large, complex, and unstable queries - no matter how 'clever' they might be. If you do end up doing this for performance reasons, comment the code extensively, make sure the other members of your team understand what has been done and why, and do what you can to make life easy on the query optimiser.
Paul
July 19, 2010 at 3:47 am
Thanks Paul!
More the query is complex more possible plans are possible and SQL Sever is not going to pick up best one necessarily.
Best design is one which is simple.
July 19, 2010 at 4:00 am
Sanjay-940444 (7/19/2010)
More the query is complex more possible plans are possible and SQL Sever is not going to pick up best one necessarily. Best design is one which is simple.
You got it.
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply