- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
How about cases where you need to run a bunch of stored procedures. The names of the procedures are not known at compile time. Furthermore, the user can decide to make certain procedures active and others inactive.
Do you have a better way to handle this type of situation that does not involve dynamic SQL?
Admin
I'm scared of clowns. And spiders.
Admin
But they're fun to watch and laugh at.
Admin
In Oracle, using non-dynamic SQL provides two advantages:
a) in case of stored procedures or preprocessors: a lot of checks during compile time, catches many typos (table names, column names, etc.)
b) the parsed statement is cached; this reduces the overhead for the execution of the statement (this is especially important if the statement accesses relatively small tables); dynamic sql may flood the cache of the pre-parsed statements (espcially if constants are used in the statement instead of bind variables)
For that reasons, the (undeniable) advantages of dynamic sql should always be weighted against the advantages of static sql.
Admin
Dynamic SQL should only be used as a last resort, even then I would try and find another way, horrible, almost as bad as cursor's /shudder
Admin
I always use something like this:
SELECT row_id FROM Table WHERE @Parameter IS NULL OR @Parameter = Column
and use the WITH RECOMPILE option. I'm eager to find out why dynamic SQL would be a better option.
Admin
I always use something like this:
SELECT row_id FROM Table WHERE @Parameter IS NULL OR @Parameter = Column
and use the WITH RECOMPILE option. I'm eager to find out why dynamic SQL would be a better option.
Admin
Well... so much for trying to make this board software do what I want. WTF...
Admin
Oh, but don't say you didn't expect this.
Brings to mind the "exception handling" post though - the one that caught an exception only to throw it again...
Admin
A lot more goes into it than just number of rows. The most important element is statistics as they relate to selectivity. And in this case you have an extremely selective index, so 255 rows is plenty for a seek:
---
use tempdb
go
create table xyz (searchit int not null primary key)
go
insert xyz (searchit)
select number
from master..spt_values
where type = 'p'
go
set statistics profile on
go
declare @mysearch int
set @mysearch = 100
select *
from xyz
WHERE
searchit = @mysearch
go
set statistics profile off
go
drop table xyz
go
---
Admin
Please see some of my earlier responses where I posted code fragments. You'll see that the WHERE @Param IS NULL OR ... option results in a table scan. Proper use of dynamic SQL eliminates that problem.
Admin
Just a FYI for those of us using Oracle. The WHERE @Param IS NULL OR ... option does not perform a full table scan.
It must be a mssql-ism.
Admin
Goodie for you, here's your gold start. OH WAIT - You weren't first! Here's your sign.
Admin
Exactly! Thank you. Dynamic SQL is definately a WTF.
Admin
Indeed. Yes. Dynamic SQL (shudder) is a WTF, all the time, regardless of the situation, ever, period, end of story. Never ever use Dynamic SQL (shudder)
It's definitely better to hand code all possible combinations of user input on a complex search form. Yes. All the time, just so we can avoid the horribleness of the Dynamic SQL (shudder).
</sarcasm, for those who missed it>
I'm guessing you purists are paid by the line then, and love huge amounts of code??
Admin
Variables are very bad. I only code using constants. That way the results are always predictable.
Admin
What? Haven't you heard of 'dynamic constants' yet?
They can save you a pile of code, but I guess they introduce some sort of performance impact.
Admin
I'm confused. No where in the queries does the expression "[Archive_Desc] = @Archive_Desc" appear.
If @Archive_Desc is NOT NULL (the else branch), then the where condition is "[Archive_Desc] IS NULL".
If you are going to re-write the code, at least make sure it does the same thing as the original queries.
(Why you want to get all of the rows where Archive_Desc IS NULL when the input parameter @Archive_Desc is NOT NULL is beyond me and also WTF IMHO.)
Admin
The quoting function is also 100% pure WTF.
Admin
Admin
Whooo Hoooo,
I'm Back. Be afraid, and all that.
Having being chastised for being "arrogent". . .
A database, after all is merely a collection of files.
If, one, (a person) can grok (understand in the ultimate sense of understanding) what significance fields in one or more collections of files might have to drawing meaningful conclusions, then that one (hopefully, the same person) might manipulate the FILES in a manner that might extract meaningful conclusions.
On the gripping hand. . . If all you know about your job is "joins", "outer joins". . .
Sadly, you are at the mercy of your software.
He who lives by the software, dies by the software.
Quite frankly, ALEX these posts are not WTF. They are WGAS (who gives a sh*t).
Bottom Feeders.
I wish I was Sincerly
Gene Canuck
Admin
Sorry, Gene.
I meant, "Sincerely"
Hopefully, your genes have been successful.
Simplifier.
Let's see if fortune is captha
Obviously, not quick enough. reference might do
Admin
Wow! Talk about broken software.
If anyone is interested in seeing a blog that works, I would refer you to
http://www.felbers.net/fa/
"Fanatical Apathy"
Apart from the best (most intelligent) comments to a blog (as opposed to this site) -- nyah nyah nyah, I'm wearing asbestos. . . It's a really well done site.
Comment by number, for example.
"Free is worth whatever it costs you"
badly paraphrased from Robert A. Heinlein.
Admin
This is obviously some kind of general wtf pattern as it reminds me of the guy who had to modify his über long if-elseif construct into switch-case construct and ended up having all of his his previous code in default block...
Admin
I've helped correct Java homework sometimes, and I came across something resembling this bit (reproduced as well as I remember):
Someone had evidently been told to "use loops, for goodness' sake!"
Admin
Google says:
<font class="p" color="#cc0000">Did you mean: </font>shagability
Admin
This is a classic WTF, though most people use switch/case instead of ifs.
Admin
Ohh! Structured Quine Language! Nifty!
Admin
Quinnum
Actually one of the last projects I did, was to take some very very unreadable dynamic SQL Server SQL code and structure it using some pretty standard logic. Yes the procedure was roughly 20 times longer when I had finished, however it was also infinitely more readable and roughly five times as quick.
In terms of coding, 99% of the time Dynamic SQL is the quick fix (as you have stated), however in terms of efficiency Dynamic SQL is the wrong choice if the logic can be coded in a more conventional manner, try it yourself, you may be surprised!!!
I am not a purist by nature, however saying that I look for the most efficient way for something to work, are you saying that you dont?
Admin
Google for
sargability site:microsoft.com
did not return anything.
Admin
I was going to bring up this point too, but you beat me to it. I was hoping that there was some kind of weird SQL logic going on here that I didn't understand as a non-SQL expert, but it doesn't look like it. It's just WTF.
Admin
Neither if/then nor switch/case is needed :
Admin
I'm pretty sure this guy works for me now.
Admin
Thank you,
Admin
http://blog.ragan.com/archives/sqlblog/2005/08/sargable.html
Admin
After doing some testing, it looks like doing the 'null is null' thing sometimes causes a full table scan. I tested with Oracle 10g, on a table with about 200k rows.
Most unique!
Admin
Wow. You've got to be kidding me.
Admin
If you are using sp_executesql with parameters on MS SQL, your statement is completely untrue.
Admin
Amen!
Dynamic SQL is a bad idea for performance and security reasons (it requires that all users have full access to the table). It should be avoided as much as possible. The logic could easily be cleaned up instead.
Admin
Can you show me an example of how or when properly used dynamic SQL (parameterized) would cause a performance problem compared with static SQL?
And as for security, that's no longer an issue in SQL Server 2005. Look up the EXECUTE AS option.
Admin
Funniest F***in' thing I've seen in a while.
Thank-You, Master!
Admin
right :-))
Admin
Dynamic SQL is not a WTF.
The WTF is the implementation.
Dynamic SQL without parameters is not a WTF. In fact there are cases where dynamic SQL without parameters OUTPERFORMS dynamic SQL with parameters.
See Ian Jose's post on condition-specific stored procedures for an example:
http://blogs.msdn.com/ianjo/archive/2005/11/10/491547.aspx
Admin
Such as?
His suggestion for mulitple stored procedures will fix some performance problems but could introduce a type of coupling between the application and the database; the application might have to know which stored procedure to call. If you're going to do this, encapsulate the fact that there are two stored procedures instead of one by using a shell stored procedure to make the decision. Or, see my post on this topic, in which I show how to deal with this problem using (surprise, surprise) dynamic SQL:
http://sqljunkies.com/WebLog/amachanic/articles/StoredProcedureCaching.aspx
And in response to your unanswered question in Ian Jose's weblog, I suggest that you read the following article:
http://www.sommarskog.se/dyn-search.html
Admin
The same is true for Oracle. Having a literal instead of a variable in an SQL-Statment can lead to a different execution plan, which may perform better. (Same rationale as given in Ian Jose's post)
Admin
I'll reiterate one more time, dyn sql in the hands of a skillfull dev can be a last resort, but suggesting it to the original coder WILL result in multiple wtf's.
Like he knows about execution plans etc.
I'm sure the newbie dev could use some more pointers than just "use dyn sql"
Admin
The developer in question was shown two generalized examples and linked to at least three articles on the topic. Shouldn't that have been enough?
Admin
Dynamic SQL is a good solution for a lot of problems, but it has its own problems. I usually use dynamic SQL to get out from under existing WTFs -- like when someone decided to make two different entities where one would be just fine (think CustomersCanada and CustomersUS). My biggest complaint about dynamic SQL is the effect on security. The whole idea that an SP can wrap the security of a call is lost with dynamic SQL.
Personally, I think SQL is a horrible language for string manipulation. I'd much rather do it in a data access layer class. Think about how much easier a lot of things like cursors and dynamic SQL are in C# instead of T-SQL. Heck, that's why they put CLR languages in SQL 2005, now I can put my code in the database so I can use it in the reporting system too. Sure SPs can provide a performance increase, but 99.9% of the time it isn't worth it -- buy another server and let the developers get the job done better and faster.
BTW, SQL 7 and later have ad-hoc batch caching. That means that code-generated blocks of T-SQL enjoy the same pre-compiled performance benefits that SPs do. The practice of writing everything as an SP as a general rule is pre-optimization at its worst. There aren't a whole lot of people that can write quality T-SQL compared to the number that can write quality C#. The tools for T-SQL suck too.
Admin
As far as I know, this is a special case in SQL Server. Since you're evaluating '@var is null', SQL Server will realize that this boils down to a constant, and resolve it before resolving the rest of the query. So the performance of:
select * from table where (@var is null or field2 = @var)
should be comparable to:
if @var is null
select * from table where @var is null
else
select * from table where field2 = @var
In one case it's evaluated in the where clause ahead of time, in the other it's evaluated in the if statement. I think the second condition is actually subject to a bug too, I believe SQL Server will, on first run when it compiles it, optimize for one branch only and always table scan in the other branch. Which is very bad in a stored procedure. Though I may have that backwards - any DBAs out there?
Admin
A common belief, but incorrect in recent versions of SQL Server. Also note that if you are using CASE statements in a stored proc the query plan is bloody useless anyway (if its even created) since each branch of the CASE will need a separate query plan. Although you believe otherwise, the use of sp_executesql will usually speed up this kind of situation.
And you'd be wrong.