Skip to content

What is more efficient: arrays or single columns values? – oracle

In the last post on this topic it turned out that using an array as a column type needs more space than using a column per value in PostgreSQL. Now I’ll do the same test case in Oracle.
Read More
As with the PostgreSQL test case I’ll create two test tables in Oracle: One using 10 columns and one just using one column of type varray:

SQL> create table single_values ( a number, b number, c number, d number, e number, f number, g number, h number, i number, j number );

Table created.

SQL> create type typ_num_arr as varray(10) of number;
  2  /

Type created.

SQL> create table arr_values ( a typ_num_arr );

Table created.

The same logic will be used to populate the tables:

set timi on
truncate table single_values;
declare
  ln number := 1000;
begin
  for i in 0..1e6
  loop
    insert into single_values (a,b,c,d,e,f,g,h,i,j)
           values (ln,ln,ln,ln,ln,ln,ln,ln,ln,ln);
  end loop;
end;
/
commit;

Table truncated.

Elapsed: 00:00:00.07

PL/SQL procedure successfully completed.

Elapsed: 00:00:29.56

Commit complete.

Elapsed: 00:00:00.00
SQL> 

For the table using the varray:

set timi on
truncate table arr_values;
declare
  ln number := 1000;
  lt typ_num_arr := typ_num_arr(ln,ln,ln,ln,ln,ln,ln,ln,ln,ln);
begin
  for i in 0..1e6
  loop
    insert into arr_values (a)
           values (lt); 
  end loop;
end;
/
commit;

Table truncated.

Elapsed: 00:00:00.01

PL/SQL procedure successfully completed.

Elapsed: 00:00:33.05

Commit complete.

Elapsed: 00:00:00.01

Now, what about the space the two tables have allocated:

SQL> select sum(bytes)/1024/1024 MB from dba_extents where SEGMENT_NAME = 'SINGLE_VALUES';

	MB
----------
	38

Elapsed: 00:00:00.23
SQL> select sum(bytes)/1024/1024 MB from dba_extents where SEGMENT_NAME = 'ARR_VALUES';

	MB
----------
	47

Elapsed: 00:00:00.20
SQL> 

The same behavior as in PostgreSQL. The table with the varray needs more space than the table with the columns per value.

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

Open source Team

Open source Team

Posts published under the Open source Team account are collective contributions by the dbi services open infrastructure consultants. The practice covers open-source databases and platforms, PostgreSQL first of all, along with MySQL, MariaDB, and Linux, from architecture, replication, and high availability to performance tuning and operations under service level agreements, and contributes actively to the PostgreSQL community.View Author posts

721 posts since September 2011

Most read in Database

The Database posts our readers open most: the all-time favourites, and what is trending right now.

Leave a Comment

Your email address will not be published. Required fields are marked *