Skip to content

perf(traffic): index exchanges on (host, ts) - #540

Open
Phantom-IN wants to merge 1 commit into
BaaRai:mainfrom
Phantom-IN:perf/traffic-host-ts-index
Open

Phantom-IN wants to merge 1 commit into
BaaRai:mainfrom
Phantom-IN:perf/traffic-host-ts-index

Conversation

@Phantom-IN

Copy link
Copy Markdown
Contributor

traffic_search (called by the agent before binding traffic to a finding) and the host-filtered traffic list both run WHERE host=? ORDER BY ts DESC LIMIT n. The capture index only has idx_ex_host(host) and idx_ex_ts(ts), so SQLite pulls every exchange for that host and sorts them to return the newest 3. Hosts(), which the traffic page polls every 5 s, scans the whole table.

This replaces idx_ex_host with idx_ex_host_ts(host, ts). The search now walks the index and stops after n rows, and Hosts() only reads the index. Existing stores pick it up on next open. idx_ex_tmpl still starts with host, so nothing that relied on the old index loses it.

BenchmarkHostSearch (50k exchanges, 45k on one host, modernc sqlite):

before after
host search 42.05 ms 0.038 ms
Hosts() 19.89 ms 7.77 ms

TestHostSearchUsesHostTSIndex checks the query plans: no temp b-tree for the host search, and a covering index for Hosts(). It fails on main. go test ./traffic/ passes.

traffic_search and the host-filtered traffic list run
WHERE host=? ORDER BY ts DESC LIMIT n. With only idx_ex_host(host) SQLite
reads every exchange of the host and sorts them in a temp b-tree to return
the newest few. Hosts(), which the traffic page polls every 5 s, scans the
table for GROUP BY host / MAX(ts).

Replace idx_ex_host with idx_ex_host_ts(host, ts). The host search walks
the index backwards and stops after n rows, and Hosts() reads only the
index. Existing stores get the new index and drop the old one on open.

BenchmarkHostSearch, 50k exchanges (45k on one host):
  search  42.05 ms -> 0.038 ms
  hosts   19.89 ms -> 7.77 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant