Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
PostgreSQL vs Elasticsearch -ファセットカウント編-
Search
forcia_dev_pr
February 21, 2022
Programming
910
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
PostgreSQL vs Elasticsearch -ファセットカウント編-
「FORCIA Meetup #4 高速検索を支えるPostgreSQLのノウハウ」の資料です
forcia_dev_pr
February 21, 2022
More Decks by forcia_dev_pr
See All by forcia_dev_pr
"書く文化"を仕組みで育てる──フォルシアの技術ブログ継続戦略
forcia_dev_pr
1
300
新しいおもちゃを見つけたい私がやっている情報収集
forcia_dev_pr
2
500
「Pythonの環境構築について」と記事作成で意識したこと
forcia_dev_pr
1
210
Neovim で VS Code みたいにコーディングする
forcia_dev_pr
1
240
なぜ・どうやって・何を書く? 〜技術記事を書く習慣の作り方〜
forcia_dev_pr
1
240
第8回ゆるふわオンサイト 解説スライド
forcia_dev_pr
0
240
第7回ゆるふわオンサイト解説
forcia_dev_pr
0
310
第6回ゆるふわオンサイト解説
forcia_dev_pr
0
320
よくわかるFORCIAのエンジニア旅行SaaSプロダクト開発編
forcia_dev_pr
0
1.4k
Other Decks in Programming
See All in Programming
そのリトライ、死んだコネクションを使い回していませんか ── GoのHTTPクライアントとHTTP/2を実プロダクト障害から学び直す
myus4a
0
340
カツオ、ご期待ください
suneo3476
0
120
mrbgem 三角測量 開発
ogom
0
140
The Rails Doctrine Decade
koic
2
420
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
19
19k
マイコン向けの軽量Ruby「PicoRuby」で各種デバイスを制御するネイティブアプリの実現手法
bash0c7
0
460
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
510
KiroのSpecで「五目並べ」を作ってみる
satoshi256kbyte
1
310
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
390
App Storeの外へ──日本のiOSサイドローディング入門 for iOSDC Japan 2026
yuukiw00w
0
280
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
770
手動確認はもう限界 〜XCUITestでCustom URL Schemeの遷移を起動種別ごとに自動テストする〜 / Testing Custom URL Schemes with XCUITest
otouto
0
360
Featured
See All Featured
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Designing for Timeless Needs
cassininazir
1
510
How to Talk to Developers About Accessibility
jct
2
560
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
380
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
720
Writing Fast Ruby
sferik
630
63k
Statistics for Hackers
jakevdp
799
230k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
550
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
330
WENDY [Excerpt]
tessaabrams
14
39k
Documentation Writing (for coders)
carmenintech
77
5.5k
Transcript
PostgreSQL vs Elasticsearch -ファセットカウント編- 籏野 拓 @フォルシア株式会社 2022.02.15 FORCIA Meetup#4
自己紹介 • 籏野 拓 (Taku Hatano) ◦ 新卒4年目 • ソフトウェアエンジニア@フォルシア株式会社
◦ 福利厚生系アプリ中心に自社プロダクトもちらほら • 活動領域 ◦ webアプリケーション (TypeScript, Node.js, React, Next.js, PostgreSQL) ◦ インフラ関連(Ansible, AWS, k8s, docker) 2
フォルシアの検索 3
What is Spook®? 4 各企業が独自に持つ膨大で複雑なデータに合わせて、 最適な検索を実現するための「技術基盤」
Spook®の特長 5 検索対象の属性を軸に絞り込みを行う →PostgreSQLの独自関数を利用し、 より高速な検索を実現
さらなる展開 6 近年、キーワード検索への需要の高まりも感じている ↓ の利用検討中
PostgreSQL vs Elasticsearch 7
キーワード検索編 普通はElasticsearchの方が5倍早い • C言語による拡張機能(extension) ◦ システム開発者向け • 形態素解析器を呼び出せるが 基本的に自作 •
Javaによる拡張機能(plugin) ◦ 応用的・ユーザー志向 • キーワード検索に必要な機能 ワンストップで提供 8 [参考] FORCIA CUBE「Elasticsearch vs. PostgreSQL」 https://www.forcia.com/blog/001551.html
ファセットカウント編 9 「ファセットカウント」 →条件で絞り込む際に 該当の条件が何件であるかを事前に表示する 実際にPostgreSQLとElasticsearchで速度比較してみた
対象データ 10 • 1レコードが複数のカテゴリを持つようなデータを対象とする。 • カテゴリは1~100のランダムな整数 • 1レコードあたり1~10のランダムな数のカテゴリをもつ • postgresはint配列、elasticsearchはNested
Typeでデータを持つ { "_index" : "search", "_type" : "_doc", "_id" : "eFHgw34BVdsiHW0pykMc", "_score" : 1.0, "_source" : { "categories" : [{ "id" : 37}, {"id" : 83}] } } id | categories ----+--------------------------------- 1 | {69,87,96,98}
集計クエリ 11 select unnest(categories) as category ,count(*) from search group
by category order by category { "size": 0, "aggs": { "categories" : { "nested": { "path": "categories" }, "aggs": { "count": { "terms": { "field": "categories.id" , "size": 101, "order": { "_key": "asc" } } } } } } }
計測方法 12 • 以下を10回繰り返して実行時間の平均を算出する ◦ 1万/10万/100万行のランダムなデータを生成する ◦ それぞれにクエリを投げて実行時間を計測 ※あるアプリでは施設数3万件、プラン数120万件くらい
計測結果 13 レコード数 PostgreSQL平均 Elasticsearch平均 10000 0.028s 0.021s 100000 0.130s
0.039s 1000000 1.429s 0.373s • レコード数が少ない場合は同等のスピード • レコード数が多いとElasticsearchに軍配 ◦ NestedTypeは配列の要素ごとに異なるドキュメント(≒レコード)として保 持されているので展開の必要がない
補足: Elasticsearchのオブジェクト配列 14 (公式: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html ) 単純なオブジェクトの配列でマッピングされている場合、 フラットな構造に展開されてインデックスされる PUT my-index-000001
/_doc/1 { "group" : "fans", "user" : [ { "first" : "John", "last" : "Smith" }, { "first" : "Alice", "last" : "White" } ] } { "group" : "fans", "user.first" : [ "alice", "john" ], "user.last" : [ "smith", "white" ] } firstとlastの組み合わせが維持されない first = “Alice” and last = “Smith” がヒットしてしまう →
補足: Elasticsearch Nested Type 15 (公式: https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html ) Nested Typeを利用することで元の組み合わせを保持してインデックスされる
{ "group" : "fans", "user.first" : "john", "user.last" : "smith" } { "group" : "fans", "user.first" : "alice", "user.last" : "white" } 「nested documents are indexed as separate documents」 ←インデックスされるイメージ first = “Alice” and last = “Smith” はヒットしない
Spook®でも計測を行ってみた 16 レコード数 PostgreSQL平均 Elasticsearch平均 Spook®平均 10000 0.028s 0.021s 0.010s
100000 0.130s 0.039s 0.022s 1000000 1.429s 0.373s 0.090s • PostgreSQLでもチューニングにより十二分な速度が出る
まとめ 17 • ElasticsearchのAggregationsという機能を使うことで GROUP BY相当の集計ができる • チューニングなどしなくてもある程度の速度が出る ◦ デフォルトでフラットな構造にしてインデックスしてくれるため
• PostgreSQLでもチューニングを行うことで高速化は可能
EOF