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

时间:2017-02-06 03:35:39

标签: java android performance search 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;
    }
}

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

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

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

用例适用于图像识别应用。当应用识别出图片时,它会返回uid,并根据uid我需要在屏幕上提供与uid尽快关联的信息。

既然我有大约10到100个对象,那么线性搜索O(n)还是通用二进制搜索O(log n)会比我上面使用的Realm搜索更快吗?或者是否可以调整Realm以获得更快的结果? (如果它现在没有以最快的方式执行)。

1 个答案:

答案 0 :(得分:0)

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

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

所以你的查询速度尽可能快。此外,对于10-100个对象,无论你做什么,它都可能对用户来说是即时的。