小编典典

Realm数据库中搜索操作的速度

java

这是我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;
    }
}

这就是我uid从默认领域中搜索的方式:

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

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

该用例用于图像识别应用程序。当应用识别出图像时,它将返回uid,并且基于uidI,我需要在屏幕上提供与uidASAP 相关联的信息的叠加层。

现在,由于我有大约10-100个对象,因此线性搜索O(n)或通用二进制搜索O(log n)会比Realm我上面使用的搜索更快吗?还是可以调整Realm以获得更快的结果?(以防它现在无法以最快的方式执行)。


阅读 334

收藏
2020-11-30

共1个答案

小编典典

在Realm中进行搜索始终会更快,因为您可以在C 内核中执行整个搜索。自己进行搜索将意味着您在Java和C 之间来回往返会产生开销。

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

因此,您的查询将尽可能快。此外,对于10-100个对象,无论您做什么,它都可能对用户即时显示。

2020-11-30