Die Raumdatenbank ist sehr langsam. Für die Abfrage von 20 Ergebnissen mit einer Begrenzung auf 25.000 Datensätze
Posted: 05 Jan 2025, 06:36
Code: Select all
@Entity(tableName = "messages",
indices = [
Index(value = ["id"]),
Index(value = ["reply_id"]), // Index for faster querying on reply_id
Index(value = ["chat_id"])
])
@TypeConverters(MessageConverters::class) // Add this line
data class Message(
@ColumnInfo(name = "chat_id")
val chatId: Int,
@ColumnInfo(name = "sender_id")
val senderId: Long,
@ColumnInfo(name = "recipient_id")
val recipientId: Long,
@ColumnInfo(name = "sender_message_id")
val senderMessageId: Long,
@ColumnInfo(name = "reply_id")
val replyId: Long,
@ColumnInfo(name = "content")
val content: String,
@ColumnInfo(name = "read")
val read: Boolean = false,
@ColumnInfo(name = "status")
val status:ChatMessageStatus = ChatMessageStatus.SENDING,
@ColumnInfo(name = "type")
val type:ChatMessageType = ChatMessageType.TEXT,
@ColumnInfo(name = "file_id")
val fileId:String?=null,
@ColumnInfo(name = "last_sent_chunk_index")
val lastSentChunkIndex:Int=-1,
@ColumnInfo(name = "last_sent_byte_offset")
val lastSentByteOffset:Long=-1,
@ColumnInfo(name = "last_sent_thumbnail_byte_offset")
val lastSentThumbnailByteOffset:Long=-1,
@ColumnInfo(name = "last_sent_thumbnail_chunk_index")
val lastSentThumbnailChunkIndex:Int=-1,
@ColumnInfo(name = "file_abs_path")
val fileAbsolutePath:String?=null,
@ColumnInfo(name = "thumb_path")
val fileThumbPath:String?=null,
@ColumnInfo(name = "thumb_data")
val thumbData:String?=null,
@ColumnInfo(name = "cache_path")
val fileCachePath:String?=null,
@ColumnInfo(name = "download_url")
val fileDownloadUrl:String?=null,
@ColumnInfo(name = "file_metadata")
val fileMetadata: FileMetadata?=null,
@ColumnInfo(name = "timestamp")
val timestamp: Long
) {
@PrimaryKey(autoGenerate = true)
var id: Long = 0
// Function to convert ByteArray (thumbData) to Bitmap
fun getThumbnailBitmap(): Bitmap? {
return thumbData?.let {
File(thumbData).let {
if(it.exists()){
// Use 'use' to automatically close the stream after use
val options = BitmapFactory.Options().apply {
// Decode only bounds (no image data yet)
inJustDecodeBounds = true
}
// Step 1: Open the file input stream and decode just bounds
FileInputStream(it).use { inputStream ->
BitmapFactory.decodeStream(inputStream, null, options)
}
// Step 2: Set preferred config to save memory
options.inPreferredConfig = Bitmap.Config.RGB_565 // Uses 2 bytes per pixel instead of 4 (ARGB_8888)
// Step 3: Decode the image at full size (no scaling)
options.inJustDecodeBounds = false // Load the actual image data
options.inSampleSize = 1 // No scaling down, retain original size
// Step 4: Decode the image with the final options
FileInputStream(it).use { inputStream ->
BitmapFactory.decodeStream(inputStream, null, options)
}
}else{
null
}
}
}
}
}
Code: Select all
@Deprecated("testing")
@Transaction
@Query("SELECT * FROM messages WHERE chat_id = :chatId ORDER BY id DESC LIMIT :limit")
fun getMessagesWithRepliesRaw1(chatId: Int, limit:Int): List
Es dauert einige Sekunden (manchmal sogar länger), bis die Ergebnisse angezeigt werden. Ich habe die Abfrage auf grundlegende Leistungsoptimierungen wie die Einschränkung der Ergebnismenge überprüft, aber das Problem besteht weiterhin.
``
getMessagesWithRepliesRaw1(100,20)
``