我想知道我的MySql数据库的当前大小,为了获取这个数据,我使用以下方法:
def self.calculate_total_db_size
sql = "SELECT table_schema AS 'database',
sum( data_length + index_length ) / ( 1024 *1024 ) AS size
FROM information_schema.TABLES
WHERE ENGINE=('MyISAM' || 'InnoDB' )
AND table_schema = '#{get_current_db_name}'"
return perform_sql_query(sql)
end
def self.get_current_db_name
return Rails.configuration.database_configuration[Rails.env]["database"]
end
def self.perform_sql_query(query)
result = []
mysql_res = ActiveRecord::Base.connection.execute(query)
mysql_res.each_hash{ |res| result << res }
return result
end
这在我的开发和测试环境中运行得很好,但是在生产环境中运行时查询不返回任何值。如果我手动在生产数据库上运行MySQL查询,则可以获得正确的值。为什么在生产环境中不能通过应用程序来执行查询呢?
有什么想法吗?
mysql_res.each_hash{ ... }
更改为mysql_res.each{ ... }
(看起来map
也可以)。 - Tyler Davis