h2 Database performance issues with Named Query

三世轮回 提交于 2019-12-14 03:55:59

问题


Just some pre-information. We are using a H2 File Database which is already around 15 GB.

Our Application runs on

  • Windows Clients
  • Jetty Webserver
  • H2 File Database

Every time Data needs to be updated on client side, the user will get a zip File with XML-Files. Either an XML file will be imported to the DB or the xml file has a flag "delete" and the entry in the DB will be deleted. Every import of a zip file has a data version. The import is done manually with Java. XML Files are deserialized to POJOs and mapped to our Domain Entitys.

With this, we are also able to make a full import of all data to the Database (which just takes ages - 8h).

To our issue:

The table where our problem occurs has around 290.000 rows.

The structure is:

We have a named query:

    @NamedNativeQuery(name="getRawTecdocWithMaxVersionAndGivenLocale", 
            query = "select tdo.tecdoc_guid as guid, tdo.tecdoc_locale as locale , tdo.tecdoc_version as version, tdo.data as data "
                    + " from TECDOC_OBJECTS tdo "
                    + " left outer join TECDOC_OBJECTS tdo1 "
                    + " on (tdo.tecdoc_guid = tdo1.tecdoc_guid and tdo.tecdoc_locale = tdo1.tecdoc_locale and tdo.tecdoc_version < tdo1.tecdoc_version) "
                    + " where tdo1.id is null " 
                        +  " and tdo.tecdoc_guid in ( ?1 ) "
                        +  " and tdo.tecdoc_locale = ?2 ",
            resultSetMapping = "rawTecdocs")

Which gets quite slow around 1 sec after a data update (zip file import). The actual query, with given guid did not change after the data update.

We have index on the columns which are selected.

Where it gets strange

If we fill our Database with a full update (all 15GB of data imported through XML), the query seems to be "fast" (20-50 ms) again.

Maybe someone has a hint for me/us to overcome this issue?


回答1:


Just my two cents: a very personal opinion.

We are using a H2 File Database which is already around 15 GB.

I love H2, yes I do.

Having said that, I personally think every database has its niche, and maybe 15 GB is a little bit over H2's market segment. When you get to the 1 GB mark in H2 you should consider switching to another database. If you like free databases you could start looking seriously at PostgreSQL and MariaDB.

Again, I love H2, but I think you'll start to have more and more performance issues with this level of data.

H2's SQL optimizer is obscure to say the least, and it's difficult to read. Also, it's not quite easy to make it change its mind (to make it switch the plan).




回答2:


I answered this question, where I asked explicitly a H2 specific question.

I now deleted at the end some combined indexes and now the performance is faster again.

As with ANALYZE on some clients it solved the problem on some it made it (or other parts) worse.

There is an option with USE INDEX, but this is only available after 1.4.194, which also made some other queries very slow or even impossible to execute due to not enough memory.



来源:https://stackoverflow.com/questions/49913418/h2-database-performance-issues-with-named-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!