使用 cfqueryparam 创建准备好的语句的条件?

发布于 2024-11-27 16:32:48 字数 400 浏览 2 评论 0 原文

只要有 1 个 cfqueryparamcfquery 就成为准备好的语句吗?或者还有其他条件吗?

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无力看清 2024-12-04 16:32:48

我可以回答你问题的某些部分:

只要有一个,查询就会成为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 a
where 1 = <cfqueryparam value="1" to queries which didn't have any dynamic parameters, in order to get them run as preparedStatements

Most 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.

疯到世界奔溃 2024-12-04 16:32:48

此外,如果您询问执行计划,那么涉及的不仅仅是PreparedStatement 生成的数量。这是一个巨大的主题,并且非常依赖于数据库。我对它没有 DBA 的掌握,但我可以回答一些关于 MS SQL 的问题。

当 ORDER BY 子句或 FROM 子句是动态的时会发生什么?会
每个独特的组合都会成为准备好的声明?

基本sql不同。因此,您最终将为每个唯一 ORDER BY 子句获得单独的执行计划。

当我们使用 INSERT 对每个值执行 cfloop 时会发生什么
cfqueryparam'ed,并调用不同数量的 cfquery
迭代?

MS SQL 应该对所有迭代重复使用相同的计划,因为只有参数发生变化。

sys.dm_exec_cached_plans 视图对于查看缓存了哪些计划以及它们被重复使用的频率。

SELECT  p.usecounts, p.cacheobjtype, p.objtype, t.text 
FROM    sys.dm_exec_cached_plans p
        CROSS APPLY sys.dm_exec_sql_text( p.plan_handle) t
ORDER BY p.usecounts DESC

要首先清除缓存,请使用DBCC FLUSHPROCINDB。显然不要在生产服务器上使用它。

DECLARE @ID int
SET @ID = DB_ID(N'YourTestDatabaseName')
DBCC FLUSHPROCINDB( @ID )

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.

What happen when the ORDER BY clause or FROM clause is dynamic? Would
every unique combination becomes a prepared statement?

The base sql is different. So you will end up with separate execution plans for each unique ORDER BY clause.

And what happen when we're doing cfloop with INSERT, with every value
cfqueryparam'ed, and invoke the cfquery with different number of
iterations?

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.

SELECT  p.usecounts, p.cacheobjtype, p.objtype, t.text 
FROM    sys.dm_exec_cached_plans p
        CROSS APPLY sys.dm_exec_sql_text( p.plan_handle) t
ORDER BY p.usecounts DESC

To clear the cache first, use DBCC FLUSHPROCINDB. Obviously do not use it on a production server.

DECLARE @ID int
SET @ID = DB_ID(N'YourTestDatabaseName')
DBCC FLUSHPROCINDB( @ID )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文