只要有 1 个 cfqueryparam
,cfquery
就成为准备好的语句吗?或者还有其他条件吗?
当 ORDER BY
子句或 FROM
子句是动态的时会发生什么?每个独特的组合都会成为准备好的声明吗?
当我们使用 INSERT
执行 cfloop
时,每个值都经过 cfqueryparam'ed,并以不同的迭代次数调用 cfquery,会发生什么情况?
准备好的语句太多有潜在的问题吗?
DB如何处理准备好的语句?它们会被转换成类似于存储过程的东西吗?
什么情况下不应该使用prepared statements?
谢谢你!
Does cfquery
becomes a prepared statement as long as there's 1 cfqueryparam
? Or are there other conditions?
What happen when the ORDER BY
clause or FROM
clause is dynamic? Would every unique combination becomes a prepared statement?
And what happen when we're doing cfloop
with INSERT
, with every value cfqueryparam'ed, and invoke the cfquery with different number of iterations?
Any potential problems with too many prepared statements?
How does DB handle prepared statement? Will they be converted into something similar to store procedure?
Under what circumstances should we Not use prepared statement?
Thank you!
发布评论
评论(2)
我可以回答你问题的某些部分:
只要有一个,查询就会成为preparedStatement。我过去添加了一个
where 1 = 对于没有任何动态参数的查询,为了让它们作为preparedStatements运行
大多数数据库处理preparedStarements的方式与存储过程类似,只是暂时保存,而不是长期的,但细节可能是特定于数据库的。
假设您正在使用 ColdFusion 提供的驱动程序,如果您在数据源设置的高级面板中打开“日志活动”复选框,那么您将获得有关 CF 如何与 DB 交互以及何时创建的非常详细的信息一个新的preparedStatement以及何时重用它们。我建议您自己尝试一下,因为涉及很多因素(数据库设置、驱动程序、CF 版本等)。如果您确实使用数据库日志记录,请在运行测试代码之前重新启动 CF,以便您可以看到它创建准备好的语句,否则您只会看到它按 ID 重新使用语句,而看不到这些语句是什么。
I can answer some parts of your question:
a query will become a preparedStatement as long as there is one
<queryparam
. I have in the past added awhere 1 = <cfqueryparam value="1"
to queries which didn't have any dynamic parameters, in order to get them run as preparedStatementsMost DBs handle preparedStarements similarly to Stored Procedures, just held temporarily, rather than long-term, however the details are likely to be DB-specific.
Assuming you are using the drivers supplied with ColdFusion, if you turn on the 'Log Activity' checkbox in the advanced panel of the DataSource setup, then you'll get very detailed information about how CF is interacting with he DB and when it is creating a new preparedStatement and when it is re-using them. I'd recommend trying this out for yourself, as so many factors are involved (DB setup, Driver, CF version etc). If you do use the DB logging, re-start CF before running your test code, so you can see it creating the prepared statements, otherwise you'll just see it re-using statements by ID, without seeing what those statements are.
此外,如果您询问执行计划,那么涉及的不仅仅是PreparedStatement 生成的数量。这是一个巨大的主题,并且非常依赖于数据库。我对它没有 DBA 的掌握,但我可以回答一些关于 MS SQL 的问题。
基本sql不同。因此,您最终将为每个唯一 ORDER BY 子句获得单独的执行计划。
MS SQL 应该对所有迭代重复使用相同的计划,因为只有参数发生变化。
sys.dm_exec_cached_plans 视图对于查看缓存了哪些计划以及它们被重复使用的频率。
要首先清除缓存,请使用
DBCC FLUSHPROCINDB
。显然不要在生产服务器上使用它。In addition, if you are asking about execution plans then there is more involved than just the number PreparedStatement's generated. It is a huge topic and very database dependent. I do not have a DBA's grasp on it, but I can answer a few of the questions about MS SQL.
The base sql is different. So you will end up with separate execution plans for each unique ORDER BY clause.
MS SQL should reuse the same plan for all iterations because only the parameters change.
The sys.dm_exec_cached_plans view is very useful for seeing what plans are cached and how often they are reused.
To clear the cache first, use
DBCC FLUSHPROCINDB
. Obviously do not use it on a production server.