-- 创建数据库(如果不存在) CREATE DATABASE IF NOT EXISTS shorturl_analytics; -- 切换到shorturl_analytics数据库 USE shorturl_analytics; -- 删除已存在的表 DROP TABLE IF EXISTS shorturl_analytics.events; -- 创建新表 CREATE TABLE IF NOT EXISTS shorturl_analytics.events ( -- 事件基础信息 event_id String, event_time DateTime64(3), -- 精确到毫秒的时间戳 event_type String, -- click, redirect, conversion, error event_attributes String DEFAULT '{}', -- 链接基本信息 link_id String, link_slug String, -- 新增slug link_label String, -- 新增label link_title String, link_original_url String, link_attributes String DEFAULT '{}', link_created_at DateTime64(3), -- 精确到毫秒的时间戳 link_expires_at Nullable(DateTime64(3)), -- 精确到毫秒的时间戳 link_tags String DEFAULT '[]', -- Array of {id, name, attributes} -- 用户信息 user_id String, user_name String, user_email String, user_attributes String DEFAULT '{}', -- 团队信息 team_id String, team_name String, team_attributes String DEFAULT '{}', -- 项目信息 project_id String, project_name String, project_attributes String DEFAULT '{}', -- QR码信息 qr_code_id String, qr_code_name String, qr_code_attributes String DEFAULT '{}', -- 访问者信息 visitor_id String, session_id String, ip_address String, country String, city String, device_type String, -- 改为String类型 browser String, os String, user_agent String, -- 来源信息 referrer String, utm_source String, utm_medium String, utm_campaign String, -- 交互信息 time_spent_sec UInt32 DEFAULT 0, is_bounce Boolean DEFAULT true, is_qr_scan Boolean DEFAULT false, conversion_type String, -- 改为String类型 conversion_value Float64 DEFAULT 0 ) ENGINE = MergeTree() PARTITION BY toYYYYMM(event_time) -- 直接使用DateTime64进行分区 ORDER BY (event_time, link_id, event_id) SETTINGS index_granularity = 8192;