ruby 从New Relic获取一个查询语句的粘贴并解释所有这些语句;现在还可以转换UPDATE和DELETE

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 从New Relic获取一个查询语句的粘贴并解释所有这些语句;现在还可以转换UPDATE和DELETE相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env ruby
puts "NR Bulk Query Explainer"
puts "make sure to adjust the mysql command line in the script to your local setup"
puts "Paste your SQL Statements Trace from New Relic and ~ and RETURN to submit."
cli = "mysql -uroot notes_from_scratch -e "

strings,queries = [],[]
while (s=gets)!="~\n"
  strings << s
end

# transform the update and delete queries to something explainable
strings.each do |s|
  next if (s.strip)==""
  string_parts = s.split("\t")
  string_parts.each do |p|
    first = (p[0..5] ? p[0..5].downcase : "")
    if first=="delete"
      # DELETE FROM `users` WHERE `id` = ?
      p.gsub!("DELETE FROM ","SELECT * from ")
      (queries << p) and break
    elsif first=="update"
      # UPDATE `subscriptions` SET user_guid = ? WHERE (user_guid = ?)
      p.gsub!("UPDATE ","SELECT * from ")
    
      select,table,rest = p.split("`")
      where_pos = (rest =~ /WHERE/)
      where = rest[where_pos..rest.length] if where_pos
      if where
        query = [select.strip,"`#{table}`",where.strip].join(" ")
        queries << query
      end
      break
    elsif first.downcase=="select"
      (queries << p) and break
    end
  end
end

# fix up the data in the queries
final_queries = []
queries.each do |q|
  candidate = q.strip.gsub("LIMIT ?", "LIMIT #{rand(20) + 1}").gsub("?", "'1'").gsub("`","``")
  final_queries << candidate
end

puts '=='
cnt = 1
final_queries.each do |q|
  eq = "#{cli} \"EXPLAIN #{q}\""
  puts "#{cnt}. #{q}"
  puts "-"
  IO.popen(eq) { |io| puts io.read }
  puts "-"
  2.times { puts }
end
puts "=="

以上是关于ruby 从New Relic获取一个查询语句的粘贴并解释所有这些语句;现在还可以转换UPDATE和DELETE的主要内容,如果未能解决你的问题,请参考以下文章

ini New Relic Postfix插件示例配置

ini New Relic Postfix插件示例配置

新的 Relic 平台插件和 RVM

自定义指标未显示在 New Relic

New Relic性能监控概览

如何使用 New Relic 跟踪 Parse Server 的类名?