我正在使用 H2 数据库来提高性能,因此我决定在运行时将数据从 MySQL 加载到 H2 数据库。
场景是我创建了三个表:book、author、book_author。 book_author 是一个多对多表。
表book、author和book_author分别有1000、50000和50000条记录。
选择查询
select book.name
from book, author , book_author
where book.id = book_author.book_id
and book_author.author_id = author.id
and author.name = 'Charles Dickens'
执行需要 7 分钟。
我使用 spring-jdbc 创建 H2 内存数据库。
EmbeddedDatabase database_01 = new EmbeddedDatabaseBuilder().
setType(EmbeddedDatabaseType.H2).
addScript("initial_script.sql").
setName("database_01").build();
JdbcTemplate jdbcTemplate_01 = new JdbcTemplate(database_01);
有人可以建议为什么要花费这么多时间以及优化它的方法吗?
最佳答案
您创建了正确的索引吗?另请参阅documentation about index and how they are used in H2 .
关于H2内存数据库查询很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13155098/