realm-java: Performance regression in query (nativeSize and nativeGetRow)
Goal
What do you want to achieve?
Upgrade Realm to latest and retain performance
Expected Results
No performance drop
Actual Results
My query which looks like this became slow when updating from 1.2.0 -> 4.0.0-BETA3-SNAPSHOT.
Date startDate = DateUtils.setToMidnight(date);
Date endDate = DateUtils.setToEndOfDay(startDate);
RealmResults<Schedule> schedules = query(realm)
.beginGroup()
.beginGroup()
.lessThan(ScheduleFields.START_TIME, endDate)
.greaterThanOrEqualTo(ScheduleFields.START_TIME, startDate)
.endGroup()
.or()
.beginGroup()
.greaterThanOrEqualTo(ScheduleFields.END_TIME, startDate)
.lessThan(ScheduleFields.START_TIME, endDate)
.endGroup()
.endGroup()
.lessThanOrEqualTo(ScheduleFields.START_TIME, date)
.equalTo(ScheduleFields.CHANNEL_ID, channel.getId())
.findAllSorted(ScheduleFields.START_TIME);
if(!schedules.isEmpty()) {
return schedules.get(schedules.size() - 1);
} else {
return null;
}
With a schema that looks like this
public class Schedule
extends RealmObject {
@PrimaryKey
private long id;
private String title;
@Index
private String titleLowerCase;
private long categoryId;
private String category;
@Index
private Date startTime;
@Index
private Date endTime;
private int duration;
private String ageLimit;
private long programId;
private String episodeId;
@Index
private long channelId;
private Channel channel;
private RealmList<TVGroup> tvGroups;
The query was then reduced to
Schedule schedule = query(realm)
.lessThanOrEqualTo(ScheduleFields.START_TIME, date)
.equalTo(ScheduleFields.CHANNEL_ID, channel.getId())
.findAllSorted(ScheduleFields.START_TIME, Sort.DESCENDING)
.first(null); // <-- added this
if(schedule == null) {
return null;
}
long scheduleStartTime = schedule.getStartTime().getTime();
long scheduleEndTime = schedule.getEndTime().getTime();
if((scheduleStartTime < endDate.getTime() && scheduleStartTime >= startDate.getTime())
|| (scheduleEndTime >= startDate.getTime() && scheduleStartTime < endDate.getTime()) ){
return schedule;
}
return null;
But it is still slower in first()
than in Realm 2.3.0 (and then slow in Realm 3.0.0)
Version of Realm and tooling
Realm version(s): 1.2.0 -> 4.0.0-BETA3-SNAPSHOT
Realm sync feature enabled: no
Android Studio version: 2.3.3
Which Android version and device: LG Nexus 5X, 8.0.0
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 16 (16 by maintainers)
Commits related to this issue
- update tableview in Results.size() The orignal purpose of not creating TableView in size() is to make the call faster if the query has not been executed before. But in the real usecases, users always... — committed to realm/realm-object-store by beeender 7 years ago
- Rename update_tableveiew and expose it to public Normally size() is much faster than creating the tableview. But in some cases, like string relevant queries, the size() and creating the tableview may... — committed to realm/realm-object-store by beeender 7 years ago
- Rename update_tableveiew and expose it to public Normally size() is much faster than creating the tableview. But in some cases, like string relevant queries, the size() and creating the tableview may... — committed to realm/realm-object-store by beeender 7 years ago
- Evaluate queries immediately for sync queries - OsResults.load() actually does evaluate queries if needed now by using newly exposed OS method Results.evaluate_query_if_needed(). So the Results's... — committed to realm/realm-java by beeender 7 years ago
- Evaluate queries immediately for sync queries - OsResults.load() actually does evaluate queries if needed now by using newly exposed OS method Results.evaluate_query_if_needed(). So the Results's... — committed to realm/realm-java by beeender 7 years ago
I’ll test this asap
Well like, previously it was not lagging super-badly while scrolling, and now it does.
But I’ve been doing some method tracing magic and some query optimization
Which is now faster, but apparently the real culprit is
because I’m getting the last element in the RealmResults.
Apparently this is what’s slower now, but I’m not sure why yet. I’ll get the Realm file out.