如何在 android 中为 sqlite 查询引入索引?
How to introduce indexing to sqlite query in android?
在我的 android 应用程序中,我使用 Cursor c = db.rawQuery(query, null);
从本地 sqlite 数据库查询数据,query
字符串之一如下所示:
SELECT t1.* FROM table t1
WHERE NOT EXISTS (
SELECT 1 FROM table t2
WHERE t2.start_time = t1.start_time AND t2.stop_time > t1.stop_time
)
但是,问题是当数据库变大时查询变得非常慢。试图研究引入 indexing
来加快查询速度,但到目前为止,还不是很成功,因此,在这里得到一些帮助会很棒,因为也很难找到 [=19= 的示例] 申请。
您可以为 start_time
和 stop_time
列创建复合索引:
CREATE INDEX idx_name ON table_name(start_time, stop_time);
您可以阅读 The SQLite Query Optimizer Overview:
The ON and USING clauses of an inner join are converted into
additional terms of the WHERE clause prior to WHERE clause analysis
...
和:
If an index is created using a statement like this:
CREATE INDEX idx_ex1 ON ex1(a,b,c,d,e,...,y,z);
Then the index might be used if the initial columns of the index
(columns a, b, and so forth) appear in WHERE clause terms. The initial
columns of the index must be used with the = or IN or IS operators.
The right-most column that is used can employ inequalities.
您可能需要从设备上卸载应用程序以便删除数据库并重新运行以重新创建它,或者增加数据库的版本号以便您可以在 onUpgrade()
方法中创建索引.
在我的 android 应用程序中,我使用 Cursor c = db.rawQuery(query, null);
从本地 sqlite 数据库查询数据,query
字符串之一如下所示:
SELECT t1.* FROM table t1
WHERE NOT EXISTS (
SELECT 1 FROM table t2
WHERE t2.start_time = t1.start_time AND t2.stop_time > t1.stop_time
)
但是,问题是当数据库变大时查询变得非常慢。试图研究引入 indexing
来加快查询速度,但到目前为止,还不是很成功,因此,在这里得到一些帮助会很棒,因为也很难找到 [=19= 的示例] 申请。
您可以为 start_time
和 stop_time
列创建复合索引:
CREATE INDEX idx_name ON table_name(start_time, stop_time);
您可以阅读 The SQLite Query Optimizer Overview:
The ON and USING clauses of an inner join are converted into additional terms of the WHERE clause prior to WHERE clause analysis ...
和:
If an index is created using a statement like this:
CREATE INDEX idx_ex1 ON ex1(a,b,c,d,e,...,y,z);
Then the index might be used if the initial columns of the index (columns a, b, and so forth) appear in WHERE clause terms. The initial columns of the index must be used with the = or IN or IS operators. The right-most column that is used can employ inequalities.
您可能需要从设备上卸载应用程序以便删除数据库并重新运行以重新创建它,或者增加数据库的版本号以便您可以在 onUpgrade()
方法中创建索引.