Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Mateusz Herych - LIKE '%smth%' is not the way
Search
Base Lab
February 12, 2014
Programming
160
0
Share
Mateusz Herych - LIKE '%smth%' is not the way
Droidcon IT, Turin Feb 2014
Base Lab
February 12, 2014
More Decks by Base Lab
See All by Base Lab
Szymon Sobczak - Hadoop + Storm
baselab
0
110
Slawek Skowron - Monitoring @ Scale
baselab
0
140
Karol Nowak - Monitoring clock drift in Amazon EC2 environment
baselab
0
120
Tomasz Nowak - Web Application Testing made easy
baselab
0
310
Szymon Pawlik - UX i Automatyzacja czyli jak testerzy mogą poprawić produkt.
baselab
0
250
Jerzy Chałupski - Offline mode in Android apps
baselab
3
490
Jerzy Chałupski - Data model on Android
baselab
4
250
Other Decks in Programming
See All in Programming
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
660
PCOVから学ぶコードカバレッジ #phpcon_odawara
o0h
PRO
0
280
Lightning-Fast Method Calls with Ruby 4.1 ZJIT / RubyKaigi 2026
k0kubun
3
1.3k
AWS re:Invent 2025の少し振り返り + DevOps AgentとBacklogを連携させてみた
satoshi256kbyte
3
170
Road to RubyKaigi: Play Hard(ware)
makicamel
1
470
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
420
Swift Concurrency Type System
inamiy
1
550
VueエンジニアがReactを触って感じた_設計の違い
koukimiura
0
180
AIを導入する前にやるべきこと
negima
2
240
Server-Side Kotlin LT大会 vol.18 [Kotlin-lspの最新情報と Neovimのlsp設定例]
yasunori0418
1
190
実践CRDT
tamadeveloper
0
590
How Swift's Type System Guides AI Agents
koher
0
300
Featured
See All Featured
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
250
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Ethics towards AI in product and experience design
skipperchong
2
260
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
43k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
122
21k
Claude Code のすすめ
schroneko
67
220k
Paper Plane
katiecoart
PRO
1
49k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Discover your Explorer Soul
emna__ayadi
2
1.1k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.4k
Transcript
None
Mateusz Herych Android Developer - Base CRM Co-organizer - GDG
Krakow Co-organizer - KrakDroid
Stats
LIKE ‘%smth%’
LIKE ‘%smth%’ is not the way.
Search
Search Offline.
Why?
Why? Let the backend guys do the job
Why? Internet is not everywhere.
Why? Internet is not everywhere. It takes time. (especially SSL)
Why? Internet is not everywhere. It takes time. (especially SSL)
And sometimes it’s shitty.
Why? Internet is not everywhere. It takes time. (especially SSL)
And sometimes it’s shitty.
Sure, some apps don’ t really need it You need
an Internet to order that taxi anyway
Do you keep offline content? Let your users navigate fast.
Did I say fast?
How? Let’s go deeper.
Context
CRM - Contacts - Deals - Notes - ...
CRM - Contacts (~100) - Deals (~50) - Notes (~100)
- ... 2009
select id from deals where name LIKE ‘% something%’
CRM - Contacts (~40K) - Deals (~20K) - Notes (~300K)
- ...
None
HOW DOES “LIKE” WORKS LIKE?
Docs saying
I tried to put all the conditions that need to
be satisfied so SQLite can use indices combined with LIKE operator. Docs saying
They didn’t fit. Docs saying
http://www.sqlite. org/optoverview.html Docs saying
Hey, you, SQLite! EXPLAIN (my) QUERY PLAN
PRAGMA case_sensitive_like=1;
PRAGMA case_sensitive_like=1; CREATE INDEX search_index on deals(name);
PRAGMA case_sensitive_like=1; CREATE INDEX search_index on deals(name); SELECT id FROM
deals WHERE name LIKE ‘Some%’;
EXPLAIN QUERY PLAN SELECT id FROM deals WHERE name LIKE
‘Some%’; SEARCH TABLE deals USING COVERING INDEX search_index (name>? AND name<?) (~31250 rows)
EXPLAIN QUERY PLAN SELECT id FROM deals WHERE name LIKE
‘%Some%’;
EXPLAIN QUERY PLAN SELECT id FROM deals WHERE name LIKE
‘%Some%’; SCAN TABLE deals (~500000 rows)
EXPLAIN QUERY PLAN SELECT id FROM deals WHERE name LIKE
‘%Some%’; SCAN TABLE deals (~500000 rows) (And then you die)
first_name || ‘ ‘ || last_name? UNIONs, complicated VIEWs? Like
is NOT the way to go.
What people think SQLite is
What SQLite really is
SQLite is powerful Not kidding.
FTS3 Full Text Search
CREATE VIRTUAL TABLE search USING fts3 (tokens)
? CREATE VIRTUAL TABLE search USING fts3 (tokens INT)
Nope. PRAGMA table_info(search); cid|name|type|notnull|dflt_value|pk 0|word||0||0
All is TEXT, except for hidden rowid.
What is virtual table? Imagine it’s a Java interface. interface
VirtualTable { void insert(Params p); void update(Params p); // etc, also createTable. }
What is a virtual table? class Fts3 implements VirtualTable {
// … }
None
MATCH Let’s go make some magic.
SELECT * FROM search WHERE content MATCH ‘something’
SELECT rowid, * FROM search WHERE content MATCH ‘something’ rowid|word
1|something 2|not something special 3|SoMeThInG
SELECT rowid, * FROM search WHERE content MATCH ‘some* spe*’
rowid|word 2|not something special
CREATE VIRTUAL TABLE search USING fts3 (author, lyrics)
SELECT * FROM search WHERE lyrics MATCH ‘author:Giorgio Synthesizer author
|lyrics Giorgio Moroder|..Why don’t I use a synthesizer...
Cool?
Cool? Look at this.
SELECT * FROM search WHERE lyrics MATCH ‘why NEAR synthesizer’
author |lyrics Giorgio Moroder|..Why don’t I use synthesizer...
SELECT * FROM search WHERE lyrics MATCH ‘why NEAR/3 synthesizer’
author |lyrics Giorgio Moroder|..Why don’t I use synthesizer...
Tips.
1. Your FTS vtable should contain only tokens. Eventually divided
into sections.
2. Link your FTS table’s records with other table (containing
real object’s id and type) using rowid.
3. Remember. FTS is fast enough for searching purposes. But
it’s always slower than ‘=’ based query on indexed field.
4. EXPLAIN QUERY PLAN doesn’t work for fts tables. Try
to measure it with .timer ON.
5. ???
6. QUESTIONS TIME!