gpt4 book ai didi

mysql - 通过 Rails 检查 MySql 数据库大小

转载 作者:行者123 更新时间:2023-11-29 04:13:04 25 4
gpt4 key购买 nike

我有一个运行在 MySql 数据库上的 Rails 2.3.8 应用程序。是否可以通过 Rails 应用程序检查数据库的实际大小(以兆字节为单位)?如果是这样,我能否获得详细 View ,例如某个表占用多少兆字节?

有什么想法吗?

最佳答案

如果您只想将结果返回到哈希中,那么这很有效:

db_name = Rails.configuration.database_configuration[Rails.env]["database"]

sql = "SELECT table_name AS name, table_rows, data_length, index_length FROM information_schema.TABLES WHERE table_schema = '#{db_name}' ORDER BY (data_length + index_length) DESC;"

table_data = ActiveRecord::Base.connection.execute(sql).map{|r| {name: r[0], rows: r[1], data_size: r[2], index_size: r[3]}}

然后,如果您在 Rails 控制台中并想要转储结果:

c,d,i=0,0,0;table_data.each {|r| print r[:name].ljust(30),helper.number_with_delimiter(r[:rows]).rjust(16)," rows", helper.number_to_human_size(r[:data_size]).rjust(12), helper.number_to_human_size(r[:index_size]).rjust(12), helper.number_to_human_size(r[:data_size] + r[:index_size]).rjust(12),"\n";c+=r[:rows];d+=r[:data_size];i+=r[:index_size]};print "-"*90,"\n","Total".ljust(30),helper.number_with_delimiter(c).rjust(16)," rows", helper.number_to_human_size(d).rjust(12), helper.number_to_human_size(i).rjust(12), helper.number_to_human_size(d+i).rjust(12),"\n"

关于mysql - 通过 Rails 检查 MySql 数据库大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5297602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com