首页 > 解决方案 > 查询执行时间不一致

问题描述

我一遍又一遍地执行相同的查询,有时它非常快,有时它需要更长的时间。有谁知道这可能是什么原因造成的?

我有一个内容提供程序,可通过我的活动的加载程序访问。

它必须将 10,000 多个条目过滤到数据库中,我对其进行了显着优化以减少查询时间。

查询通常在 0-30 毫秒内运行。当我每次执行查询时都会打印出信息。

但我注意到现在有了更多数据,有时查询会运行得非常快,而其他时候则需要 3-5 秒。使用与快速执行相同的选择/查询/参数。

日志打印出以下时间:并且查询完全相同。

Query took 15ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 15ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 3499ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 3502ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 18ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 6ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 3618ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]
Query took 3601ms to complete SELECTION: Rows: 3 :: type != ? sel args: [0]

我在Provider中记录了上面的日志

queryBuilder.setProjectionMap(projectionMap);
long start = System.currentTimeMillis();
retCursor = queryBuilder.query(mOpenHelper.getReadableDatabase(), null,
                selection,
                selectionArgs,
                null,
                null,
                null, null);
//This goes with the log statement, to prevent a null pointer on selectionArgs
if (selectionArgs == null) {
    selectionArgs = new String[]{"null"};
}
Log.d(LOG_TAG, "Query took " + (System.currentTimeMillis() - start) + "ms to complete SELECTION:" +
        " Rows: " + retCursor.getCount() +
        " :: " + selection + " sel args: " + Arrays.toString(selectionArgs));

编辑:我从中提取的表有半百万行。但是我在一个列中添加了一个标志来指示它是否是为特定用户插入的最后一条记录。并将其编入索引。这让我可以很快地为每个用户的最后一个位置获得一排。新位置删除旧位置上的标志并插入带有标志设置的新位置。我可以在不到 20 毫秒的时间内查询该表,或者那些似乎卡在 2.5 秒或更长时间的表。这似乎发生在现在大约 30-40%(est)的时间。

标签: androidsqliteloader

解决方案


推荐阅读