2

Have a problem with querying data via Ormlite DAO, when there are few thousand results.

Code:

List<Point> pl = db.getPointsDAO().queryBuilder().where().
            eq("route_id", croute).query();

When I want to get a large list of points List<Point> pl for current Route croute I have to wait like 40 sec for 40.000 points.

where Point.class is:

@DatabaseTable(tableName = "points")
public class Point extends BaseEntity {
    @DatabaseField(generatedId = true)
    private Integer point_id;
    @DatabaseField(canBeNull = false)
    ...
    @DatabaseField(canBeNull = false)
    private Double dose;
    @DatabaseField(dataType=DataType.DATE_STRING, format="yyyy-MM-dd HH:mm:ss")
    public Date date;
    @DatabaseField(canBeNull=true,foreign=true)
    private Route route;

public Point() {
    super();
};
... ...
}

and Route.class is:

@DatabaseTable(tableName = "routes")
public class Route extends BaseEntity {

    @DatabaseField(generatedId = true)
    private Integer route_id;

    @DatabaseField(canBeNull = true)
    private String name;

    @ForeignCollectionField(eager = false)
    ForeignCollection<Point> points;

    public Route() {
        super();
    }
    ... ...
}

Some ideas what I'm doing wrong?

Thanks, Toni

toni
  • 361
  • 7
  • 19

1 Answers1

2

Couple of things to try @toni.

  1. I'd consider storing your Date as a DATE_LONG instead of a string which will save 40k string/date conversions.
  2. @Selvin is right that if there is some way for you can iterate through the database, this may lower your memory requirements and speed things up. See dao.iterator() in ORMLite.
  3. I'd use int and double primitives to lower your GC for each of your objects although I doubt it will make much of a difference.
  4. Try loading in 1000 points, then 10000, then 20000 to see if there is a drop off in performance at some point. That will tell you that you are hitting up against memory limits.
  5. Use the adb logcat utility to see if you can see the GC times to see if you are just thrashing the collector. Anything that you can do to lower your memory usage will help then. Look for lines like:
    GC_EXPLICIT freed 4140 objects / 216560 bytes in 114ms
  6. Although I doubt it is the issue, could you be missing an index? Try adding a index = true on the foreign route field.
Gray
  • 115,027
  • 24
  • 293
  • 354
  • Hi @Gray. I implemented your proposals and also used dao.iterator() but I still get speed 1k Points / sec. I check the log scale (100,1000,10000,...) and it is not a big difference. Is there problem in annotation conversion? Should I use raw query? Thank you. – toni Oct 19 '11 at 10:25
  • This is not annotation conversion which only happens at DAO creation. What did the GC logs say? Lot of time spent there? When I get back to my desk, I'll try a bulk test to see if I see anything... – Gray Oct 19 '11 at 12:22
  • At loading of 4.3k points it shows one time `GC_EXPLICIT freed 10K, 50% free`... But it shows: `GC_CONCURRENT freed 543K, 42% free 4439K/7559K, external 0K/512K, paused 3ms+11ms` all the time... looping... Thank you @Gray! – toni Oct 19 '11 at 12:45
  • This looks like you are just creating too many objects @toni. In looking at ORMLite, it looks like it only will create `Point` objects -- no temporary objects aside from those that the underlying Android API does. We haven't solved your problem, right? – Gray Oct 19 '11 at 14:16
  • I don't understand you. So this speed of loading data is normal then? – toni Oct 19 '11 at 14:18
  • So on my Mac in the simulator, I'm getting ~1ms per point load on average (10k points == 6836ms) with about 3 objects GC'd per point. This seems like it is similar to your experience. So I guess it is "normal" although it certainly seems slow. I don't know what % of this is ORMLite's overhead. – Gray Oct 19 '11 at 15:36
  • Thank you @Gray. So I'll have to live with this :)... Maybe somebody from android or ormlite group will see this :). Take care. – toni Oct 19 '11 at 16:11
  • I'm the lead author for ORMLite and I see it @toni. :-) Not sure there is much I can do about it though. – Gray Oct 20 '11 at 14:56