1

我正在为我当前的项目(Java)计划/设计我的数据库。对于这个项目,我将使用休眠。

在我的项目中,我有一个名为 Command 的基类/接口。事实上,Command 类只包含一个字段 id(唯一)。

public interface Command {
   public int getId();
   public void execute();
}

因此,Command 的每个子类的共同点是一个唯一的 id 和一个名为 execute 的方法,该方法由子类实现。

存在许多子类,如 MoveCommand、ResizeCommand 等,它们没有公共数据字段(id 除外)。

例如:

public class MoveCommand implements Command {
   private int id;
   private int oldX;
   private int oldY;
   private int newX;
   private int newY;
   public void execute() { ... }
   public int getId(){return id; }
}

public class ResizeCommand implements Command {
   private int id;
   private int oldWidth;
   private int oldHeight;
   private int newWidth;
   private int newHeight;
   public int getId(){return id; }
   public void execute() { ... }
}

然后我有另一个名为 Document 的类,其中包含命令列表

public class Document {
  private List<Command> commands;
}

因此,在运行时执行了许多基类“从命令中选择”的查询。

我的问题是:在这种情况下我应该使用什么样的继承策略来获得最佳性能。

在 SQL 中:在这种情况下,JOIN(每个子类的表)或 UNION(每个具体类的表)性能更好吗?

有没有人有这个话题的经验?

我猜每个子类的表会是更好的(设计)解决方案,但我不确定,因为存在一个具有单列(id)的数据库表,因为这是命令唯一的共同点。

4

1 回答 1

1

我希望联合策略更快,因为它需要更少的连接,但这实际上取决于您的应用程序的功能。由于基表不包含除 ID 之外的任何其他列,因此无论如何它都不是真正有用的。

两种方法之间的差异主要与归一化有关。join 策略允许为层次结构中的任何类(甚至基类)定义外键,而 union 策略只允许具体类的外键。这可能是文档和命令之间的连接表的问题。union 策略还意味着公共列的重复,但你没有任何列,所以这不是一个真正的问题。

于 2011-10-30T15:53:03.200 回答