Speed of search operation in a Realm database

我的梦境 提交于 2019-12-02 07:36:17

问题


This is the model for my RealmObject class :

    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;
    }
}

And this is how I search for the uid from my default realm:

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

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

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.

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).


回答1:


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++.

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.

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.



来源:https://stackoverflow.com/questions/42060003/speed-of-search-operation-in-a-realm-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!