It is more likely that your indexing (or lack thereof) is slower than dynamic SQL.
What does the implementation plan look like? Is the same request slow when executed in SSMS? How about when it is stored in the procedure?
If your table is an unindexed heap, it will not work well as the number of records increases - this is independent of the query, and the dynamic query may actually work better because the nature of the table changes, since a dynamic query is more likely to re-evaluate the query plan if it is not is in cache. This is usually not a problem (and I would not classify it as an advantage of dynamic queries), with the exception of the early stages of the system, when the SPs were not recompiled, but the statistics and query plans are outdated, but the data volume has just changed dramatically.
Not yet static. I have a dynamic query, but it does not provide any optimizations. Should I run it with a static query and give suggestions, applying them to a dynamic query? - Xaisoft (41 minutes ago)
Yes, dynamic query (EXEC (@sql)) probably will not be parsed unless you parsed the workload file. - Cade Roux (33 minutes ago)
When you have a search query on several related tables, the columns with indexes should be the search columns as well as the primary key / foreign key columns, but this depends on the power of the various tables. This analyzer should show this. - Cade Roux (22 minutes ago)
Cade roux
source share