realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
889 stars 52 forks source link

Batch data updates are slow #1616

Closed X-Herman closed 5 months ago

X-Herman commented 6 months ago

How frequently does the bug occur?

Always

Description

The version of realm I'm currently using is io.realm.kotlin:library-base:1.8.0, which can be tricky when I want to update the local list data to the status field. Here is my code and test results

realm.writeBlocking {
            val status = msgChat.status
            val result = query(MsgChatRealm::class, "cvsId == '${msgChat.cvsId}'")
                .query("from != '${IM.api()?.getUserId()}'")
                .query("status == ANY{${needUpdateStatusArray.joinToString()}}")
                .query("timestamp <= '${oldTimestamp}'")
                .find()//.map { it.toEntity() }.toMutableList()
            Log.d("updateOppositeStatusByMaxTimeMsgChat=>", "size : ${result.size}")
            for (qr in result) {
                findLatest(qr)?.status = status
            }
            Log.d(
                "updateOppositeStatusByMaxTimeMsgChat=>",
                "dif : ${System.currentTimeMillis() - time}"
            )
        }
16:46:17.210 13531-13892 updateOppos...meMsgChat=>  D  start : 1703580377210
16:46:17.264 13531-13899 updateOppos...meMsgChat=>  D  start : 1703580377264
16:46:17.356 13531-13903 updateOppos...meMsgChat=>  D  start : 1703580377356
16:46:17.465 13531-13905 updateOppos...meMsgChat=>  D  start : 1703580377465
16:46:20.193 13531-13593 updateOppos...meMsgChat=>  D  dif : 5178
16:46:20.213 13531-13593 updateOppos...meMsgChat=>  D  size : 20
16:46:20.325 13531-13593 updateOppos...meMsgChat=>  D  dif : 5310
16:46:20.344 13531-13593 updateOppos...meMsgChat=>  D  size : 9
16:46:20.410 13531-13593 updateOppos...meMsgChat=>  D  dif : 5394
16:46:20.430 13531-13593 updateOppos...meMsgChat=>  D  size : 3
16:46:20.474 13531-13593 updateOppos...meMsgChat=>  D  dif : 5456
16:46:20.492 13531-13593 updateOppos...meMsgChat=>  D  size : 1
16:46:20.526 13531-13593 updateOppos...meMsgChat=>  D  dif : 5508
16:46:20.544 13531-13593 updateOppos...meMsgChat=>  D  size : 2
16:46:20.585 13531-13593 updateOppos...meMsgChat=>  D  dif : 5564

Database queries are fast, almost 1ms, but proceed

for (qr in result) {
                findLatest(qr)?.status = status
}

This operation will take more than 5s, is there any way to batch update data to improve performance

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

1.8.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

Yes

Platform OS and version(s)

android 13

Build environment

Android Studio version: ... Android Build Tools version: ... Gradle version: ...

rorbech commented 6 months ago

Hi @X-Herman. The findLatest is used to look up the most recent version of an older object. Since you are already querying the most recent version of the realm (by having the query inside your write), you don't have to use findLatest in your case. Could you try to eliminate that and see if that improves your use case?

X-Herman commented 5 months ago

I've tried it when I'm not using findLatest, and the write speed time is almost the same. I've found that it should be the write frequency, about 50 writes per second