简体   繁体   English

是否可以在sql server上使用jdbc获取查询计划?

[英]is it possible to get the query plan out using jdbc on sql server?

I am using the JTDS driver and I'd like to make sure my java client is receiving the same query plan as when I execute the SQL in Mgmt studio, is there a way to get the query plan (ideally in xml format)? 我正在使用JTDS驱动程序,我想确保我的java客户端收到与在Mgmt studio中执行SQL时相同的查询计划,有没有办法获得查询计划(理想情况下是xml格式)?

basically, I'd like the same format output as 基本上,我想要相同的格式输出

set showplan_xml on 

in management studio. 在管理工作室。 Any ideas? 有任何想法吗?

Some code for getting the plan for a session_id 一些用于获取session_id计划的代码

SELECT usecounts, cacheobjtype,
  objtype, [text], query_plan
FROM sys.dm_exec_requests req, sys.dm_exec_cached_plans P
  CROSS APPLY
    sys.dm_exec_sql_text(plan_handle)
  CROSS APPLY
    sys.dm_exec_query_plan(plan_handle)    
WHERE cacheobjtype = 'Compiled Plan'
    AND [text] NOT LIKE '%sys.dm_%'
    --and text like '%sp%reassign%'
    and p.plan_handle = req.plan_handle
    and req.session_id = 70 /** <-- your sesssion_id here **/
  1. Identify your Java session id. 确定您的Java会话ID。 Print @@SPID from java or use SSMS and look into sys.dm_exec_sessions and/or sys.dm_exec_connections for your Java client session (it can be identified by program_name , host_process_id , client_net_address etc). 从java打印@@SPID或使用SSMS,查看Java客户端会话的sys.dm_exec_sessions和/或sys.dm_exec_connections (可以通过program_namehost_process_idclient_net_address等标识)。
  2. Execute your statement. 执行你的陈述。 Look in sys.dm_exec_requests for the session_id found at 1. sys.dm_exec_requests中查找在1处找到的session_id
  3. Extract the plan using sys.dm_exec_query_plan from the plan_handle found at 2. 使用位于2处的plan_handle sys.dm_exec_query_plan提取计划。
  4. Save the plan as a .sqlplan file and open it in SSMS 将计划保存为.sqlplan文件并在SSMS中打开它

Alternatively you can use the Profiler, attach the profiler to server and capture the Showplan XML event. 或者,您可以使用Profiler,将分析器附加到服务器并捕获Showplan XML事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM