Skip to content

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

Merged
BaaRai merged 4 commits into
BaaRai:mainfrom
Phantom-IN:perf/traffic-host-ts-index
Nov 26, 2026
Merged

BaaRai merged 4 commits 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
@5P34R

5P34R commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Review notes

Looks good to merge. query(), Search() and Hosts() use the new index (the test checks this). Dropping idx_ex_host is safe because idx_ex_tmpl(host, url_template) still covers host= lookups. The migration is idempotent and crash-safe (create runs before drop).

Notes:

  • CREATE INDEX runs synchronously during Open, so on stores with millions of rows startup is delayed and needs temporary disk space about the size of the index. Worth a line in the CHANGELOG.
  • The CHANGELOG claim about the Traffic page's host filter is overstated. Page() filters with host LIKE '%h%' or host IN (…), orders by ts DESC, id DESC (still a temp B-tree for the tiebreaker), and its COUNT(*) walks every row for the host.

The traffic page's list goes through Page(), which this index does not
help. Also note the one-time index build on the first start.
@Phantom-IN

Copy link
Copy Markdown
Contributor Author

Fair, the page list goes through Page() and this doesn't help it. Reworded the CHANGELOG to traffic_search/Search and Hosts(), and added a note about the one-time index build on large stores (dbfaf82).

BaaRai added a commit to Coff0xc/ARTEX-contrib that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai force-pushed the perf/traffic-host-ts-index branch from 314059c to ca2eeac Compare November 26, 2026 22:09
BaaRai added a commit to Coff0xc/ARTEX-contrib that referenced this pull request Nov 26, 2026
@BaaRai

BaaRai commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Thanks @Phantom-IN, not merged yet: a CHANGELOG conflict appeared after other PRs landed while this was queued. I am rebuilding it on current main and merging next.

@BaaRai
BaaRai force-pushed the perf/traffic-host-ts-index branch from ca2eeac to c784e23 Compare November 26, 2026 22:24
BaaRai added a commit to Coff0xc/ARTEX-contrib that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai merged commit d1745dc into BaaRai:main Nov 26, 2026
6 checks passed
@BaaRai

BaaRai commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Merged, thanks @Phantom-IN. Rebased on current main and verified with its tests and negative control.

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.

3 participants