热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Sqlite+80K行+LIKE=键盘滞后-Sqlite+80Krows+LIKE=keyboardlag

IvehadthisproblemthatIhavebeenputtingoffsolving,butnowisthetime.我有这个问题,我一直推迟解决,但现在是

I've had this problem that I have been putting off solving, but now is the time.

我有这个问题,我一直推迟解决,但现在是时候了。

I have a basic dictionary program. It has a UISearchBar and a UITableView. It works the way that it should except when running on the device it causes Keyboard lag. (Simulator is fine, of course) I have two types of searching. As-you-type and On-return. I find that both take about the same amount of time to return results, but the As-you-type makes the keyboard lag.

我有一个基本的字典程序。它有一个UISearchBar和一个UITableView。它的工作方式除了在设备上运行时它会导致键盘滞后。 (模拟器很好,当然)我有两种类型的搜索。按你型和回程。我发现两者都需要大约相同的时间来返回结果,但是As-you-type会使键盘滞后。

I have UISearchBar textDidChange that takes the searchText and sends it to a search method that does all the sqlite lifting, puts the results in an Array. The reloads the table.

我有UISearchBar textDidChange,它接受searchText并将其发送到执行所有sqlite提升的搜索方法,将结果放入数组中。重新加载表格。

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if((searchType == SEARCH_AS_YOU_TYPE) && ([searchText length] >= 2)){
        NSString *myKeyword = [NSString stringWithFormat:@"%@", searchText];
        [self search:myKeyword];
        [myTableView reloadData];
    }
}

I limit the results to 50. And I my SQL query uses the LIKE and OR, no way around that yet.

我将结果限制为50.而我的SQL查询使用LIKE和OR,还没有办法解决。

SELECT WORD_ID, DEFIN, PINYIN, SIMP, TRAD from WORDS where DEFIN LIKE "%to dog %" OR DEFIN LIKE "%dog" OR DEFIN LIKE "%dog%"  ORDER BY DEFIN LIMIT 50

I've also tried moving the [myTableView reloadData] into the search method, in hopes that the keyboard would at least not lag. No Joy. And sadly I know that sqlite is basically checking every row, when it uses the like operator. But 3-4 seconds for 80rows seems kinda slow.

我也尝试将[myTableView reloadData]移动到搜索方法中,希望键盘至少不会滞后。没有喜悦。遗憾的是,我知道sqlite基本上是检查每一行,当它使用like运算符时。但是对于80人来说3-4秒似乎有点慢。

Any thoughts, ideas, comments or suggestions would be greatly appreciated!

任何想法,想法,意见或建议将不胜感激!

5 个解决方案

#1


It sounds to me that you are searching and ready the keyboard in the same thread. This way you will search as many times as you have characters and the typing speed is limited to the search speed.

听起来你正在搜索并在同一个线程中准备好键盘。这样,您将搜索与字符一样多的次数,并且打字速度仅限于搜索速度。

A proper solution is to separate this into two threads, one to read and display the keyboard, the second to search and display the search results. This way, if you are typing fast er than you can search, only the search will lag, but not the typing. For example the Firefox address bar works that way.

一个合适的解决方案是将其分为两个线程,一个用于读取和显示键盘,第二个用于搜索和显示搜索结果。这样,如果您输入的速度比搜索速度快,那么只有搜索会滞后,而不是输入。例如,Firefox地址栏以这种方式工作。

Your code will be more complex due to the two threads and the communication/coordination between the two, but I think this is the only good solution.

由于两个线程以及两者之间的通信/协调,您的代码将更加复杂,但我认为这是唯一的好解决方案。

#2


Here's how I'm guessing SQLite performs that query:

以下是我猜测SQLite执行该查询的方式:

  1. Find all rows that match one of your LIKE statements.

    查找与您的LIKE语句之一匹配的所有行。

  2. Sort them by DEFIN.

    按DEFIN排序。

  3. Truncate the results after the first 50 rows.

    在前50行之后截断结果。

I suspect the truly painful part is the sort; without that, it could simply collect the first 50 matching rows it found. Can you get away with not sorting the definitions?

我怀疑真正痛苦的部分是那种;没有它,它可以简单地收集它找到的前50个匹配行。你可以逃脱不对定义进行排序吗?

#3


Because you are using LIKE sqlite won't be using an index and it will sequentially scan from the start till is hits a limit of 50 records in the result set. Depending on the order of your data it could hit the first 50 results in the beginning or at the end worst case.

因为您正在使用LIKE sqlite将不会使用索引,它将从开始顺序扫描,直到达到结果集中的50条记录的限制。根据您的数据顺序,它可能会在开始或结束时遇到前50个结果。

How about you create new column that stores the first letter of each of the base words in defin ie d for dog etc and index this column.

如何创建新列,用于存储defin中每个基本单词的第一个字母,即d等等,并为此列编制索引。

Modify your query to select * from words where fisrtletter = :firstletter order by defin limit 50

修改您的查询以从fisrtletter =的词语中选择*:首先按照定义限制50排序

This will cut your search space significantly

这将显着减少您的搜索空间

The use the like operator for the complete match

使用like运算符进行完全匹配

you could also pre-order the data so that the order by won't be needed, I suspect the order by will cause the full table to be scanned rather than terminating when the first 50 records are hit

你也可以预先订购数据,这样就不需要订单了,我怀疑订单会导致扫描整个表而不是在前50个记录被点击时终止

#4


Try making the search query in a different thread. As UI operations are performed only on the main thread, any time consuming operations done on the main thread will delay the UI operations.

尝试在不同的线程中进行搜索查询。由于UI操作仅在主线程上执行,因此在主线程上执行的任何耗时操作都将延迟UI操作。

#5


I generally use

我一般用

[self performSelectorInBackground:@selector(threadedFetch) withObject:nil];

in combination with Core Data's NSFetchedResultsController to start a fetch upon user search input. But be careful when treading. Use separate NSManagedObjectContext for each thread or have a single context which you lock with -[NSManagedObjectContext lock].

与Core Data的NSFetchedResultsController结合使用,可以在用户搜索输入时启动提取。但是在踩踏时要小心。为每个线程使用单独的NSManagedObjectContext,或者使用单个上下文锁定 - [NSManagedObjectContext lock]。


推荐阅读
  • Thefollowingtableshowsthesymbolicconstantnames,hexadecimalvalues,andmouseorkeyb ... [详细]
  • 5transfertheinputtextintoappKeyeventInputConnectionicgetCurrentInputCo ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
  • 本文介绍了Foundation框架中一些常用的结构体和类,包括表示范围作用的NSRange结构体的创建方式,处理几何图形的数据类型NSPoint和NSSize,以及由点和大小复合而成的矩形数据类型NSRect。同时还介绍了创建这些数据类型的方法,以及字符串类NSString的使用方法。 ... [详细]
  • 【我所認知的BIOS】—>SuperIOByLightSeed2009-9-21、Superio概述SuperIO芯片也叫IO芯片。在486以上档次的主板上都有IO ... [详细]
  • 1.背景相信大家在自己的项目中都会遇到EditText输入的问题,自然而然随之产生的就是让人头痛的键盘问题了,之所以说让人头疼是因为需求不同,设计不同,我们所要做的处理也不同,加上Googl ... [详细]
  • InaniOSapplication,isitpossibletomakeownkeyboardsuggestionsiftheusertapsaspecificU ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 本文讨论了如何使用IF函数从基于有限输入列表的有限输出列表中获取输出,并提出了是否有更快/更有效的执行代码的方法。作者希望了解是否有办法缩短代码,并从自我开发的角度来看是否有更好的方法。提供的代码可以按原样工作,但作者想知道是否有更好的方法来执行这样的任务。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • MySQL多表数据库操作方法及子查询详解
    本文详细介绍了MySQL数据库的多表操作方法,包括增删改和单表查询,同时还解释了子查询的概念和用法。文章通过示例和步骤说明了如何进行数据的插入、删除和更新操作,以及如何执行单表查询和使用聚合函数进行统计。对于需要对MySQL数据库进行操作的读者来说,本文是一个非常实用的参考资料。 ... [详细]
author-avatar
我要知道521无敌
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有