如何在Mysql中有效地设计数据库表

时间:2013-02-24 09:14:56

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

我有以下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的新bie。我想知道如何有效地创建mysql表,以便我可以在更短的时间内检索数据。此外,如果我的pojo包含任何结构缺陷,请继续更改或提供建议。

1 个答案:

答案 0 :(得分:0)

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

// 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,name,location]
  • ViewType [id,description]
  • Comment [id,posted_by_account_id,text,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]