1

I currently have stored procedures for Oracle SQL, version 18c, for both inserting and fetching multiple rows of data from one parent table and one child table, being called from my Java Spring Boot application. Everything works fine, but it is extremely slow, for only a few rows of data.

When only inserting 70 records between the two, it takes up to 267 seconds into empty tables. Fetching that same data back out takes about 40 seconds.

Any help would be greatly appreciated or if there is any additional information needed from me.

Below is a cut down and renamed version of my stored procedures for my parent and child tables, actual parent table has 32 columns and child has 11.

PROCEDURE processParentData(
      i_field_one varchar2,
      v_parent_id OUT number) is
      v_new PARENT%ROWTYPE;
    BEGIN
      
    v_new.id := ROW_SEQUENCE.nextval;                                           
    v_new.insert_time := systimestamp;
    v_new.field_one := i_field_one;

    insert into PARENT values v_new;

    v_parent_id := v_new.id;

    END;
    
    PROCEDURE readParentData(
      i_field_one IN varchar2,
      v_parent OUT SYS_REFCURSOR) AS
    BEGIN
      OPEN v_parent FOR select h.* from PARENT h
      where h.field_one = i_field_one;
    END;
    
    PROCEDURE processChild(
      i_field_one varchar2,
      i_parent_id number) is
      v_new CHILD%ROWTYPE;
    BEGIN
      
    v_new.id := ROW_SEQUENCE.nextval;                                           
    v_new.insert_time := systimestamp;

    v_new.field_one := i_field_one;
    v_new.parent_id := i_parent_id;

    insert into CHILD values v_new;

    END;
    
    PROCEDURE readChild(
      i_parent_id IN number,
      v_child OUT SYS_REFCURSOR)  AS
    BEGIN
      OPEN v_child FOR select h.* from CHILD h
      where h.parent_id = i_parent_id;
    END;

For my Java code I am using Spring JDBC. After I get the parent data, I then fetch each child data by looping through the parent data and calling readChild with the parent ID for each.

var simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withCatalogName("PARENT_PACKAGE")
    .withProcedureName("processParentData");

SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
    .addValue("i_field_one", locationId)
    .addValue("v_parent_id", null);
            
Map<String, Object> out = simpleJdbcCall.execute(sqlParameterSource);
var stopId = (BigDecimal) out.get("v_parent_id");
return stopId.longValue();
var simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withCatalogName("PARENT_PACKAGE")
    .withProcedureName("readParentData")
    .returningResultSet("v_parent", BeanPropertyRowMapper.newInstance(Parent.class));

SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
    .addValue("i_field_one", location.getId());
            
Map<String, Object> out = simpleJdbcCall.execute(sqlParameterSource);
return (List<Parent>) out.get("v_parent");

UPDATE 1: As I know and have tested, using the same data and tables, if I use pure JDBC or JPA/Hibernate for inserting and fetching to the tables directly and avoid using stored procedures, then the whole process of inserting and fetching only takes a few seconds.

The issue is, at the company I work at, they have set a policy that all applications going forward are not allowed to have direct read/write access to the database and everything must be done through stored procedures, they say for security reasons. Meaning I need to workout how to do the same thing we have been doing for years with direct read/write access, now with only using Oracle stored procedures.

UPDATE 2: Adding my current Java code for fetching the child data.

for (Parent parent : parents) {
    parent.setChilds(childRepository.readChildByParentId(parent.getId()));
}

public List<Child> readChildByParentId(long parentId) {
        var simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
            .withCatalogName("CHILD_PACKAGE")
            .withProcedureName("readChild")
            .returningResultSet("v_child", BeanPropertyRowMapper.newInstance(Child.class));

        SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
            .addValue("i_parent_id ", parentId);
                    
        Map<String, Object> out = simpleJdbcCall.execute(sqlParameterSource);
        return (List<Child>) out.get("v_child");
}
Brett
  • 363
  • 1
  • 4
  • 11
  • 1
    hi; you could do the whole looping inside SQL itself; rather than bringing them to java and then manipulating here row by row and posting back – Nizam - Madurai Jun 05 '21 at 04:43
  • @NizamMadurai Could you give more details as to how that would be done? I am not sure how I would do that with Java and stored procedures, for both inserting and fetching data. – Brett Jun 05 '21 at 13:28
  • Procedure `readParentData` accesses `PARENT` using field `field_one`. How many rows total are in `PARENT` and does `field_one` have an index? – Matthew McPeak Jun 07 '21 at 14:53
  • Also, your company policy of requiring access through stored packages probably has merit. You should probably get on board with it if you are not already. But you can ask them if they might create a GLOBAL TEMPORARY TABLE or two. These should not present security vulnerabilities and they would add design options for you. That is, you could use bulk JDBC to insert data into a GTT and then invoke a stored procedure that processes that staged data using efficient bulk operations. – Matthew McPeak Jun 07 '21 at 14:54
  • 1
    #1 Oracle procedures and low level configurations some times are tricky. Check this https://stackoverflow.com/questions/41841491/why-oracle-stored-procedure-execution-time-is-greatly-increased-depending-on-how #2 Are you using a localhost database for your tests? If not, you could use docker to have a local environment (app and database) and measure the times. Check this: https://gist.github.com/jrichardsz/0a90cd74f0ed4635721844e5e66d3544#file-oracle-with-docker-md – JRichardsz Jun 07 '21 at 15:33

6 Answers6

1

The problem is that the insert you are trying to perform using the stored procedure is not optimized, because you are calling the database every time you try to insert a row. I strongly recommend you to transform the data to XML (for example, you can also use CSV) and pass it to the procedure, then loop over it and perform the inserts that you need.

Here is an example made using Oracle:

CREATE OR REPLACE PROCEDURE MY_SCHEMA.my_procedure(xmlData clob) IS
begin
    FOR CONTACT IN (SELECT *
                    FROM XMLTABLE(
                            '/CONTACTS/CONTACT' PASSING
                            XMLTYPE(contactes)
                            COLUMNS param_id FOR ORDINALITY
                                ,id NUMBER PATH 'ID'
                                ,name VARCHAR2(100) PATH 'NAME'
                                ,surname VARCHAR2(100) PATH 'SURNAME'
                        ))
    LOOP
        INSERT INTO PARENT_TABLE VALUES CONTACT.id, CONTACT.name, CONTACT.surname;
        
    end loop;
end;

The XML, you can use a String to pass the data to the procedure:

<CONTACTS>
  <CONTACT>
    <ID>1</ID>
    <NAME>Jonh</NAME>
    <SURNAME>Smith</SURNAME>
  </CONTACT>
<CONTACTS>
Alberto Chaves
  • 446
  • 3
  • 8
1

For my Java code I am using Spring JDBC. After I get the parent data, I then fetch each child data by looping through the parent data and calling readChild with the parent ID for each.

Instead of fetching child data in loop, you can modify your procedure to accept list of parent id and return all the data in one call.

It will be helpful if you share spring boot for loop code as well.

Update

Instead of fetching single parent details, you should have update your code like this. Also you have to update your procedure as well.

List<Long> parents = new ArrayList<>();
for (Parent parent : parents) {
    parents.add(parent.getId());
}

You can use java streams but that is secondary things.

Now you have to modify your procedure and method to accept multiple parent ids.

List<Child> children = childRepository.readreadChildByParentId(parents);

public List<Child> readChildByParentId(long parentId) {
            var simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withCatalogName("CHILD_PACKAGE")
                .withProcedureName("readChild")
                .returningResultSet("v_child", BeanPropertyRowMapper.newInstance(Child.class));
    
            SqlParameterSource sqlParameterSource = new MapSqlParameterSource()
                .addValue("i_parent_id ", parentId);
                        
            Map<String, Object> out = simpleJdbcCall.execute(sqlParameterSource);
            return (List<Child>) out.get("v_child");
    }

After having all the children you can set parent children via java code.

P.S. Could you please check if you fetch parents with children if parent is coming from the database?

Pirate
  • 2,886
  • 4
  • 24
  • 42
  • Thanks, I will look into this idea. For now I have updated my question to also include my Java code for getting the child data. – Brett Jun 12 '21 at 16:43
  • from the updated code, i can see that you are fetching child data via loop. Suppose there are 1000 or parents, so you will make 1000 call to db hence degrading the performance. – Pirate Jun 12 '21 at 17:01
1

Your performance problems are probably related with the number of operations performed against the database: you are iterating in Java your collections, and interacting with the database in every iteration. You need to minimize the number of operations performed.

One possible solution can be the use of the standard STRUCT and ARRAY Oracle types. Please, consider for instance the following example:

public static void insertData() throws SQLException {
  DriverManagerDataSource dataSource = ...
  JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  jdbcTemplate.setResultsMapCaseInsensitive(true);
  SimpleJdbcCall insertDataCall = new SimpleJdbcCall(jdbcTemplate)
      .withCatalogName("parent_child_pkg")
      .withProcedureName("insert_data")
      .withoutProcedureColumnMetaDataAccess()
      .useInParameterNames("p_parents")
      .declareParameters(
          new SqlParameter("p_parents", OracleTypes.ARRAY, "PARENT_ARRAY")
      );

  OracleConnection connection = null;

  try {
    connection = insertDataCall
        .getJdbcTemplate()
        .getDataSource()
        .getConnection()
        .unwrap(OracleConnection.class)
    ;

    List<Parent> parents = new ArrayList<>(100);
    Parent parent = null;
    List<Child> chilren = null;
    Child child = null;
    for (int i = 0; i < 100; i++) {
      parent = new Parent();
      parents.add(parent);
      parent.setId((long) i);
      parent.setName("parent-" + i);
      chilren = new ArrayList<>(1000);
      parent.setChildren(chilren);
      for (int j = 0; j < 1000; j++) {
        child = new Child();
        chilren.add(child);
        child.setId((long) j);
        child.setName("parent-" + j);
      }
    }

    System.out.println("Inserting data...");

    StopWatch stopWatch = new StopWatch();
    stopWatch.start("insert-data");
    StructDescriptor parentTypeStructDescriptor = StructDescriptor.createDescriptor("PARENT_TYPE", connection);
    ArrayDescriptor parentArrayDescriptor = ArrayDescriptor.createDescriptor("PARENT_ARRAY", connection);
    StructDescriptor childTypeStructDescriptor = StructDescriptor.createDescriptor("CHILD_TYPE", connection);
    ArrayDescriptor childArrayDescriptor = ArrayDescriptor.createDescriptor("CHILD_ARRAY", connection);

    Object[] parentArray = new Object[parents.size()];
    int pi = 0;
    for (Parent p : parents) {
      List<Child> children = p.getChildren();
      Object[] childArray = new Object[children.size()];
      int ci = 0;
      for (Child c : children) {
        Object[] childrenObj = new Object[2];
        childrenObj[0] = c.getId();
        childrenObj[1] = c.getName();
        STRUCT childStruct = new STRUCT(childTypeStructDescriptor, connection, childrenObj);
        childArray[ci++] = childStruct;
      }

      ARRAY childrenARRAY = new ARRAY(childArrayDescriptor, connection, childArray);

      Object[] parentObj = new Object[3];
      parentObj[0] = p.getId();
      parentObj[1] = p.getName();
      parentObj[2] = childrenARRAY;
      STRUCT parentStruct = new STRUCT(parentTypeStructDescriptor, connection, parentObj);
      parentArray[pi++] = parentStruct;
    }

    ARRAY parentARRAY = new ARRAY(parentArrayDescriptor, connection, parentArray);

    Map in = Collections.singletonMap("p_parents", parentARRAY);

    insertDataCall.execute(in);
    connection.commit();

    stopWatch.stop();

    System.out.println(stopWatch.prettyPrint());
  } catch (Throwable t) {
    t.printStackTrace();
    connection.rollback();
  } finally {
    if (connection != null) {
      try {
        connection.close();
      } catch (Throwable nested) {
        nested.printStackTrace();
      }
    }
  }
}

Where:

CREATE OR REPLACE TYPE child_type AS OBJECT (
  id NUMBER,
  name VARCHAR2(512)
);

CREATE OR REPLACE TYPE child_array 
    AS TABLE OF child_type;

CREATE OR REPLACE TYPE parent_type AS OBJECT (
  id NUMBER,
  name VARCHAR2(512),
  children child_array
);

CREATE OR REPLACE TYPE parent_array 
    AS TABLE OF parent_type;
CREATE SEQUENCE PARENT_SEQ INCREMENT BY 1 MINVALUE 1;

CREATE SEQUENCE CHILD_SEQ INCREMENT BY 1 MINVALUE 1;

CREATE TABLE parent_table (
  id NUMBER,
  name VARCHAR2(512)
);

CREATE TABLE child_table (
  id NUMBER,
  name VARCHAR2(512),
  parent_id NUMBER
);
CREATE OR REPLACE PACKAGE parent_child_pkg AS

  PROCEDURE insert_data(p_parents PARENT_ARRAY);

END;

CREATE OR REPLACE PACKAGE BODY parent_child_pkg AS

  PROCEDURE insert_data(p_parents PARENT_ARRAY) IS
    l_parent_id NUMBER;
    l_child_id NUMBER;
  BEGIN
    FOR i IN 1..p_parents.COUNT LOOP
      SELECT parent_seq.nextval INTO l_parent_id FROM dual;
      
      INSERT INTO parent_table(id, name)
      VALUES(l_parent_id, p_parents(i).name);
      
      FOR j IN 1..p_parents(i).children.COUNT LOOP
        SELECT child_seq.nextval INTO l_child_id FROM dual;
        
        INSERT INTO child_table(id, name, parent_id)
        VALUES(l_child_id, p_parents(i).name, l_parent_id);
        
      END LOOP;
    END LOOP;
  END;

END;

And Parent and Child are simple POJOs:

import java.util.ArrayList;
import java.util.List;

public class Parent {
  private Long id;
  private String name;
  private List<Child> children = new ArrayList<>();

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List<Child> getChildren() {
    return children;
  }

  public void setChildren(List<Child> children) {
    this.children = children;
  }
}
public class Child {
  private Long id;
  private String name;

  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

Please, forgive for the code legibility and incorrect error handling, I will improve the answer later including some information about obtaining the data as well.

jccampanero
  • 50,989
  • 3
  • 20
  • 49
0

The times you mention are horrible indeed. A big boost forward in performance will be to work set based. This means reducing the row by row database calls.

Row by row is synonymous for slow, especially when network round trips are involved.

One call to get the parent. One call to get the set of children and process them. The jdbc fetch size is a nice tunable here. Give it a chance to work for you.

  • Thanks for the reply, but could you give more details of how this could be accomplished using Java/Spring and Oracle stored procedures, for both inserting and fetching data? – Brett Jun 05 '21 at 16:21
  • The stored procedure is not going to help you if you keep calling that row by row. You should try to minimize network round trips. I am not a Java developer so I can’t help you with that. I am a performance analyst and I see this happening a lot. Make the tools work for you. –  Jun 05 '21 at 17:14
  • Then for just the case of creating Oracle stored procedures to do what I need and forgetting about Java all together, would you be able to give more details for that? – Brett Jun 06 '21 at 02:12
  • please share your use case, what do you want to achieve with the help of store procedures. They are used to club database call in single call. – Pirate Jun 11 '21 at 12:20
0

You do not need to use DYNAMIC SQL OPEN v_parent FOR and also it is not clear how the view v_parent is defined. Try to check exec plan of this query:

 FOR select h.* from PARENT h where h.field_one = ?;

Usually returning recordset via SYS_REFCURSOR increases performance when you return more (let's say) than 10K records.

ibre5041
  • 4,903
  • 1
  • 20
  • 35
0

The SimpleJdbcCall object can be reused in your scenario as only the parameters changes. The SimpleJdbcCall object compiles the jdbc statement on the first invocation. It does some meta-data fetching and it interacts with the Database for that. So, having separate objects would mean fetching same metadata that many times which is not needed.

So, I suggest to initialise all the 4 SimpleJdbcCall objects in the very beginning and then work with them.

var insertParentJdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withCatalogName("PARENT_PACKAGE")
    .withProcedureName("processParentData");

var readParentJdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withCatalogName("PARENT_PACKAGE")
    .withProcedureName("readParentData")
    .returningResultSet("v_parent", BeanPropertyRowMapper.newInstance(Parent.class));

var insertChildJdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withCatalogName("CHILD_PACKAGE")
    .withProcedureName("processChildData");
    
var readChildJdbcCall = new SimpleJdbcCall(jdbcTemplate)
    .withCatalogName("CHILD_PACKAGE")
    .withProcedureName("readChild")
    .returningResultSet("v_child", BeanPropertyRowMapper.newInstance(Child.class));
SKumar
  • 1,940
  • 1
  • 7
  • 12