当查询超过几千个结果时,Android 中的 Ormlite DAO 变得非常慢

发布于 2024-12-10 17:59:08 字数 1260 浏览 0 评论 0 原文

当有几千个结果时,通过 Ormlite DAO 查询数据时遇到问题。

代码:

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

当我想获取一个大的点列表List时pl 对于当前路线 croute 我必须等待 40 秒才能获得 40.000 点。

其中 Point.class 是:

@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();
};
... ...
}

和 Route.class 是:

@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();
    }
    ... ...
}

我做错了什么?

谢谢, 托尼

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彼岸花ソ最美的依靠 2024-12-17 17:59:08

有几件事可以尝试@toni。

  1. 我会考虑将您的 Date 存储为 DATE_LONG 而不是字符串,这样可以节省 40k 字符串/日期转换。
  2. @Selvin 是对的,如果有某种方法可以迭代数据库,这可能会降低您的内存需求并加快速度。请参阅 ORMLite 中的 ">dao.iterator()。
  3. 我会使用 int 和 double 基元来降低每个对象的 GC,尽管我怀疑这会产生很大的差异。
  4. 尝试加载 1000 个点,然后加载 10000 个点,然后加载 20000 个点,看看在某个时刻性能是否会下降。这会告诉您您正在达到内存限制。
  5. 使用 adb logcat 实用程序查看是否可以查看 GC 时间,以了解您是否只是在破坏收集器。任何可以降低内存使用量的方法都会有所帮助。查找如下行:
    GC_EXPLICIT freed 4140 objects / 216560 bytes in 114ms
  6. 虽然我怀疑这是问题所在,但您是否缺少索引?尝试在外部 route 字段上添加 index = true

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文