Ad hoc queries vs stored procedures vs Dynamic SQL. Can anyone say pros and cons?
Best regards, Kristaps
Ad hoc queries vs stored procedures vs Dynamic SQL. Can anyone say pros and cons?
Best regards, Kristaps
Stored Procedures
Dynamic SQL (i.e. uses exec command within a stored procedure)
Ad Hoc SQL (i.e. created in your business code)
Note: Always parameterize your ad hoc SQL.
For OLAP ad hoc SQL: only parameterize string data. This satisfies two conditions. It prevents SQL injection attack. And it makes the queries look more unique to the database. Yes, you'll get a poor query plan cache hit ratio. But that's desirable for OLAP queries. They benefit from unique plan generation, since their datasets and most efficient plans vary greatly among given parameters.
Stored procedures
Ad hoc and dynamic - see Bill Paetzke's answers and comments.
Also, don't forget patterns such as bulk insert for SQL which isn't in your list but should still be considered.
Stored procedures PROs:
Stored procedures CONs:
RDBMS? This answer is specific to older oracle
In older oracle version < 11, dynamic sql does not reuse existing SGA sqltext plans, it creates a new entry for every execution plan the parser needs. With a lot a dynamic sql calls the sqltext area gets flushed fast enough that query reuse goes way down and peformance follows it on down.