Commit a9b014e5 authored by tr's avatar tr

1、SQL简易分析:增加执行计划和IO计数

parent c1c0f6d7
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//
using Tools.MyUtils;
namespace Tools.MyData
{
public class AnlysisHelper : IDisposable
{
public void Dispose()
{
this.Dispose();
}
public AnlysisHelper()
{ }
public AnlysisResult ExecSQL(string link, string sql)
{
SqlConnection conn = null;
SqlTransaction tran = null;
SqlDataAdapter ada = null;
SqlCommand cmd = null;
DataSet ds = new DataSet();
try
{
var anlysisSql = $@"
SET STATISTICS IO ON
SET STATISTICS PROFILE ON
{sql}
SET STATISTICS IO OFF
SET STATISTICS PROFILE OFF
";
conn = new SqlConnection(link);
conn.Open();
conn.InfoMessage += Conn_InfoMessage;
tran = conn.BeginTransaction(); //开启事务
cmd = new SqlCommand(anlysisSql, conn, tran);
ada = new SqlDataAdapter(cmd);
ada.Fill(ds);
}
catch (Exception ex)
{
if (tran != null)
tran.Rollback();
//
Utils.Tip(ex.Message);
}
finally
{
if (conn != null && conn.State == ConnectionState.Open)
conn.Close();
}
return new AnlysisResult() {
data = ds,
Info = sb.ToString(),
};
}
private StringBuilder sb = new StringBuilder();
private void Conn_InfoMessage(object sender, SqlInfoMessageEventArgs e)
{
sb.Append($"{e.Message}\r\n");
}
}
public class AnlysisResult
{
public DataSet data { get; set; }
public string Info { get; set; }
}
}
......@@ -86,6 +86,7 @@
<Compile Include="MyCommon\wEditBasic.Designer.cs">
<DependentUpon>wEditBasic.cs</DependentUpon>
</Compile>
<Compile Include="MyData\AnlysisHelper.cs" />
<Compile Include="MyData\DBHelper.cs" />
<Compile Include="MyData\SqlHelper.cs" />
<Compile Include="MyUtils\DrawHelper.cs" />
......
This diff is collapsed.
......@@ -70,6 +70,12 @@ namespace Tools.UI
}
//显示结果
tbAnlysisResult.Text = string.Join("\r\n", result);
//获取查询计划和IO计数
AnlysisHelper anlysis = new AnlysisHelper();
AnlysisResult anlysisResult = anlysis.ExecSQL(this._db._link, text);
this.dgvPlanResult.DataSource = anlysisResult.data.Tables[1];
this.tbIOResult.Text = anlysisResult.Info;
}
catch (Exception ex)
{
......@@ -100,7 +106,7 @@ namespace Tools.UI
{
var result = new List<string>();
//只按常用连接字符分段
var partList = text.Split(new string[] { " from ", " left join ", " inner join " }, StringSplitOptions.RemoveEmptyEntries).ToList();
var partList = text.Split(new string[] { "from ", "left join ", "inner join " }, StringSplitOptions.RemoveEmptyEntries).ToList();
partList.RemoveAt(0); //剔除第1个,第一个为select
foreach (var s in partList)
{
......@@ -114,20 +120,5 @@ namespace Tools.UI
return result;
}
/*
* 下一步功能规划:
*
* 1、保留SQL录入区域,其他区域重构。去除Count(*)执行测试区域
*
* 2、执行时获取执行计划
*
* 3、读取IO数量
*
* 4、读取执行时间(可以基于数据库,也可基于程序)
*
*/
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment