Java JPA EntityManager的“查找”对于合并行数在1000万左右的子类实体来说是一个漫长的过程

Java JPA EntityManager的“查找”对于合并行数在1000万左右的子类实体来说是一个漫长的过程,java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,我有三个子类实体扩展了一个抽象类 @Entity @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS) public abstract class Parent { @Id private long id; } @Entity public class FirstChild { } @Entity public class SecondChild { } @Entity public class ThirdCh

我有三个子类实体扩展了一个抽象类

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Parent {

    @Id
    private long id;

}

@Entity
public class FirstChild {

}

@Entity
public class SecondChild {

}

@Entity
public class ThirdChild {

}
我使用Hibernate作为持久性提供者,使用MySQL作为数据库。将为每个子级创建三个表

现在,我使用JPA的EntityManager javax.persistence.EntityManager进行一个简单的查询,通过id查找实体

假设一个id是123456789

当所有三个表中总共有几千行时:

    Parent p = entityManager.find(id,Parent.class); //Runs fine taking no time
当所有三个表中总共有1000万行时

    Parent p = entityManager.find(id,Parent.class); //It's taking forever
而在同样的情况下,

 FirstChild first = entityManager.find(id,FirstChild.class) //Runs fine taking no time

 SecondChild second = entityManager.find(id,SecondChild.class) //Runs fine taking no time

 ThirdChild third = entityManager.find(id,ThirdChild.class) //Runs fine taking no time

这里发生了什么事?1000万不是一个大数字。不管怎样,带有精确类的查询运行得很好。请帮忙!谢谢大家!

查看Hibernate正在运行的查询。大多数连接(可能是并集)可能没有索引。解释选择它以查看发生了什么。是的,它是从三个表中选择的并集ID不是自动生成的,但我在应用层定义了它。但它在所有三张桌子上都是独一无二的。当我通过id主键本身检索实体时,我不明白为什么需要在这里建立索引?看看Hibernate正在运行的查询。大多数连接(可能是并集)可能没有索引。解释选择它以查看发生了什么。是的,它是从三个表中选择的并集ID不是自动生成的,但我在应用层定义了它。但它在所有三张桌子上都是独一无二的。当我通过id主键本身检索实体时,我不明白为什么需要在这里建立索引?