order by downloaded time to improve ebs read performance
This commit is contained in:
parent
df2eaa251c
commit
a799c05f81
4 changed files with 28 additions and 19 deletions
|
|
@ -13,6 +13,7 @@ CREATE TABLE IF NOT EXISTS hosts (
|
||||||
html_title TEXT,
|
html_title TEXT,
|
||||||
iframe_allowed BOOLEAN,
|
iframe_allowed BOOLEAN,
|
||||||
best_icon_hash TEXT,
|
best_icon_hash TEXT,
|
||||||
|
icon_downloaded_at TIMESTAMPTZ,
|
||||||
parsed BOOLEAN DEFAULT FALSE,
|
parsed BOOLEAN DEFAULT FALSE,
|
||||||
random_order DOUBLE PRECISION DEFAULT random()
|
random_order DOUBLE PRECISION DEFAULT random()
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ SET work_mem = '2GB';
|
||||||
\timing on
|
\timing on
|
||||||
|
|
||||||
CREATE TEMP TABLE best_icons AS
|
CREATE TEMP TABLE best_icons AS
|
||||||
SELECT DISTINCT ON (host_id) host_id, icon_hash
|
SELECT DISTINCT ON (host_id) host_id, icon_hash, downloaded_at
|
||||||
FROM icons
|
FROM icons
|
||||||
WHERE scan_state = 'completed'
|
WHERE scan_state = 'completed'
|
||||||
AND icon_hash IS NOT NULL
|
AND icon_hash IS NOT NULL
|
||||||
|
|
@ -50,7 +50,7 @@ ORDER BY host_id,
|
||||||
|
|
||||||
\echo 'Step 2: Updating hosts...'
|
\echo 'Step 2: Updating hosts...'
|
||||||
|
|
||||||
UPDATE hosts h SET best_icon_hash = b.icon_hash
|
UPDATE hosts h SET best_icon_hash = b.icon_hash, icon_downloaded_at = b.downloaded_at
|
||||||
FROM best_icons b WHERE h.id = b.host_id;
|
FROM best_icons b WHERE h.id = b.host_id;
|
||||||
|
|
||||||
\timing off
|
\timing off
|
||||||
|
|
|
||||||
|
|
@ -2,30 +2,35 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HostRow struct {
|
type HostRow struct {
|
||||||
ID int64
|
ID int64
|
||||||
Hostname string
|
Hostname string
|
||||||
Protocol string
|
Protocol string
|
||||||
HtmlTitle string
|
HtmlTitle string
|
||||||
IframeAllowed bool
|
IframeAllowed bool
|
||||||
BestIconHash string
|
BestIconHash string
|
||||||
RandomOrder float64
|
RandomOrder float64
|
||||||
|
IconDownloadedAt *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchHostsPage gets a page of hosts with titles, ordered by id for disk locality.
|
// fetchHostsPage gets a page of hosts with titles, ordered by icon_downloaded_at for disk locality.
|
||||||
|
// Icons written to disk at similar times are physically adjacent — reading in write order
|
||||||
|
// maximizes OS readahead cache hits. Hosts without icons come last (no disk reads needed).
|
||||||
// random_order is included for bundle bucket assignment (randomized bundles).
|
// random_order is included for bundle bucket assignment (randomized bundles).
|
||||||
func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastID int64, limit int) ([]HostRow, error) {
|
func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastDownloaded *time.Time, lastID int64, limit int) ([]HostRow, error) {
|
||||||
rows, err := pool.Query(ctx, `
|
rows, err := pool.Query(ctx, `
|
||||||
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_hash, ''), random_order
|
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_hash, ''), random_order, icon_downloaded_at
|
||||||
FROM hosts
|
FROM hosts
|
||||||
WHERE html_title IS NOT NULL AND id > $1
|
WHERE html_title IS NOT NULL
|
||||||
ORDER BY id
|
AND (icon_downloaded_at, id) > ($1, $2)
|
||||||
LIMIT $2
|
ORDER BY icon_downloaded_at NULLS LAST, id
|
||||||
`, lastID, limit)
|
LIMIT $3
|
||||||
|
`, lastDownloaded, lastID, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +39,7 @@ func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastID int64, limit
|
||||||
var hosts []HostRow
|
var hosts []HostRow
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var h HostRow
|
var h HostRow
|
||||||
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash, &h.RandomOrder); err != nil {
|
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash, &h.RandomOrder, &h.IconDownloadedAt); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
hosts = append(hosts, h)
|
hosts = append(hosts, h)
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,7 @@ func main() {
|
||||||
// Stage 1: DB fetcher — continuously fetches pages into hostCh
|
// Stage 1: DB fetcher — continuously fetches pages into hostCh
|
||||||
go func() {
|
go func() {
|
||||||
defer close(hostCh)
|
defer close(hostCh)
|
||||||
|
var lastDownloaded *time.Time
|
||||||
var lastID int64
|
var lastID int64
|
||||||
pageSize := 50000
|
pageSize := 50000
|
||||||
fetched := 0
|
fetched := 0
|
||||||
|
|
@ -150,14 +151,16 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fetchStart := time.Now()
|
fetchStart := time.Now()
|
||||||
hosts, err := fetchHostsPage(ctx, pool, lastID, limit)
|
hosts, err := fetchHostsPage(ctx, pool, lastDownloaded, lastID, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to fetch hosts: %v", err)
|
log.Fatalf("Failed to fetch hosts: %v", err)
|
||||||
}
|
}
|
||||||
if len(hosts) == 0 {
|
if len(hosts) == 0 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
lastID = hosts[len(hosts)-1].ID
|
last := hosts[len(hosts)-1]
|
||||||
|
lastDownloaded = last.IconDownloadedAt
|
||||||
|
lastID = last.ID
|
||||||
fmt.Printf("[fetcher] %d hosts in %dms (hostCh: %d/%d)\n",
|
fmt.Printf("[fetcher] %d hosts in %dms (hostCh: %d/%d)\n",
|
||||||
len(hosts), time.Since(fetchStart).Milliseconds(), len(hostCh), cap(hostCh))
|
len(hosts), time.Since(fetchStart).Milliseconds(), len(hostCh), cap(hostCh))
|
||||||
for _, h := range hosts {
|
for _, h := range hosts {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue