java - Get the query plan using jdbc PreparedStatement on sql server -
using statment, resultset.getobject returns query plan xml
connection conn = getconnection(); string query = " set showplan_xml on "; statement st = conn.createstatement(); boolean execute=st.execute(query); log.info("execute status {} " , execute); query = " select atmprofiles.termid columnid, atmprofiles.termid columnname atmprofiles (nolock) " + " authprocessname = 'atmst' " + "order atmprofiles.termid "; resultset rs = st.executequery(query); while(rs.next()) { object object = rs.getobject(1); log.info("query plan {} ", object); }
but if execute same through preparedstatement, returns actual result insteadof queryplan
connection conn = getconnection(); string query = " set showplan_xml on "; preparedstatement ps = conn.preparestatement(query); boolean execute = ps.execute(); log.info("execute status {} " , execute); query = " select atmprofiles.termid columnid, atmprofiles.termid columnname atmprofiles (nolock) " + " authprocessname = 'atmst' " + "order atmprofiles.termid "; ps=conn.preparestatement(query); execute=ps.execute(); log.info("execute status {} " , execute); resultset rs = ps.getresultset(); while(rs.next()) { object object = rs.getobject(1); // here returns selected object log.info("query plan {} ", object); }
any idea acheive via preparedstatement.
i haven't found reference why executing set showplan_xml on
prepared statement not work; however, should desired results when run statement directly , actual query prepared statement. in code:
connection conn = getconnection(); string showplanquery = "set showplan_xml on"; statement st = conn.createstatement(); st.execute(showplanquery); string actualquery = "select atmprofiles.termid atmprofiles (nolock) "; preparedstatement ps=conn.preparestatement(actualquery); ps.execute(); resultset rs = ps.getresultset(); while(rs.next()) { object object = rs.getobject(1); // should log query plan log.info("query plan {} ", object); }
hope helps.
Comments
Post a Comment