IndexedDB模糊搜索
好吧,首先,对不起我的英语。
我正在从事一个网络项目,当我在输入框中键入内容时,该项目会显示建议,但我想使用 IndexedDB 来提高 Firefox 中的查询速度。
对于WebSQL,我有这样一句话:
db.transaction(function (tx) {
var SQL = 'SELECT "column1",
"column2"
FROM "table"
WHERE "column1" LIKE ?
ORDER BY "sortcolumn" DESC
LIMIT 6';
tx.executeSql(SQL, [searchTerm + '%'], function(tx, rs) {
// Process code here
});
});
我想用IndexedDB做同样的事情,并且我有这个代码:
db.transaction(['table'], 'readonly')
.objectStore('table')
.index('sortcolumn')
.openCursor(null, 'prev')
.onsuccess = function (e) {
e || (e = event);
var cursor = e.target.result;
if (cursor) {
if (cursor.value.column1.substr(0, searchTerm.length) == searchTerm) {
// Process code here
} else {
cursor.continue();
}
}
};
但是速度太慢而且我的代码有错误..我想知道是否有更好的方法来做到这一点。
感谢您的回复。
Ok, first of all, sorry for my English.
I'm working in a web project that show suggests when I type something in the inputbox, but I want to use IndexedDB to improve the query speed in Firefox.
With WebSQL I have this sentence:
db.transaction(function (tx) {
var SQL = 'SELECT "column1",
"column2"
FROM "table"
WHERE "column1" LIKE ?
ORDER BY "sortcolumn" DESC
LIMIT 6';
tx.executeSql(SQL, [searchTerm + '%'], function(tx, rs) {
// Process code here
});
});
I want to do same thing with IndexedDB and I have this code:
db.transaction(['table'], 'readonly')
.objectStore('table')
.index('sortcolumn')
.openCursor(null, 'prev')
.onsuccess = function (e) {
e || (e = event);
var cursor = e.target.result;
if (cursor) {
if (cursor.value.column1.substr(0, searchTerm.length) == searchTerm) {
// Process code here
} else {
cursor.continue();
}
}
};
But there's too slow and my code is buggy.. I want to know is there a better way to do this.
Thank for reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我终于找到了这个问题的解决方案。
解决方案包括在搜索词和搜索词之间绑定一个关键范围,并在最后添加“z”字母。示例:
因为我需要对结果进行排序,所以我在事务之前定义了一个数组,然后我们在加载所有数据时调用它,如下所示:
I finally found the solution to this problem.
The solution consist to bound a key range between the search term and the search term with a 'z' letter at the final. Example:
Because I need to order the result, so I defined a array before the transaction, then we call it when we loaded all data, like this:
我一直在尝试 IndexedDB,发现它非常慢,再加上其 api 的复杂性,我不确定它是否值得使用。
这实际上取决于您拥有多少数据,但可能值得在内存中进行搜索,然后您可以从某种存储(indexedDB 或更简单的 localStorage)中编组和取消编组数据。
I have been experimenting with IndexedDB and I have found it to be very slow, added to that the complexity of its api and I'm not sure its worth using at all.
It really depends on how much data you have, but potentially it'd be worth doing the searching in memory, and then you can just marshall and un-marshall the data out of some kind of storage, either indexedDB or the simpler localStorage.
我在同一问题上浪费了约 2 个小时,但我找到了真正的问题。
这里的解决方案:
IDBCursor.PREV
替换为prev
(这很糟糕,但这是解决方案)
IDBCursor.PREV
目前在 Chrome 上存在 bug (26/02/2013)I have lost ~2 hours on the same problem and I have found the real problem.
Here the solution :
IDBCursor.PREV
byprev
(it's awful but this is the solution)
IDBCursor.PREV
is bugged at the moment on Chrome (26/02/2013)