我创建了一个这样的表,
CREATE TABLE Leaderboard(
userId bigint not null,
matchId bigint not null,
score mediumint not null,
country CHAR(10),
tournamentId int not null
)
我正在创建 5 个人的比赛,他们都来自 5 个不同的国家(“英国”、“美国”、“西班牙”、“德国”、“法国”)
我想在该表中插入大约一百万个条目,并满足以下要求,
一场比赛将这样形成
usergermany | userUSA | userSPAIN | userUK | userFrance
因此,一场比赛将有 5 位用户,他们都来自不同的国家/地区。一场锦标赛会包含多场比赛,并且一个用户只能参加一场锦标赛中的一场比赛。
所以一个示例表将如下所示
userid matchid score country tournamentid
...
988654 3877543 random USA 177
388654 3877543 random GERMANY 177
433432 3877543 random FRANCE 177
776212 3877543 random UK 177
1632987 3877543 random SPAIN 177
2113242 3877544 random SPAIN 177
2918974 3877544 random USA 177
111738 3877544 random UK 177
1772342 3877544 random FRANCE 177
1343243 3877544 random GERMANY 177
123131 3877545 random UK 178
1231414 3877545 random FRANCE 178
2858348 3877545 random GERMANY 178
1122432 3877545 random USA 178
2923434 3877545 random SPAIN 178
...
一个 userId 不能在锦标赛中存在两次,
一个国家不能在一场比赛中出现两次(比赛组由不同的国家组成)
此外,由于一场比赛有 5 名玩家参加,所以 matchid 只会在表中出现 5 次。
我想在该表中插入一百万个条目,用户 ID 在上述约束下随机从 1 到 3 百万。每场比赛有 10,000 场比赛,因此从 1 开始有 100 场比赛。
100 场比赛,每场 10000 场比赛,总共 100 万行。
- 更新 -
create table seq_data as
with recursive tmp(x) as (
select 1
union all
select x+1 from tmp
limit 3000000
)
select * from tmp;
CREATE TABLE if not exists Leaderboard(
userId bigint not null,
matchId bigint not null,
score mediumint not null,
country CHAR(10),
tournamentId int not null
);
DELIMITER //
CREATE PROCEDURE InsertLeaderboardData()
BEGIN
DECLARE tournamentloop INT DEFAULT 0;
DECLARE matchloop INT DEFAULT 0;
DECLARE groupLoop INT DEFAULT 0;
DECLARE matchidKey INT DEFAULT 1;
WHILE tournamentloop < 1 DO
SET tournamentloop = tournamentloop + 1;
SET matchloop = 0;
WHILE matchloop < 10000 DO
SET matchidKey = matchidKey + 1;
SET matchloop = matchloop + 1;
SET groupLoop = 0;
WHILE groupLoop < 5 DO
SET groupLoop = groupLoop + 1;
INSERT INTO Leaderboard (userId, matchId, score, country, tournamentId)
VALUES (1, matchidKey, FLOOR(RAND() * 1000) + 1, ELT(groupLoop, "SPAIN", "FRANCE", "UK", "USA", "GERMANY"), tournamentloop);
SELECT matchidKey;
END WHILE;
END WHILE;
END WHILE;
END;
//
DELIMITER ;
CALL InsertLeaderboardData();
DROP PROCEDURE InsertLeaderboardData;
虽然运行速度很慢,但我还想要多一项功能。
至于 的,INSERT INTO
我userId
暂时将值设置为 1,以便稍后编辑。我想要做的是,我在表 seq_data 中创建了从 1 到 300 万的序列号。对于每个WHILE tournamentloop < 100 DO
范围,我想从seq_data
表中获取 50,000 个不同的数字。这样我就可以给用户一个id。
士气是,任何用户 ID 都不能在锦标赛中出现两次。因此,对于每个锦标赛范围,我需要检索不同的随机 ID 并将其分配给行。我已经创建了序列号表,我不知道这里最好的方法是什么。
- 陈述
INSERT INTO Leaderboard (userId, matchId, score, country, tournamentId) VALUES (useridKey, matchidKey, FLOOR(RAND() * 1000) + 1, ELT(groupLoop, "SPAIN", "FRANCE", "UK", "USA", "GERMANY"), tournamentloop);
我把这个变成
DECLARE tournamentloop INT DEFAULT 0;
DECLARE matchloop INT DEFAULT 0;
DECLARE groupLoop INT DEFAULT 0;
DECLARE matchidKey INT DEFAULT 1;
DECLARE useridKey INT DEFAULT 1;
PREPARE insertStmt FROM 'INSERT INTO Leaderboard (userId, matchId, score, country, tournamentId)VALUES (useridKey, matchidKey, FLOOR(RAND() * 1000) + 1, ELT(groupLoop, "SPAIN", "FRANCE", "UK", "USA", "GERMANY"), tournamentloop);';
EXECUTE insertStmt;
我收到错误:Unknown column 'useridKey' in 'field list'