[quoted text, click to view] WJ wrote:
>
> Thanks David. Actually, I was going to exec the Dynamic SQL from a
> stored procedure, so I'd still
> have to only grant exec to the proc to users.
>
> Many of the fields being queries are ints and the most common have
> indexes. The ones that are non-numeric
> are varchar(255) and the user can only search with a BEGINS with
> filter (i.e. @Filter+'%'). No
> contains with a wildcard before and after the string being searched
> for. I think that will help offset some
> performance concerns.
>
> Thanks for the NOLOCK hint. That's a good idea. I'll do that.
As Tibor mentioned, user require security access to the underlying
tables in order to use dynamic SQL, even from stored procedures. You
think if you were limited to selecting data, you could grant them select
access to a view on the table.
You are going to need to do some really good testing to test for
performance in all possible conditions. Remember, if SQL Server
determines that a high percentage of rows are to be returned (and that
value could be as low as 5%), it may opt to use a table scan / clustered
index scan.
But you do have the opportunity to do some real tweaking if you are
ambitious. If you find SQL Server does not always use the proper index
strategy, and it might not given the breadth of variables, you can add
index hints to the SQL statement. For example, if you determine that a
column is being used that you know is highly selective, you could add an
index hint when that column is used in the dynamic SQL query. I'm
generally not big on index hints, but if you see performance that falls
short of acceptible, you have options.
You also can employ things like temp tables to pull out keys when highly
selectable columns are used and join the temp table with the main query.
The point is, with dynamic SQL, you have a lot of options.
You could also turn this into a client component if you find the coding
is lenghy. While it will make updates for involved, it will give you a
real language which which to program and you can still use
sp_executesql.
Lot of options.
--
David Gugick
Imceda Software
www.imceda.com