Skip to content

Commit

Permalink
fix: 使用glob替换like优化查询性能: glob能利用索引但是like不行
Browse files Browse the repository at this point in the history
  • Loading branch information
qwertyyb committed Apr 5, 2023
1 parent fd324d6 commit cbefa87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Fire/CandidatesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct CandidatesView: View {
@Environment(\.colorScheme) var colorScheme

var _candidatesView: some View {
ForEach(Array(candidates.enumerated()), id: \.element) { (index, candidate) -> CandidateView in
ForEach(Array(candidates.enumerated()), id: \.offset) { (index, candidate) -> CandidateView in
CandidateView(
candidate: candidate,
index: index,
Expand Down
11 changes: 7 additions & 4 deletions Fire/DictManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DictManager {
text,
type, min(query) as query
from wb_py_dict
where query like :queryLike \(
where query glob :queryLike \(
codeMode == .wubi ? "and type = 'wb'"
: codeMode == .pinyin ? "and type = 'py'" : "")
group by text
Expand Down Expand Up @@ -120,20 +120,21 @@ class DictManager {
}

if !Defaults[.zKeyQuery] {
return origin + "%"
return origin + "*"
}

// z键查询,z不能放在首位
let first = origin.first!
return String(first) + (String(origin.suffix(origin.count - 1))
.replacingOccurrences(of: "z", with: "_")) + "%"
.replacingOccurrences(of: "z", with: "?")) + "*"
}

func getCandidates(query: String = String(), page: Int = 1) -> (candidates: [Candidate], hasNext: Bool) {
if query.count <= 0 {
return ([], false)
}
NSLog("get local candidate, origin: \(query), query: ", query)
NSLog("[DictManager] getCandidates origin: \(query)")
let startTime = CFAbsoluteTimeGetCurrent()
let queryLike = getQueryLike(query)
var candidates: [Candidate] = []
sqlite3_reset(queryStatement)
Expand Down Expand Up @@ -169,6 +170,8 @@ class DictManager {
if candidates.isEmpty {
candidates.append(Candidate(code: query, text: query, type: CandidateType.placeholder))
}
let duration = CFAbsoluteTimeGetCurrent() - startTime
NSLog("[DictManager] getCandidates query: \(query) , duration: \(duration)")
return (candidates, hasNext: allCount > count)
}

Expand Down

0 comments on commit cbefa87

Please sign in to comment.