Limiting the number of records from mysqldump?

前端 未结 4 813
清酒与你
清酒与你 2020-12-12 09:51

I am trying to load a small sample of records from a large database into a test database.

How do you tell mysqldump to only give you n records out of 8 million?

4条回答
  •  醉酒成梦
    2020-12-12 10:31

    As the default order is ASC which is rarely what you want in this situation, you need to have a proper database design to make DESC work out of the box. If all your tables have ONE primary key column with the same name (natural or surrogate) you can easily dump the n latest records using:

    mysqldump --opt --where="1 ORDER BY id DESC limit 1000000" --all-databases > dump.sql
    

    This is a perfect reason to why you should always name your PK's id and avoid composite PK's, even in association tables (use surrogate keys instead).

提交回复
热议问题