Exposed icon indicating copy to clipboard operation
Exposed copied to clipboard

SQLite adapter: performance issues during insert operations

Open lehvolk opened this issue 2 years ago • 5 comments

I have an application that doing large number of inserts during startup. CPU snapshot shows that about 10% of time consumed by creating SQLExcpetions based on missed columns in ResultSet returned by JDBC driver.

Preconditions Having this as dependencies

    implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.39.2.1'

    api group: 'org.jetbrains.exposed', name: 'exposed-core', version: '0.39.2'
    api group: 'org.jetbrains.exposed', name: 'exposed-dao', version: '0.39.2'
    api group: 'org.jetbrains.exposed', name: 'exposed-jdbc', version: '0.39.2'

Having dummy scheme:

object Locations : IntIdTable() {
    val path = varchar("path", length = 1024)
    val runtime = bool("runtime").default(false)
    val outdated = reference("outdated_id", Locations.id).nullable()
}

and code:

Locations.insertAndGetId {
    it[path] = "smth"
}

This code produces 4 SQLExceptions with message no such column: 'id' and no such column: 'id', no such column: 'path'. no such column: 'runtime' and no such column: 'outdated'

As I can see in debug after insert driver returns result set for query select last_insert_rowid() and Exposed tries to restore entity with it.

lehvolk avatar Aug 30 '22 07:08 lehvolk

Do you create tables with Exposed or you use existing table? Can you share CREATE TABLE statement for your Locations db?

Tapac avatar Oct 02 '22 09:10 Tapac

Do you create tables with Exposed.

Yes. Tables created with SchemaUtils. As a result SQL query is produced by SchemaUtils.

lehvolk avatar Oct 17 '22 10:10 lehvolk

Do you have any SQLite specific parameters set to your connection?

Tapac avatar Nov 12 '22 22:11 Tapac

Do you have any SQLite specific parameters set to your connection?

No. Something like this:

        val config = SQLiteConfig().also {
            it.setSynchronous(SQLiteConfig.SynchronousMode.OFF)
            it.setJournalMode(SQLiteConfig.JournalMode.OFF)
            it.setPageSize(32_768)
            it.setCacheSize(-8_000)
        }
        if (location == null) {
            val url = "jdbc:sqlite:file:jcdb-${UUID.randomUUID()}?mode=memory&cache=shared&rewriteBatchedStatements=true"
            dataSource = SQLiteDataSource(config).also {
                it.url = url
            }
        } else {
           dataSource = SQLiteDataSource(config).also {
                it.url = url
            }
        }

lehvolk avatar Nov 30 '22 12:11 lehvolk

Hey @lehvolk. I tried to reproduce this issue with your setup but couldn't. Could you please verify that it still happens in the latest Exposed version (0.43.0 at the time of writing)?

I have an application that doing large number of inserts during startup.

How large?

joc-a avatar Oct 13 '23 17:10 joc-a