田中博悠 / tanahiro2010

npm install -g ghusage
ghusage sync
ghusage::wide::

ghusage sync → SQLite(ghusage.db)activity_events → コミット・レビュー・コメントなどのイベントaccount_events → どの分析対象のイベントか--user でほかの人も分析できるaccount_id はほぼ全部同じ値macos-15 03:12:50 Testing cached 7-day aggregation (load + overview)
macos-15 03:15:43 Success
macos-15 03:15:43 Testing cached 30-day aggregation (load + overview)
macos-15 03:19:43 Success173 秒
10 万イベント・デバッグビルド・7 日分を 1 回
SELECT ... FROM activity_events e
JOIN account_events ae ON ae.event_id = e.event_id
WHERE ae.account_id = ?1 AND e.parent_pr_id = ?2activity_events → イベント本体account_events → どのアカウントがどのイベントを見たかactivity_events(parent_pr_id) にインデックスaccount_events の主キーは (account_id, event_id)|--SEARCH ae USING PRIMARY KEY (account_id=?)
`--SEARCH e USING INDEX sqlite_autoindex_activity_events_1 (event_id=?)account_id は全行同じaccount_id = 1 で絞っても 10 万行残る::wide::
ANALYZE しないと中身の偏りを知らないANALYZE も PRAGMA optimize も流してなかったaccount_id = ? → 10 行くらいparent_pr_id = ? → 10 行くらいA low-quality index (...) is one where there are more than 10 or 20 rows in the table that have the same value for the left-most column of the index.
https://www.sqlite.org/queryplanner-ng.html
SELECT ... FROM activity_events e
CROSS JOIN account_events ae ON ae.event_id = e.event_id
WHERE e.parent_pr_id = ?2 AND ae.account_id = ?1JOIN → CROSS JOINSQLite chooses to never reorder tables in a CROSS JOIN.
https://www.sqlite.org/optoverview.html
|--SEARCH e USING INDEX activity_events_parent_pr (parent_pr_id=?)
`--SEARCH ae USING COVERING INDEX account_events_event (event_id=? AND account_id=?)#[test]
fn pull_request_events_are_found_through_the_parent_index() {
let plan: Vec<String> = query_list(
&store.conn,
&format!("EXPLAIN QUERY PLAN {}", pr_events_sql()),
params![1, "PR_x"],
|r| Ok(r.get::<_, String>(3)?),
)
.unwrap();
assert!(plan[0].contains("activity_events_parent_pr"));
}JOIN → 260.8 秒CROSS JOIN → 0.79 秒macOS 26.5 / Apple M4 / SQLite 3.51.0
ANALYZE を 1 回流してから同じことをするJOIN → 0.33 秒CROSS JOIN → 0.43 秒Update 2024: The query planner has been improved so much over the years that you should never need to use any of the hacks described below.
https://www.sqlite.org/queryplanner-ng.html
PRAGMA optimize と比べたいcargo test → 509 秒 → 78 秒SEARCH は速いという意味じゃないnpm install -g ghusage