CREATE TABLE `geo_stat` (
	`id` bigint AUTO_INCREMENT PRIMARY KEY,
	`grain` varchar(16) NOT NULL,
	`period_start` datetime NOT NULL,
	`period_end` datetime NOT NULL,
	`dataset` varchar(64) NOT NULL DEFAULT 'all',
	`tier` varchar(64) NOT NULL DEFAULT 'all',
	`client` varchar(64) NOT NULL DEFAULT 'all',
	`source` varchar(64) NOT NULL DEFAULT 'all',
	`country` char(2) NOT NULL,
	`continent` varchar(8) NOT NULL DEFAULT '',
	`sessions` bigint NOT NULL DEFAULT 0,
	`requests` bigint NOT NULL DEFAULT 0,
	`input_tokens` bigint NOT NULL DEFAULT 0,
	`output_tokens` bigint NOT NULL DEFAULT 0,
	`reasoning_tokens` bigint NOT NULL DEFAULT 0,
	`cache_read_tokens` bigint NOT NULL DEFAULT 0,
	`total_tokens` bigint NOT NULL DEFAULT 0,
	`input_cost_microcents` bigint NOT NULL DEFAULT 0,
	`output_cost_microcents` bigint NOT NULL DEFAULT 0,
	`total_cost_microcents` bigint NOT NULL DEFAULT 0,
	`avg_duration_ms` decimal(12,2),
	`p50_duration_ms` int,
	`p95_duration_ms` int,
	`avg_ttfb_ms` decimal(12,2),
	`p50_ttfb_ms` int,
	`p95_ttfb_ms` int,
	`avg_output_tps` decimal(12,4),
	`success_count` bigint NOT NULL DEFAULT 0,
	`error_count` bigint NOT NULL DEFAULT 0,
	`sample_count` bigint NOT NULL DEFAULT 0,
	`market_share_tokens` decimal(10,6),
	`market_share_requests` decimal(10,6),
	`market_share_sessions` decimal(10,6),
	`rank_by_tokens` int,
	`rank_by_requests` int,
	`rank_by_sessions` int,
	`rank_by_cost` int,
	`created_at` datetime NOT NULL DEFAULT (now()),
	`updated_at` datetime NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `uniq_country_period` UNIQUE INDEX(`grain`,`period_start`,`dataset`,`tier`,`client`,`source`,`country`)
);
--> statement-breakpoint
CREATE TABLE `provider_stat` (
	`id` bigint AUTO_INCREMENT PRIMARY KEY,
	`grain` varchar(16) NOT NULL,
	`period_start` datetime NOT NULL,
	`period_end` datetime NOT NULL,
	`dataset` varchar(64) NOT NULL DEFAULT 'all',
	`tier` varchar(64) NOT NULL DEFAULT 'all',
	`client` varchar(64) NOT NULL DEFAULT 'all',
	`source` varchar(64) NOT NULL DEFAULT 'all',
	`provider` varchar(128) NOT NULL,
	`sessions` bigint NOT NULL DEFAULT 0,
	`requests` bigint NOT NULL DEFAULT 0,
	`input_tokens` bigint NOT NULL DEFAULT 0,
	`output_tokens` bigint NOT NULL DEFAULT 0,
	`reasoning_tokens` bigint NOT NULL DEFAULT 0,
	`cache_read_tokens` bigint NOT NULL DEFAULT 0,
	`total_tokens` bigint NOT NULL DEFAULT 0,
	`input_cost_microcents` bigint NOT NULL DEFAULT 0,
	`output_cost_microcents` bigint NOT NULL DEFAULT 0,
	`total_cost_microcents` bigint NOT NULL DEFAULT 0,
	`avg_duration_ms` decimal(12,2),
	`p50_duration_ms` int,
	`p95_duration_ms` int,
	`avg_ttfb_ms` decimal(12,2),
	`p50_ttfb_ms` int,
	`p95_ttfb_ms` int,
	`avg_output_tps` decimal(12,4),
	`success_count` bigint NOT NULL DEFAULT 0,
	`error_count` bigint NOT NULL DEFAULT 0,
	`sample_count` bigint NOT NULL DEFAULT 0,
	`market_share_tokens` decimal(10,6),
	`market_share_requests` decimal(10,6),
	`market_share_sessions` decimal(10,6),
	`rank_by_tokens` int,
	`rank_by_requests` int,
	`rank_by_sessions` int,
	`rank_by_cost` int,
	`created_at` datetime NOT NULL DEFAULT (now()),
	`updated_at` datetime NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `uniq_provider_period` UNIQUE INDEX(`grain`,`period_start`,`dataset`,`tier`,`client`,`source`,`provider`)
);
--> statement-breakpoint
RENAME TABLE `stat` TO `model_stat`;--> statement-breakpoint
CREATE INDEX `idx_country_map_tokens` ON `geo_stat` (`grain`,`period_start`,`dataset`,`tier`,`total_tokens`);--> statement-breakpoint
CREATE INDEX `idx_country_rank` ON `geo_stat` (`grain`,`period_start`,`dataset`,`tier`,`rank_by_tokens`);--> statement-breakpoint
CREATE INDEX `idx_country` ON `geo_stat` (`country`,`grain`,`period_start`);--> statement-breakpoint
CREATE INDEX `idx_continent` ON `geo_stat` (`continent`,`grain`,`period_start`);--> statement-breakpoint
CREATE INDEX `idx_provider_leaderboard_tokens` ON `provider_stat` (`grain`,`period_start`,`dataset`,`tier`,`total_tokens`);--> statement-breakpoint
CREATE INDEX `idx_provider_market_share` ON `provider_stat` (`grain`,`period_start`,`dataset`,`tier`,`market_share_tokens`);--> statement-breakpoint
CREATE INDEX `idx_provider_rank` ON `provider_stat` (`grain`,`period_start`,`dataset`,`tier`,`rank_by_tokens`);--> statement-breakpoint
CREATE INDEX `idx_provider` ON `provider_stat` (`provider`,`grain`,`period_start`);