Search code examples
sql-serverdatabasejava-8simplejdbccall

Optimization of execution of stored procedure in MSSQL server with SimpleJdbcCall


I have the code that executes some stored procedure. mssql-jdbc driver to connect to Microsoft SQL Server database is used.

Here is simplified code

SimpleJdbcCall call = simpleJdbcCallFactory.create(jdbcTemplate)
    .withSchemaName(SCHEMA)
    .withProcedureName(SP)
    .declareParameters(
        new SqlParameter("Id1", Types.INTEGER),
        new SqlParameter("Id2", Types.TINYINT),
        new SqlParameter("Id3", Types.INTEGER))
    .returningResultSet("result", (rs, rowNum) -> MappingObject.builder()
            .id(rs.getInt("Id"))
            .date(rs.getTimestamp("Date"))
            // .......
            .build());

MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("Id1", 1);
params.addValue("Id2", 2);
params.addValue("Id3", 3);

Map queryRes = call.execute(params);  

When I run this I see in profiler 3 calls:

  • exec sp_executesql N'EXEC sp_stored_procedures @P0, @P1, @P2 ',N'@P0 nvarchar(4000),@P1 nvarchar(4000),@P2 nvarchar(4000)',N'MY_SP_NAME',N'MY_NAMESPACE',NULL It seems that it checks that stored procedure exists
  • exec sp_executesql N'EXEC sp_sproc_columns_100 @P0, @P1, @P2,@P3,@P4 ',N'@P0 nvarchar(4000),@P1 nvarchar(4000),@P2 nvarchar(4000),@P3 nvarchar(4000),@P4 nvarchar(4000)',N'MY_SP_NAME,N'MY_NAMESPACE',NULL,NULL,N'3' it gets a column information of this sp
  • exec sp_executesql N'EXEC MY_NAMESPACE.MY_SP_NAME @P0, @P1, @P2 ',N'@P0 int,@P1 tinyint,@P2 int',1,2,3 and finally it executes my stored procedure

I see in profiler 3 sql calls every time I run this one stored procedure. I'd like to make only one call, is it possible with SimpleJdbcCall?


Solution

  • I've just found an answer, maybe this will be useful to somebody. All we need to avoid 3 calls is withoutProcedureColumnMetaDataAccess() for SimpleJdbcCall.

    Like this

    SimpleJdbcCall getQACall = simpleJdbcCallFactory.create(jdbcTemplate)
        .withSchemaName(SCHEMA_ARTICLE)
        .withProcedureName(SP_GET_ALL_QUESTIONS_ANSWERS)
        .withoutProcedureColumnMetaDataAccess()
        .declareParameters(
        // etc