ruby-on-rails - Rails ActiveRecord mysql2 适配器,默认使用 PreparedStatement
问题描述
0.4.0 版中已经添加了对 mysql2 的 PreparedStatement 支持,根据此链接
根据 0.5.2 版中的以下详细信息,它仍然没有在内部的所有 ORM 查询中使用准备好的语句:
Shipment.where(order_id: 78987898789)<br>
Shipment.where('order_id = ?', 56789876)
mysql日志:
2019-03-10T13:20:01.722848Z 1072 Query SELECT `shipments`.* FROM `shipments` WHERE `shipments`.`order_id` = 78987898789<br>
2019-03-10T13:22:27.748687Z 1072 Query SELECT `shipments`.* FROM `shipments` WHERE (order_id = 56789876)
有没有办法为所有 ORM 查询启用/禁用它?(就像 PostgreSQL 适配器ref一样)。启用它是否会对整体应用程序性能产生不利影响?
如果没有,我还没有尝试过,但是否可以使用Sequel来实现这一点,以及将现有应用程序从 MySQL2 迁移到 Sequel 有多复杂。
解决方案
Mysql2 < 5 的 ActiveRecord,不支持可配置的参数来启用
ActiveRecord 4.2.6 中的prepared_statement 代码片段
连接适配器/mysql2_适配器.rb
module ConnectionAdapters
class Mysql2Adapter < AbstractMysqlAdapter
ADAPTER_NAME = 'Mysql2'.freeze
def initialize(connection, logger, connection_options, config)
super
@prepared_statements = false # No configurable param, default set to false
configure_connection
end
...
end
带有 Mysql2 = 5.2.1 适配器的 ActiveRecord 支持可配置参数以启用来自 ActiveRecord 5.2.1 的prepared_statement 代码片段
连接适配器/mysql2_适配器.rb
module ConnectionAdapters
class Mysql2Adapter < AbstractMysqlAdapter
ADAPTER_NAME = "Mysql2".freeze
include MySQL::DatabaseStatements
def initialize(connection, logger, connection_options, config)
super
@prepared_statements = false unless config.key?(:prepared_statements)
configure_connection
end
...
end
因此,在 ActiveRecord 5.2.1 中,只需在 database.yml 中添加以下行即可启用prepared_statements
准备好的陈述:真
推荐阅读
- python - 尝试将记录从数据帧推送到 SQL Server 表
- python - 可视化使用 pandas 数据框创建的二分网络图
- mysql - 在 CREATE TEMPORARY TABLE 上截断不正确的 INTEGER 值
- java - Java 错误:未为 String 类型定义方法 add(String)
- javascript - 如何通过 JS/JQuery 在用户端检测反转颜色模式?
- sql - 如何使用条件连接两个表可能包含正则表达式条件或数组条件
- android - 颤振:RawKeyboard 侦听器未删除
- java - Micronaut Test 模拟 bean 未能注入值
- r - 转换具有第一列标题的数据集并且数据在 R 中的行中的最佳方法是什么?如何将它们转换为列?
- wordpress - 带有输入选项的 WordPress TinyMCE 自定义样式