-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_tables.sql
More file actions
62 lines (57 loc) · 1.73 KB
/
Copy pathsetup_tables.sql
File metadata and controls
62 lines (57 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
-- Run once to create output tables in the recommendations database.
-- Phase 1: materialized user-item interaction matrix (source for ALS training)
CREATE TABLE IF NOT EXISTS recommendations.user_item_interactions
(
user_id Int32,
item_id String,
num_views UInt64,
num_add_to_cart UInt64,
num_wishlist UInt64,
num_checkout UInt64,
num_purchases UInt64,
interaction_score Float64,
last_event_time DateTime
)
ENGINE = MergeTree
ORDER BY (user_id, item_id);
-- Personalized feed for logged-in users (ALS output)
CREATE TABLE IF NOT EXISTS recommendations.user_recs
(
user_id UInt32,
item_ids Array(String),
scores Array(Float32),
computed_at DateTime
)
ENGINE = ReplacingMergeTree(computed_at)
ORDER BY user_id;
-- Item-item similarity via ALS item-factor cosine similarity
CREATE TABLE IF NOT EXISTS recommendations.item_similar
(
item_id String,
similar_item_ids Array(String),
scores Array(Float32),
computed_at DateTime
)
ENGINE = ReplacingMergeTree(computed_at)
ORDER BY item_id;
-- Session co-occurrence pairs ("frequently viewed/bought together")
CREATE TABLE IF NOT EXISTS recommendations.item_pairs
(
item_id String,
paired_item_ids Array(String),
pair_scores Array(Float32),
pair_type String,
computed_at DateTime
)
ENGINE = ReplacingMergeTree(computed_at)
ORDER BY (item_id, pair_type);
-- Popularity fallback for cold-start / anonymous users
CREATE TABLE IF NOT EXISTS recommendations.item_popularity
(
item_id String,
category String,
event_count UInt64,
computed_at DateTime
)
ENGINE = ReplacingMergeTree(computed_at)
ORDER BY (category, item_id);