Android 房间非常慢的读取操作
我有一个房间数据库,我试图从中读取数据,当读取16k记录时,操作需要15秒。 这是我的密码Android 房间非常慢的读取操作,android,performance,android-room,Android,Performance,Android Room,我有一个房间数据库,我试图从中读取数据,当读取16k记录时,操作需要15秒。 这是我的密码 @Entity(tableName = "reading_table") data class DatabaseReading( @PrimaryKey(autoGenerate = true) val readingId: Int = 0, val readingNum: Int, val deviceId: String, val readi
@Entity(tableName = "reading_table")
data class DatabaseReading(
@PrimaryKey(autoGenerate = true)
val readingId: Int = 0,
val readingNum: Int,
val deviceId: String,
val readingTime: Long,
val topRh: Double,
val topTemp: Double,
val botRh: Double,
val botTemp: Double,
)
这是我的刀
@Query("SELECT * FROM reading_table WHERE deviceId = :id ORDER BY readingTime ASC")
suspend fun getDeviceReadings(id: String): List<DatabaseReading>
GetReaders对16155条记录的读取时间为15563ms
如何改进这一点?我的意思是,对于数量相当大的移动设备,您可能不需要,也可能不会全部使用16k记录
您可能应该使用library for room来只加载数据块,而不是加载所有数据。事实证明,问题是由android studio上的database inspector插件引起的。
只要我不使用它,一切都会正常运行。我需要从数据中绘制一个图表,因此我确实需要加载所有内容,我找到了这篇文章,他们正在对50K行运行测试,并且值要少得多。另一件奇怪的事情是,插入相同数量的数据需要1.9万美元。我看不到这篇文章,因为它在付费墙后面
fun getReadings() {
Timber.d("get readings start")
viewModelScope.launch {
withContext(Dispatchers.IO) {
val time = measureTimeMillis {
devicesRepository.database.deviceDatabaseDao.getDeviceReadings(
deviceID
)
}
Timber.d("get readings time $time")
}
}
}