Realm数据库中的搜索操作速度 [英] Speed of search operation in a Realm database

查看:105
本文介绍了Realm数据库中的搜索操作速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 RealmObject 类的模型:

    public class ARDatabase extends RealmObject
    {
    @PrimaryKey
    private String uid;

    private String namex;
    private String desc;
    private boolean isVideo;
    private boolean isDeleted;
    private String urlImg;
    private String urlApp;
    private int updates;
    private boolean isDownloaded;
    private String location;

    public ARDatabase(){}

    public String getUid()
    {
        return uid;
    }

    public void setUid(String uid)
    {
        this.uid = uid;
    }

    public String getNamex()
    {
        return namex;
    }

    public void setNamex(String namex)
    {
        this.namex = namex;
    }

    public String getDesc()
    {
        return desc;
    }

    public void setDesc(String desc)
    {
        this.desc = desc;
    }

    public boolean getIsVideo()
    {
        return isVideo;
    }

    public void setIsVideo(boolean isVideo)
    {
        this.isVideo = isVideo;
    }

    public boolean getIsDeleted()
    {
        return isDeleted;
    }

    public void setIsDeleted(boolean isDeleted)
    {
        this.isDeleted = isDeleted;
    }

    public String getUrlImg()
    {
        return urlImg;
    }

    public void setUrlImg(String urlImg)
    {
        this.urlImg = urlImg;
    }

    public String getUrlApp()
    {
        return urlApp;
    }

    public void setUrlApp(String urlApp)
    {
        this.urlApp = urlApp;
    }

    public int getUpdates()
    {
        return updates;
    }

    public void setUpdates(int updates)
    {
        this.updates = updates;
    }

    public boolean getIsDownloaded()
    {
        return isDownloaded;
    }

    public void setIsDownloaded(boolean isDownloaded)
    {
        this.isDownloaded = isDownloaded;
    }

    public String getLocation()
    {
        return location;
    }

    public void setLocation(String location)
    {
        this.location = location;
    }
}

这就是我搜索<$ c的方式来自我的默认领域的$ c> uid :

final ARDatabase db = mRealm.where(ARDatabase.class).equalTo("uid",imageTitles.get(result)).findFirst();

现在,问题是:考虑到里面有10-100个对象我的领域,搜索有多快?

Now, the question is: Considering I have 10-100 objects inside my realm, how fast would the search be?

用例适用于图像识别应用。当应用识别出图像时,它会返回 uid ,并根据 uid 我需要在屏幕上显示与 uid 相关的信息。

The use case is for an image recognition app. When the app recognizes an image it returns the uid, and based on the uid I need to provide an overlay on the screen with the information associated with the uid ASAP.

现在因为我有大约10-100个对象,所以线性搜索 O(n)或通用二进制搜索 O(log n)将比<$ c更快$ c>领域搜索我上面用过的?或者是否可以调整 Realm 以获得更快的结果? (如果它现在没有以最快的方式执行)。

Now since I have around 10-100 objects, would a linear search O(n) or a generic binary search O(log n) would be faster than the Realm search I've used above? Or is it possible to tweak Realm to get faster results? (in case it's not performing the fastest way now).

推荐答案

在Realm中进行搜索总是会更快,因为您可以在C ++核心内执行整个搜索。自己进行搜索意味着你会遇到在Java和C ++之间来回的开销。

Doing the search in Realm will always be faster since you can execute the entire search inside the C++ core. Doing the search yourself will mean you occur the overhead of going back and forth between Java and C++.

快速搜索单个元素的唯一要求就是你有一个该字段上的 @Index 注释,但在您的情况下,您已经有 @PrimaryKey 自动应用 @Index 注释。

The only requirement for doing fast searching for single elements is that you have an @Index annotation on the field, but in your case you already have @PrimaryKey which automatically applies the @Index annotation as well.

因此您的查询速度尽可能快。此外,对于10-100个物体,无论你做什么,它都可能对用户来说是瞬间的。

So your query is as fast as it can be. Besides, for 10-100 objects, no matter what you do, it will probably appear instantaneous to the user.

这篇关于Realm数据库中的搜索操作速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆