likes
comments
collection
share

使用内置数据库功能生成示例数据

作者站长头像
站长
· 阅读数 10

有很多方法可以为数据库生成虚拟数据。您可以创建数据生成器、使用模拟数据服务或获取生产数据的子集。此外,许多数据库提供生成合成数据的内置功能。

在本文中,您将了解如何使用特殊数据库函数、分层查询和递归公用表表达式 (CTE)在PostgreSQL、MySQL、 Oracle 和 SQL Server中生成示例数据集。

示例表

我们将使用以下示例表来演示各种内置数据生成技术:

create table sample(  id int,   val text);

id列存储记录的标识符,而val列保存一些文本值。

一旦您学会如何为具有两个列的表生成模拟数据,您就可以将这些技巧应用到具有更多不同数据类型列的表上。

PostgreSQL中的生成系列函数

PostgreSQL提供了一个特殊的generate_series函数,它可以为指定范围产生一系列数字或日期/时间值。

例如,要生成从1到5的一系列ID,您可以使用以下函数调用:

select id from generate_series(1, 5) as id;

+--+
|id|
+--+
|5 |
|4 |
|3 |
|2 |
|1 |
+--+

之后,您可以使用以下简单语句将生成的系列插入表中sample:

insert into sample(id)
select id from generate_series(1, 5) as id;

此查询向表中添加 5 条记录,每条记录都有一个唯一标识符。但是,该val列仍然是null:

select * from sample order by id;

+--+----+
|id|val |
+--+----+
|1 |null|
|2 |null|
|3 |null|
|4 |null|
|5 |null|
+--+----+

要使用val非空值填充列,您只需修改数据生成查询,如下所示:

-- Delete previously generated records
delete from sample;

-- Generate 1000 records using the concat function to populate the `val` column
insert into sample(id,val)
select id, concat('val', id * 10) from generate_series(1, 1000) as id;

最后,从表中检索前 5 条记录来查看示例数据:

select * from sample order by id limit 5;

+--+-----+
|id|val  |
+--+-----+
|1 |val10|
|2 |val20|
|3 |val30|
|4 |val40|
|5 |val50|
+--+-----+

快速又简单。

再次生成系列...但在 SQL Server 中

SQL Server 的最新版本引入了对generate_series 函数的支持。因此,无论您使用 PostgreSQL 还是 SQL Server,生成示例数据的体验都是一致的。

要生成从 1 到 5 的一系列数字,您可以按如下方式调用该函数:

select value from generate_series(1,5);

+-----+
|value|
+-----+
|1    |
|2    |
|3    |
|4    |
|5    |
+-----+

随后,执行以下命令在表中创建1000条记录sample:

insert into sample(id,val)
select value,concat('val', value * 10) from generate_series(1,1000);

要检查生成的数据,查询前 5 条记录:

select * from sample order by id
offset 0 rows
fetch next 5 rows only;

+--+-----+
|id|val  |
+--+-----+
|1 |val10|
|2 |val20|
|3 |val30|
|4 |val40|
|5 |val50|
+--+-----+

正如所演示的,该generate_series函数是执行数据生成任务的强大工具。然而,该函数还不是 SQL 标准的一部分,并且可能并非在所有数据库系统中都可用。

Oracle 中的分层查询

Oracle是不支持该generate_series功能的数据库之一。然而,数据库社区设计了许多替代方法来生成虚拟数据。

一种流行的方法涉及使用分层查询。例如,下面的分层查询可以生成从1到5的一系列记录:

select level from dual connect by level <= 5;

+-----+
|LEVEL|
+-----+
|1    |
|2    |
|3    |
|4    |
|5    |
+-----+

在内部,查询构造一个数据树结构,其中LEVEL伪列指示树的深度,从根开始。

通过使用带有插入语句的分层查询,您可以为sample表生成 1000 条记录:

-- Oracle doesn't support the `text` data type,
-- requiring you to create the table this way
create table sample (id int, val varchar(10));

-- Generate 1000 records
insert into sample(id,val)
select level, concat('val', level * 10) from dual 
connect by level <= 1000;


生成的数据将类似于您在 PostgreSQL 和 SQL Server 中看到的数据:

select * from sample order by id
offset 0 rows fetch next 5 rows only;

+--+-----+
|ID|VAL  |
+--+-----+
|1 |val10|
|2 |val20|
|3 |val30|
|4 |val40|
|5 |val50|
+--+-----+

MySQL 中的递归公用表表达式

与 Oracle 一样,MySQL 也不支持该generate_series功能,因此需要寻找替代方法来生成样本数据。

其中一种方法是使用递归公用表表达式(CTE)。为了说明这一点,以下递归查询生成一系列从 1 到 5 的数字:

with recursive seq as (
    select 1 as id union all select id +1 from seq where id < 5
)
select id from seq;

+--+
|id|
+--+
|1 |
|2 |
|3 |
|4 |
|5 |
+--+

接下来,您可以将此递归与以下插入语句结合使用来生成 1000 条记录:

insert into sample(id,val)
with recursive seq as (
    select 1 as id union all select id +1 from seq where id < 1000
)
select id, concat('val', id * 10) from seq;

最后,为了确保模拟数据已正确生成,请快速查看前五个记录:

select * from sample order by id limit 5;

+--+-----+
|id|val  |
+--+-----+
|1 |val10|
|2 |val20|
|3 |val30|
|4 |val40|
|5 |val50|
+--+-----+

继续掌握数据库

如您所见,关系数据库不仅仅是应用程序数据的存储。它们提供了广泛的功能,允许您直接在数据库端执行各种任务。其中一项任务是样本数据生成,有时可以通过内置数据库功能来满足。

作者:Denis Magda

更多技术干货请关注公号【云原生数据库

squids.cn,云数据库RDS,迁移工具DBMotion,云备份DBTwin等数据库生态工具。

转载自:https://juejin.cn/post/7300812626279399443
评论
请登录