简体   繁体   English

如何为iOS加速或优化此SQLite查询?

[英]How can I speed up or optimize this SQLite query for iOS?

I have a pretty simple DB structure. 我有一个非常简单的数据库结构。 I have 12 columns in a single table, most are varchar(<50), with about 8500 rows. 我在一个表中有12列,大多数是varchar(<50),大约有8500行。

When I perform the following query on an iPhone 4, I've been averaging 2.5-3 seconds for results: 当我在iPhone 4上执行以下查询时,我的平均搜索结果为2.5-3秒:

SELECT * FROM names ORDER BY name COLLATE NOCASE ASC LIMIT 20

Doesn't seem like this sort of thing should be so slow. 看来这种事情应该不会这么慢。 Interestingly, the same query from the same app running on a 2nd gen iPod is faster by about 1.5 seconds. 有趣的是,来自第二代iPod上运行的相同应用程序的相同查询的速度约为1.5秒。 That part is beyond me. 那部分超出了我。

I have other queries that have the same issue: SELECT * FROM names WHERE SEX = ?1 AND ORIGIN = ?2 ORDER BY name COLLATE NOCASE ASC LIMIT 20 我还有其他具有相同问题的查询: SELECT * FROM names WHERE SEX = ?1 AND ORIGIN = ?2 ORDER BY name COLLATE NOCASE ASC LIMIT 20

and

SELECT * FROM names WHERE name LIKE ?3 AND SEX = ?1 AND ORIGIN = ?2 ORDER BY name COLLATE NOCASE ASC LIMIT 20

etc. 等等

I've added an index on the SQLite db: CREATE INDEX names_idx ON names (name, origin, sex, meaning) where name , origin , sex and meaning are the columns I tend to query against with WHERE and LIKE operators. 我在SQLite数据库上添加了一个索引: CREATE INDEX names_idx ON names (name, origin, sex, meaning) ,其中nameoriginsexmeaning是我倾向于使用WHERELIKE运算符查询的列。

Any thoughts on improving the performance of these searches or is this about as atomic as it gets? 是否有任何关于改善这些搜索性能的想法,或者说这与原子搜索一样原子?

The index CREATE INDEX names_idx ON names (name, origin, sex, meaning) will only be used, I believe, if your query includes ALL those columns. 我相信,如果您的查询包括所有这些列,则只会使用索引CREATE INDEX names_idx ON names (name, origin, sex, meaning) If only some are used in a query, the index can't be used. 如果查询中仅使用某些索引,则无法使用索引。

Going on your first query: SELECT * FROM names ORDER BY name COLLATE NOCASE ASC LIMIT 20 - I would suggest adding an index on name, just by itself, ie CREATE INDEX names_idx1 ON names (name) . 继续进行第一个查询: SELECT * FROM names ORDER BY name COLLATE NOCASE ASC LIMIT 20我建议仅在名称上添加一个索引,即CREATE INDEX names_idx1 ON names (name) That should in theory speed up that query. 从理论上讲,这应该加快查询速度。

If you want other indexes with combined columns for other common queries, fair enough, and it may improve query speed, but remember it'll increase your database size. 如果您希望其他带有合并列的索引用于其他常见查询,这是很公平的,它可以提高查询速度,但是请记住,它将增加数据库的大小。

What is the most used search criteria ? 最常用的搜索条件是什么? if you search for names for example you could create more tables according to the name initials. 例如,如果您搜索名称,则可以根据名称首字母创建更多表。 A table for names which start with "A" etc. The same for genre. 一个以“ A”等开头的名称的表格。 This would improve your search performance in some cases. 在某些情况下,这将提高您的搜索性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM