【问题标题】:How to design database tables efficiently in MysqlMysql中如何高效设计数据库表
【发布时间】:2013-02-24 09:14:55
【问题描述】:

我有以下pojo

   public class Like {
     private Long commentId;
     private Collection<Long> accountIds;
   }

   public class Comment {
private Long personId;
    private Long pageId;
    private Long Id;
    private String text;
    private Like like;
    private LocalDate commentDate;
   }

   public class Page {
     private Long Id;
     private Long textId;
     private Collection<Comment> comments;
     private LocalTime postingDate;
     private ViewType type;
     private String mediaUrl;
     private Collection<Long> openAccountIds;
     private Like like;
   }

    public class Text{
      private Long accountId;
      private Long Id;
      private String name;
      private LocalTime firstPostedTime;
      private LocalTime lastPostedTime;
      private ViewType type;
      private Collection<Page> pages;
      private Like like;
      private String description;
      private Collection<Long> openAccountIds;
     }

现在我的文本存储库如下:

 public interface TextRepository {

Collection<Text> getAllTexts(Long accountId);

Diary getText(Long TextId);

Page getPage(Long pageId);

Comment getComment(Long commentId);

void addPageToText(Long TextId , Page page);

void addCommentToPage(Long pageId , Comment comment);

void updateText(Text text);

void deletePage(Long pageId);

void deleteComment(Long commentId);

void updateLikeToText(Long textIds);

void updateLikeToPage(Long pageId);

void updateLikeToComment(Long commentId);

}

我是 mysql 的新手。我想知道如何有效地创建 mysql 表,以便我可以在更短的时间内检索数据。此外,如果我的 pojo 包含任何结构缺陷,请继续更改它们或提供建议。

【问题讨论】:

    标签: mysql indexing foreign-keys relational-database entity-relationship


    【解决方案1】:

    以下是对象模型需要考虑的一些建议(参见 cmets),

    // Specifying all the fields as private will not allow
    // any other class to use the data!
    public class Account
    {
        public String name;
        public String location;
    }
    
    public class Text
    {
        public Collection<Account> likedBy;
        public Collection<Account> openAccounts;
        public Collection<Page> pages;
        public Account postedBy; 
        public String name; // Not sure what this field represents...
        public LocalTime firstPostedTime;
        public LocalTime lastPostedTime;
        public ViewType type;
        public String description;
    
        // Consider using get/set methods for collections, 
        // so as to expose only minimal required information 
    //  public like(Account account)
    //  {
    //      likedBy.add(account);
    //  }
    //  
    //  public dislike(Account account)
    //  {
    //      likedBy.remove(account);
    //  }
    }
    
    public class Page
    {
        public Collection<Comment> comments;
        public LocalTime postingDate;
        public ViewType type;
        public String mediaUrl;
        public Collection<Account> openAccounts;
        public Collection<Account> likedBy;
    
    //  public addComment(Comment comment)
    //  {
    //      ...
    //      Update posting date
    //  }
    //  
    //  public addOpenAccount(Account account)
    //  {
    //      ...
    //  }
    }
    
    public class Comment 
    {
        public Account postedBy;
        public String text;
        public Collection<Account> likedBy;
        public LocalDate commentDate;
    }
    

    下一步是构建实体关系图。在规范化模式时引入了主键和外键 (xxxId)。

    架构可能如下所示,

    • Account [id、姓名、位置]
    • ViewType [id, 描述]
    • Comment [id,posted_by_account_id,文本,postedDate]
    • CommentLikes [comment_id, account_id]
    • Text [id, account_id, name, firstPostedTime, lastPostedTime, type_Id, description]
    • TextAccounts [text_id, account_id]
    • TextLikes [text_id, account_id]
    • TextPages [text_id, page_id]
    • Page [id,mediaUrl,type_id,postingDate]
    • PageLikes [page_id, account_id]
    • PageComments [page_id, comment_id]
    • PageAccounts [page_id, account_id]

    【讨论】:

    • 我认为拥有private Long commentId; 之类的东西是可以的,只要类中有一个像public Long CommentId { get; set; } 这样的公共访问器,以便其他类可以访问它。 :)
    • 正是我的观点。最初的设计将所有字段都设为私有。为简单起见,我排除了 getterssetters
    猜你喜欢
    • 1970-01-01
    • 2011-12-21
    • 2013-09-25
    • 2013-12-30
    • 2012-11-15
    • 1970-01-01
    • 2016-12-04
    • 2014-01-12
    • 1970-01-01
    相关资源
    最近更新 更多