postgresql - PostgreSQL Generate_Series() 插入未完成
问题描述
所以我试图跳过使用外部脚本生成模拟数据,而是在 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;
我能想到的几种可能性:
- 这在某种程度上导致了分支因素,在这种情况下,可能有一种更有效的方法来编写它
- 我的硬件不足(i5-4210U,8GB RAM,500GB HDD,剩余空间约 20GB)。但我也在我的 2TB 桌面上运行了这个,结果相同。
- md5 哈希或 random() 函数造成了巨大的阻塞,这就是为什么我的计算机在运行此查询的前几分钟冻结的原因。
解决方案
通过执行您在 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
推荐阅读
- flutter - 如何在 Flutter 中添加/求和 TextFormField 值?
- python - 全局图的动态可视化
- python - 如何编写检查给定字符串是否在值中并返回键的python函数
- python - 有没有办法更快地进行循环
- reactjs - 自定义按钮组件始终处于禁用状态
- node.js - 将所有请求数据放入 URI 与对象 (REST)
- postman - 来自邮递员的 GET 请求不会触发 API,但浏览器会触发
- c - 为数组索引赋值返回 SIGSEGV;分段故障。| C
- javascript - 在 2 个分隔符之间拆分字符串并包含它们
- python - 图像预处理 - 为 CNN 创建数据集