因此,我试图跳过使用外部脚本生成模拟数据,而是在PostgreSQL中使用generate_series()。如果我尝试减少行数,充其量只能返回“无法写块:临时日志文件...设备上没有足够的空间”。
代码:
CREATE TABLE posts(
id INTEGER PRIMARY KEY,
author VARCHAR(20),
likes INTEGER,
content VARCHAR(200),
posted TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO posts
SELECT DISTINCT id, author, likes, content, posted FROM
generate_series(1,10000) AS id, substr(md5(random()::text), 0, 20) AS
author, generate_series(1,10000) AS likes, md5(random()::text) AS
content, generate_series('2007-02-01'::timestamp,
'2018-04-01'::timestamp, '1 hour') AS posted;
我想到的几种可能性:
答案 0 :(得分:1)
通过执行from子句中的操作,您将获得生成的所有集合的笛卡尔积。如果您只想生成10000行,则需要以下内容。
INSERT INTO posts
SELECT id, substr(md5(random()::text), 0, 20) AS author, (random() * 100)::integer AS likes,
md5(random()::text) AS content, '2007-02-01'::timestamp + (id * '1 hour'::interval) AS posted
FROM
generate_series(1,10000) AS id