Im trying to execute a query with some parameters. If I execute this sql in pl/sql developer (oracle) with literal parameters It runs in 1 second. If I execute this sql using java, jdbc driver and literal parameters It runs very fast too. For example using this code:
String query = "select ID, (a lot of other columns)"
+ " from VCP_TIT_LIQDC"
+ " WHERE"
+ " ((123 is not null and id_tit = 123 and exists"
+ " (select 1 from sd_dual"
+ " where ((exists"
+ " (select 1"
+ " from fn_config_usr cfg"
+ " where cfg.nome_usr_bd = 'NAME' and cfg.ind_acesso_qualquer_site = 'S') or exists"
+ " (select 1"
+ " from fn_rel_usr_site rel"
+ " where rel.nome_usr_bd = 'NAME' and rel.cod_site = decode(vcp_tit_liqdc.id_empresa_lider, null, vcp_tit_liqdc.cod_site, vcp_tit_liqdc.cod_site_empresa_lider)))))) OR"
+ " (123 is null and (28 is null or"
+ " ((id_empresa = nvl(28 ,id_empresa) and id_empresa_lider is null) OR"
+ " id_empresa_lider = 28 )) AND decode (id_empresa_lider, null, cod_site, cod_site_empresa_lider) in"
+ " (select nvl (null , site.cod_site)"
+ " from fn_site site, fn_rel_usr_site rus, fn_config_usr cfg"
+ " where cfg.nome_usr_bd = 'NAME' and rus.nome_usr_bd (+) = cfg.nome_usr_bd and site.cod_site = decode (cfg.ind_acesso_qualquer_site, 'S', site.cod_site, rus.cod_site) group by site.cod_site))) AND (usr_cadastro = 'NAME' or ( id_tit in"
+ " (select ac.id_tit"
+ " from cp_titulo_acesso ac, cp_usr_acesso_janela jan"
+ " where ac.id_acesso = jan.id_acesso and 'WINDOW' = nvl(jan.nome_janela_sis, 'WINDOW' ) and jan.nome_usr_bd = 'NAME' ))) ORDER BY ID_TIT DESC";
PreparedStatement p = connection.prepareStatement(query);
ResultSet rs = p.executeQuery();
But if I set the parameters using "?" (binding) It takes 10 minutes or more to run. For example in code:
String query = "select ID, (a lot of other columns)"
+ " from VCP_TIT_LIQDC"
+ " WHERE"
+ " ((? is not null and id_tit = ? and exists"
+ " (select 1 from sd_dual"
+ " where ((exists"
+ " (select 1"
+ " from fn_config_usr cfg"
+ " where cfg.nome_usr_bd = ? and cfg.ind_acesso_qualquer_site = 'S') or exists"
+ " (select 1"
+ " from fn_rel_usr_site rel"
+ " where rel.nome_usr_bd = ? and rel.cod_site = decode(vcp_tit_liqdc.id_empresa_lider, null, vcp_tit_liqdc.cod_site, vcp_tit_liqdc.cod_site_empresa_lider)))))) OR"
+ " (? is null and (? is null or"
+ " ((id_empresa = nvl(? ,id_empresa) and id_empresa_lider is null) OR"
+ " id_empresa_lider = ? )) AND decode (id_empresa_lider, null, cod_site, cod_site_empresa_lider) in"
+ " (select nvl (? , site.cod_site)"
+ " from fn_site site, fn_rel_usr_site rus, fn_config_usr cfg"
+ " where cfg.nome_usr_bd = ? and rus.nome_usr_bd (+) = cfg.nome_usr_bd and site.cod_site = decode (cfg.ind_acesso_qualquer_site, 'S', site.cod_site, rus.cod_site) group by site.cod_site))) AND (usr_cadastro = ? or ( id_tit in"
+ " (select ac.id_tit"
+ " from cp_titulo_acesso ac, cp_usr_acesso_janela jan"
+ " where ac.id_acesso = jan.id_acesso and ? = nvl(jan.nome_janela_sis, ? ) and jan.nome_usr_bd = ? ))) ORDER BY ID_TIT DESC";
PreparedStatement p = connection.prepareStatement(query);
p.setInt(1, 123);
p.setInt(2, 123);
p.setString(3, "NAME");
p.setString(4, "NAME");
p.setInt(5, 123);
p.setInt(6, 28);
p.setInt(7, 28);
p.setInt(8, 28);
p.setString(9, null);
p.setString(10, "NAME");
p.setString(11, "NAME");
p.setString(12, "WINDOW");
p.setString(13, "WINDOW");
p.setString(14, "NAME");
ResultSet rs = p.executeQuery();
Is there any way to resolve this problem? Because the sql is the same in each case.
I am using 10g database and tested this versions of jdbc: 10.2.0.1.0, 11.2.0.2.0 and 12.1.0.2.0.