【问题标题】:Best way to dump one database table with limit and all association转储具有限制和所有关联的数据库表的最佳方法
【发布时间】:2012-11-01 14:30:17
【问题描述】:
class Profile < ActiveRecord::Base
  has_many :favorites, :dependent => :destroy
  has_many :friends, :dependent => :destroy
end

我需要这样的东西:

mysqldump --opt --where="1 limit 1000" -uroot development profiles  > profiles.sql

但是这个转储(如预期的那样)只包含 1000 个配置文件行,没有关联的朋友、收藏夹。

我应该使用 YAML 还是应该怎么做?

【问题讨论】:

  • 转储所有三个表,然后删除 id > 1000 的配置文件
  • 我认为这是个坏主意,因为我有超过 1800 万条记录
  • 一个脚本如何找到链接到前 1000 个用户的所有收藏夹/朋友的主键,然后您可以为那些指定这些主键的表执行单独的转储

标签: mysql ruby-on-rails yaml mysqldump dump


【解决方案1】:

获取前 5000 条记录:

mysqldump --opt --where="1 limit 5000" -uroot development profiles  > profiles.sql 

然后查找所有与此记录相关的好友:

mysqldump --opt --lock-all-tables --where="profile_id IN (SELECT * FROM (SELECT id FROM profiles LIMIT 5000) temp);" -uroot development friends  > friends.sql

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    相关资源
    最近更新 更多