如何使用 PL/SQL 在 Oracle 中创建具有随机数量字段的表?
我需要创建一个具有随机列数的 Oracle 表以进行负载测试。我只想指定 NUMBER 类型的列数、VARCHAR2 类型的列数等,并且应该自动生成字段。另外,我将使用 dbms_random 来填充随机数据的表。
我怎样才能实现这个目标?
I need to create a Oracle tables with random number of columns for load testing. I just want to specify number of columns with type NUMBER, number of columns with type VARCHAR2 etc and the fields should be generated automatically. Also, I will be filling up the tables with random data for which I will be using dbms_random.
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
做的。请注意,它相当基本;您可能希望使其更复杂,例如通过改变 varchar2 列的长度:
这里是实际操作:
我们还可以使用动态 SQL 来用随机数据行填充表。
请注意,主键是用 ROWNUM 填充的,因此如果表已包含行,则很可能会失败。
,有多种方法可以提高数据的复杂性。
同样 除此之外,使用这些类型的数据池进行负载测试并不总是一个好主意,性能问题通常是由 DBMS_RANDOM 无法获得的数据值分布的偏差引起的。列 - 例如 START_DATE - 在现实生活中往往会聚集在一起,但上述过程不会生成该模式,类似地,最大化 varchar2 列将导致表占用比实际使用情况更多的存储空间。
简而言之,随机生成的数据总比没有好,但我们需要了解它的弱点。
The following procedure does just that. Note that it is rather basic; you might want to make it more sophisticated, for example by varying the length of the varchar2 columns:
Here it is in action:
We can also use dynamic SQL to populate the table with rows of random data.
Note that the primary key is populated with the ROWNUM so it will most likely fail if the table already contains rows.
Again, there are all sorts of ways to improve the sophistication of the data.
As an aside, using these sorts of data pools for load testing is not always a good idea. Performance problems are often caused by skews in the distribution of data values which you just aren't going to get with DBMS_RANDOM. This is particularly true of some date columns - e.g. START_DATE - which would tend to be clustered together in real life but the above procedure will not generate that pattern. Similarly maxing out the varchar2 columns will lead to tables which take up more storage than they wlll under real-life usage.
In short, randomly generated data is better than nothing but we need to understand its weaknesses.
两种方法
1) 编写代码来生成包含运行和填充表所需的 CREATE TABLE 命令的文本文件,然后使用 SQL*Plus 执行这些命令。
2) 使用嵌入在 PL/SQL 中的动态 SQL -
您还可以使用“CREATE TABLE AS SELECT”同时填充表(请参阅其他答案)。
这应该给你一个很好的起点——系统中没有任何东西可以在不编写代码的情况下完成你想要的事情。
Two approaches
1) Write code to generate text files containing the CREATE TABLE commands that you need to run and populate your tables, then execute these using SQL*Plus.
2) Use Dynamic SQL embedded inside PL/SQL -
You could also use 'CREATE TABLE AS SELECT' to populate your table at the same time (see other answer).
This should give you a good starting point - there isn't anything in the system that will do what you want without writing code.
您可以自己生成这样的表。
创建具有所需数据类型的表:(
您可以将 1000 更改为所需的行数,并添加所需的数据类型)
之后,您可以使用 RandomTable 中的随机数据创建或填充表
You may generate such table by yourself.
Create table with required datatypes:
(You can change 1000 to required rows numbers, and add required data types)
After that you can create or populate tables with random data from RandomTable