Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 用于Ruby Sequel或ORM检测SQL查询以进行慢速查询日志分析的工具或修补程序_Ruby On Rails_Ruby_Activerecord_Sequel_Mysql Slow Query Log - Fatal编程技术网

Ruby on rails 用于Ruby Sequel或ORM检测SQL查询以进行慢速查询日志分析的工具或修补程序

Ruby on rails 用于Ruby Sequel或ORM检测SQL查询以进行慢速查询日志分析的工具或修补程序,ruby-on-rails,ruby,activerecord,sequel,mysql-slow-query-log,Ruby On Rails,Ruby,Activerecord,Sequel,Mysql Slow Query Log,我们有一套应用程序中的MySQL慢速查询日志,大部分是Ruby,一些使用Rails ActiveRecord,另一些使用Sequel 我们希望能够轻松地跟踪特定的(MySQL)慢速查询,返回到生成它的代码。这些工具中是否有可以启用的功能,或者有可以应用于它们的补丁程序,可以在SQL注释中添加嵌入的工具,比如\uuuuuuuuuuuu文件和\uuuuuuu函数

我们有一套应用程序中的MySQL慢速查询日志,大部分是Ruby,一些使用Rails ActiveRecord,另一些使用Sequel


我们希望能够轻松地跟踪特定的(MySQL)慢速查询,返回到生成它的代码。这些工具中是否有可以启用的功能,或者有可以应用于它们的补丁程序,可以在SQL注释中添加嵌入的工具,比如
\uuuuuuuuuuuu文件
\uuuuuuu函数
标识符应用程序正在花费时间。它特别擅长显示哪些Ruby代码生成了哪些SQL语句。访问下面的所有三个链接,学习如何更好地使用它。祝你好运


有趣的问题,这里是我将如何处理它

我会使用
config.active\u record.auto\u explain\u threshold\u in\u seconds
来,就像你现在做的那样

然后,我将覆盖
ActiveRecord::Explain
中的
logging\u query\u plan
方法,将任何相关数据添加到日志中。以下是添加当前stacktrace的示例:

# /config/initializers/add_additional_instumentation_to_explain.rb
module ActiveRecord
  module Explain


    def logging_query_plan # :nodoc:
      return yield unless logger

      threshold = auto_explain_threshold_in_seconds
      current   = Thread.current
      if threshold && current[:available_queries_for_explain].nil?
        begin
          queries = current[:available_queries_for_explain] = []
          start = Time.now
          result = yield
          if Time.now - start > threshold
            # START ADDING ADDITIONAL INFORMATION
            begin
              puts 'ADDING ADDITIONAL INFORMATION...'
              raise 'foo'
            rescue
              puts 'DISPLAYING THE CURRENT STACKTRACE FOR THE FOLLOWING EXPLAIN'
              puts $@
            end

            logger.warn(exec_explain(queries))

          end
          result
        ensure
          current[:available_queries_for_explain] = nil
        end
      else
        yield
      end
    end


  end
end
我更喜欢一种不依赖于rails的整个修改方法的方法,但这是使其可靠工作的唯一方法

值得一提的是,这将很容易衍生成一个gem,每个rails版本都有一个新的gem,并且只需在每个应用程序中包含您的版本的相关gem,因为听起来您可能支持多个版本的rails。这将有助于将上述方法的一些脆弱性降至最低。无论如何,希望这有帮助-祝你好运