Исправился по всем замечаниям

This commit is contained in:
Batirbulat
2025-12-31 17:51:04 +03:00
parent e7f7bb8dc6
commit 3297ddb2b5
3 changed files with 42 additions and 37 deletions
+28 -22
View File
@@ -86,6 +86,8 @@ ORDER BY (lesson_id, course_id);
- Дата первого и последнего просмотра
*/
-- Основная таблица с AggregatingMergeTree
CREATE TABLE bhabibullin.lesson_popularity_summary
ON CLUSTER clickhouse_cluster
(
@@ -93,18 +95,19 @@ ON CLUSTER clickhouse_cluster
lesson_title String,
course_id UInt32,
course_title String,
total_views UInt32,
unique_users UInt32,
first_view DateTime64(3, 'UTC'),
last_view DateTime64(3, 'UTC')
total_views AggregateFunction(sum, UInt64),
unique_users AggregateFunction(uniq, UInt32),
first_view AggregateFunction(min, DateTime64(3, 'UTC')),
last_view AggregateFunction(max, DateTime64(3, 'UTC'))
)
ENGINE = ReplicatedMergeTree(
'/clickhouse/tables/{shard}/bhabibullin/lesson_popularity_summary',
'{replica}'
ENGINE = ReplicatedAggregatingMergeTree(
'/clickhouse/tables/{shard}/bhabibullin/lesson_popularity_summary',
'{replica}'
)
ORDER BY (lesson_id, course_id);
ORDER BY (lesson_id, course_id)
SETTINGS index_granularity = 8192;
CREATE MATERIALIZED VIEW mv_lesson_popularity_summary
CREATE MATERIALIZED VIEW bhabibullin.mv_lesson_popularity_summary
ON CLUSTER clickhouse_cluster
TO bhabibullin.lesson_popularity_summary
AS
@@ -113,13 +116,13 @@ SELECT
l.title AS lesson_title,
flv.course_id AS course_id,
c.title AS course_title,
count() AS total_views,
uniqExact(flv.user_id) AS unique_users,
min(flv.viewed_at) AS first_view,
max(flv.viewed_at) AS last_view
sumState(1) AS total_views,
uniqState(flv.user_id) AS unique_users,
minState(flv.viewed_at) AS first_view,
maxState(flv.viewed_at) AS last_view
FROM core_fact_lesson_views AS flv
JOIN core_dim_lesson AS l ON flv.lesson_id = l.lesson_id
JOIN core_dim_course AS c ON flv.course_id = c.course_id
JOIN core_dim_lesson AS l ON flv.lesson_id = l.lesson_id
JOIN core_dim_course AS c ON flv.course_id = c.course_id
GROUP BY
flv.lesson_id,
l.title,
@@ -189,21 +192,24 @@ ORDER BY user_id;
CREATE MATERIALIZED VIEW bhabibullin.mv_inactive_users_summary
ON CLUSTER clickhouse_cluster
to bhabibullin.inactive_users_summary
TO bhabibullin.inactive_users_summary
AS
SELECT
cdu.user_id,
cdu.name,
cdu.email,
cdu.age,
0 as enrollment_count,
countDistinct(flv.course_id) as enrollment_count,
cdu.registration_date
FROM core_dim_user cdu
WHERE (cdu.user_id, 1) NOT IN (
SELECT user_id, 1
FROM core_enrollments
GROUP BY user_id
);
LEFT ANTI JOIN core_fact_lesson_views flv
ON cdu.user_id = flv.user_id
GROUP BY
cdu.user_id,
cdu.name,
cdu.email,
cdu.age,
cdu.registration_date;
INSERT INTO bhabibullin.inactive_users_summary
SELECT