没有真正的“准备好的声明”;在铁轨上?
当我们使用ActiveRecord时,我们可以使用:
User.find(:first, :conditions=>["name=?", name])
看起来ActiveRecord正在使用“准备好的语句”,但是在查看代码后,我发现ActiveRecord只是使用了 String.dup
和 connection.quote( )
来调整构建sql的内容,不像Java。
那么,rails 中没有真正的 prepared statement
吗?为什么 Rails 不提供它?
When we use ActiveRecord, we can use:
User.find(:first, :conditions=>["name=?", name])
It looks like ActiveRecord are using 'prepared statement', but after looking into the code, I found ActiveRecord just use String.dup
and connection.quote()
to adjust the content to build a sql, not like Java.
So, there is no real prepared statment
in raiils? And why rails doesn't provide it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你所说的准备好的语句是指以编译形式保存的一段 SQL,以便更有效地执行,那么你是对的:它不是在 Rails 中实现的。造成这种情况的原因有几个:
If by prepared statement you mean a piece of SQL that is held in a compiled form for more efficient execution, then you're correct: it's not something implemented in Rails. There are several reasons for this:
Rails 3.1(或更高版本)支持准备好的语句。您所做的每个新查询都将添加到准备好的语句池中。如果您使用的是 MySql DB,它仍然不支持预准备语句,因为与不使用预准备语句相比,在 MySql 中使用预准备语句会降低性能。 此处这是 Pat Shaughnessy 撰写的关于此的好文章。
Rails 3.1 (or greater) supports prepared statements. Every new query you make will be added to pool of prepared statement. If you are are using MySql DB it still won't support prepared statement because using prepared statements in MySql reduces the performance compare to without prepared statement. Here is the nice article by Pat Shaughnessy on this.
在许多(大多数?)方面,数据库视图是一个准备好的语句。通过 ActiveRecord 使用视图很容易。只需在数据库中声明视图(我使用 rake 任务),然后通过 ActiveRecord 访问它。
非常适合对复杂 SQL 进行只读访问。使数据库免于解析/计算 SQL 所需的许多步骤。
In many (most?) ways, a DB View is a prepared statement. And it is easy to use views with ActiveRecord. Just declare the view in your DB (I use rake tasks) and then access it via ActiveRecord.
Works great for read-only access to complicated SQL. Saves the DB from many of the steps required to parse/compute SQL.