postgresql - 将 100 万个随机数据插入 PostgreSQL
问题描述
表结构是这样的:
milliontable(
name varchar(10),
age integer,
joindate date
)
我想在该表中插入随机的 100 万条数据。有没有办法做到这一点?
解决方案
Use the random()
function to generate random values:
INSERT INTO milliontable (name, age, joindate)
SELECT substr(md5(random()::text), 1, 10),
(random() * 70 + 10)::integer,
DATE '2018-01-01' + (random() * 700)::integer
FROM generate_series(1, 1000000);
It is usually a silly idea to store the age of a person in a table, as this number becomes wrong automatically as time goes by. Use the birthday.
推荐阅读
- elasticsearch - Elastic Search Cluster Config - 在一个物理上自动发现、配置和多弹性
- nginx - NGINX - 无法设置内容类型标头
- javascript - ReactiveSearch 的 ReactiveList 不会随着 ES 的变化而更新
- multithreading - 使用多线程处理器调试单线程应用程序的性能
- html - 保持选择选择颜色并添加png作为背景
- mysql - MySQL 查询优化 - 现有查询运行速度太慢
- php - JavaScript - 检查数据 id 是否等于函数参数
- apache-zookeeper - 无法启动 bin/zkCli.sh
- python - 如何使用 jmeter 测试 django web?
- python - NiFi中的输入flowFile没有可行的替代方案