Multiple tables or one mega-table in SQL?

后端 未结 5 481
攒了一身酷
攒了一身酷 2021-01-13 04:52

I am new to MySQL. I am trying to design an online file storage system. The actual file storage will be taken care of separately - I just want to use MySQL to make a databas

5条回答
  •  無奈伤痛
    2021-01-13 05:52

    I'd go for all the file records in one table. 1 million records isn't really all that big a deal for MySQL - I've used tables with 100x more records than that without any problem.

    Just make sure your indexes are good. Also consider using the InnoDB engine instead of MyISAM although there'll be a trade off between the performance lost from using InnoDB against the benefit of getting row-level locking.

    The advantages of the one table vs many tables are being to do single SQL queries such as:

    • what's the total size of all of the files in the system:

      SELECT SUM(size) FROM files

    • show me the top N users by file size (or count):

      SELECT user, SUM(size) AS total FROM files GROUP BY user ORDER BY total DESC LIMIT 10

    • show me the last N modified files:

      SELECT * FROM files ORDER BY mtime DESC LIMIT 10

    All of these are impossible to implement efficiently if you have a separate table per user.

提交回复
热议问题