drmq16019 2016-07-14 07:47
浏览 25
已采纳

什么更好? 具有相同实体的多个表与具有更多记录的少数关系表

I'm creating a database on mysql for a small app. Problem is there are too many fields that are identical on different Tables like

Table 1: Muncipal Issues: ID, UserID, Title, Location, Description, ImageURL,

Table 2: Harrasement Issues: ID , UserID, Title, Location, Description, ImageURL

Tables 3 same as above

both tables have almost same coulmns. i want to ask if it's better to use a relations and create a table for handling IDs and link it with other details or it's better to create a single table with an extra coulmn for these issues.

on one hand there'll be too many tables with identical columns. on the other hand there'll few tables with too many rows in it.

What will be best for performance more rows or more tables.

i'm using Mysql.

  • 写回答

1条回答 默认 最新

  • dsideal2015 2016-07-14 09:18
    关注

    Firstly, unless you expect millions of records don't care that much about performance but care more about the structure of your data and how easy it will be to access it. Literally write down a list of data that you plan to extract in your app e.g. "find all issues today", "find all unresolved issues older than 6 months" and then try to build real SQL queries on your expected structure. If they're going hard try to change the structure.

    To answer your question: it depends. The current structure has following benefits:

    • It's easy to query certain type of issues
    • It's easy to build a PHP application - just make one template form (or model) and then copypaste it with slight changes for other tables
    • In case of performance problems it may be easier to create a cluster by simply putting each table on the different db server.

    and following downsides:

    • It's inflexible. Adding new field that you forgot to add in the beginning will be painful since you'll have to change 3 (or more) tables and then the same amount of pieces in your app.
    • Adding new types of issues will be painful and require creating new table.
    • Creating SQL-s for getting data like "all non-resolved issues (regardless of type)" will require complicated UNION-s. Moreover this UNIONS will require creating virtual field with issue type otherwise you can't tell from which table did certain id come.

    The classical db approach recommends using one table for common fields and create derived tables for fields that are different. So:

    • issues table should have all common fields and is identified by PK issue_id
    • municipal_issues uses the foreign key to issues.issue_id and has only the specific fields
    • harassment_issues uses the foreign key to issues.issue_id and has only the specific fields
    • also the issues table has the issue_type field that takes values "harassment", "municipal" etc and helps finding the table where the additional data are stored.

    This pattern is called "Class Table inheritance" and you may check out the SQL antipatterns presentation for more info and other approaches. This solves the flexibility issue and still allows re-creating each of the original tables with only one simple JOIN that goes pretty fast.

    Also as a side note you may look into the db schema of bug-trackers like Mantis since this looks like the same domain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C