Re: Pass-thru SQL performance vs Stored Proc

galenboyerdev_at_hotpop.com
Date: 01/13/05


Date: 13 Jan 2005 10:53:01 -0500


"Louis Davidson" <dr_dontspamme_sql@hotmail.com> writes:

> > This all arises because I am working on a project where it seems like we
> > are going to have to create a lot of dynamic SQL based on user selects
> > (mainly searching). A user can select one to more fields to search on
> > which we have to 'AND' to our query. This is somewhat difficult to do in
> > stored procs as you have to account for all the possible search criteria
> > and add them all as parameters. This is where straight in-line (or
> > pass-thru) SQL helps.
>
> The problem with your scenario here is that cached plans have to meet a
> stringent criteria, mainly that the query needs to be pretty much the same
> each time to reuse the plan. In your case, if you have many different
> criterium, and users regularly choose different parameters, then it will not
> be useful.
>
> So my suggestion is that if you have a relative few number of parameters and
> a high performance need, then consider coding procedures. Build a very
> generic stored procedure (look up optional parms here in this group) and
> then optimize the troublesome parts, possibly even writing as many as one
> stored procedure per permutation of parameters, particularly where testing
> requires it. Obviously this is going to be tedious, so I would also
> consider writing tools to generate this code.
>
> On the other hand, if your system is not a very busy one, you might want to
> just use dynamic SQL calls. It brings up security issues if you are not
> using a middle tier (which I would suggest for any application taking
> parameters and then executing queries from them,) but in terms of
> performance, it will be good enough. Building the plan is rarely a large
> cost, but even small costs take time. The difference being if it takes 100
> ms per execution, and you are executing it 100 times an hour, no problem, a
> minute, probably bad, or a second, then forget about it.
>
> > And finally, should I be concerned about performance issues when using
> > inline sql? Right now all our code is in stored procs.
>
> In general, always use stored procedures. The case you just suggested where
> you have a lot of parms building a large procedure is a possible exception,
> just because of complexity, but in general, I always strive to keep all code
> in stored procedures for all of the good reasons (performance, security,
> encapsulation, etc)

I would think you could do something like below and get fairly far:

- Parameterize each and everything that can be a restriction.
- Default the restriction to " and 1=1 ".

--
Galen Boyer


Relevant Pages

  • Re: Deleting Multiple Rows
    ... Going dynamic sql is mostly the last option if I can't make it with standard ... Relational databases love to work with datasets. ... Executing the stored procedure to pass single parameters will cause them to ...
    (microsoft.public.sqlserver.programming)
  • SQL command parameters Advantages of?
    ... a quick ADO.NET question in regards to the command object. ... executing a stored procedure over just calling the stored procedure via ... // Pseudo code below using dynamic SQL ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Poor performance when executing stored procedure
    ... If SQL Server ... has to re-compile the SP, it takes shorter time to compile a smaller stored ... Poor performance when executing stored procedure ...
    (microsoft.public.sqlserver.programming)
  • Stored procedure does not complete until result set is retrieved from ODBC
    ... I have a SQL Server Stored procedure that I am executing via ODBC. ... -- Start Code without cursor ...
    (microsoft.public.sqlserver.odbc)
  • Re: Database Engine Tuning Advisor suggestion to replace syntax.
    ... stored procedure, the user needs EXECUTE rights to the stored procedure. ... Query 1 requires the user to have SELECT rights on the Customers ... Also, FWIW, the dynamic SQL is only syntax checked at run time, not when the ...
    (microsoft.public.sqlserver.tools)