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" />
......
......@@ -33,29 +33,37 @@
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.tsbDbLink = new System.Windows.Forms.ToolStripButton();
this.tslbDbLinkString = new System.Windows.Forms.ToolStripLabel();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.tbScriptText = new System.Windows.Forms.TextBox();
this.panel1 = new System.Windows.Forms.Panel();
this.label1 = new System.Windows.Forms.Label();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.tbAnlysisResult = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.textBox3 = new System.Windows.Forms.TextBox();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.dgvPlanResult = new System.Windows.Forms.DataGridView();
this.panel3 = new System.Windows.Forms.Panel();
this.label3 = new System.Windows.Forms.Label();
this.tabPage3 = new System.Windows.Forms.TabPage();
this.tbIOResult = new System.Windows.Forms.TextBox();
this.panel4 = new System.Windows.Forms.Panel();
this.label4 = new System.Windows.Forms.Label();
this.toolStrip1.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).BeginInit();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.panel2.SuspendLayout();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dgvPlanResult)).BeginInit();
this.panel3.SuspendLayout();
this.tabPage3.SuspendLayout();
this.panel4.SuspendLayout();
this.SuspendLayout();
//
// toolStrip1
......@@ -67,7 +75,7 @@
this.tslbDbLinkString});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(824, 25);
this.toolStrip1.Size = new System.Drawing.Size(650, 25);
this.toolStrip1.TabIndex = 0;
this.toolStrip1.Text = "toolStrip1";
//
......@@ -100,10 +108,33 @@
this.tslbDbLinkString.Size = new System.Drawing.Size(98, 22);
this.tslbDbLinkString.Text = "·····请先选择数据库·····";
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage3);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 25);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(650, 414);
this.tabControl1.TabIndex = 1;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.splitContainer1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(642, 388);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "基础分析";
this.tabPage1.UseVisualStyleBackColor = true;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 25);
this.splitContainer1.Location = new System.Drawing.Point(3, 3);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
......@@ -113,10 +144,11 @@
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size = new System.Drawing.Size(824, 526);
this.splitContainer1.SplitterDistance = 500;
this.splitContainer1.TabIndex = 1;
this.splitContainer1.Panel2.Controls.Add(this.tbAnlysisResult);
this.splitContainer1.Panel2.Controls.Add(this.panel2);
this.splitContainer1.Size = new System.Drawing.Size(636, 382);
this.splitContainer1.SplitterDistance = 385;
this.splitContainer1.TabIndex = 2;
//
// tbScriptText
//
......@@ -125,7 +157,7 @@
this.tbScriptText.Multiline = true;
this.tbScriptText.Name = "tbScriptText";
this.tbScriptText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tbScriptText.Size = new System.Drawing.Size(500, 497);
this.tbScriptText.Size = new System.Drawing.Size(385, 353);
this.tbScriptText.TabIndex = 0;
//
// panel1
......@@ -134,38 +166,18 @@
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(500, 29);
this.panel1.Size = new System.Drawing.Size(385, 29);
this.panel1.TabIndex = 2;
//
// label1
//
this.label1.Location = new System.Drawing.Point(11, 3);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(188, 23);
this.label1.Size = new System.Drawing.Size(354, 23);
this.label1.TabIndex = 1;
this.label1.Text = "此处输入SQL语句";
this.label1.Text = "请在下方输入SQL语句,并点击【查询效率分析】";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// splitContainer2
//
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.tbAnlysisResult);
this.splitContainer2.Panel1.Controls.Add(this.panel2);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.textBox3);
this.splitContainer2.Panel2.Controls.Add(this.panel3);
this.splitContainer2.Size = new System.Drawing.Size(320, 526);
this.splitContainer2.SplitterDistance = 280;
this.splitContainer2.TabIndex = 0;
//
// tbAnlysisResult
//
this.tbAnlysisResult.Dock = System.Windows.Forms.DockStyle.Fill;
......@@ -173,8 +185,8 @@
this.tbAnlysisResult.Multiline = true;
this.tbAnlysisResult.Name = "tbAnlysisResult";
this.tbAnlysisResult.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.tbAnlysisResult.Size = new System.Drawing.Size(320, 251);
this.tbAnlysisResult.TabIndex = 0;
this.tbAnlysisResult.Size = new System.Drawing.Size(247, 353);
this.tbAnlysisResult.TabIndex = 6;
//
// panel2
//
......@@ -182,8 +194,8 @@
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(320, 29);
this.panel2.TabIndex = 3;
this.panel2.Size = new System.Drawing.Size(247, 29);
this.panel2.TabIndex = 7;
//
// label2
//
......@@ -194,59 +206,114 @@
this.label2.Text = "表或视图的COUNT(*)查询时间";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// textBox3
//
this.textBox3.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox3.Location = new System.Drawing.Point(0, 29);
this.textBox3.Multiline = true;
this.textBox3.Name = "textBox3";
this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox3.Size = new System.Drawing.Size(320, 213);
this.textBox3.TabIndex = 0;
// tabPage2
//
this.tabPage2.Controls.Add(this.dgvPlanResult);
this.tabPage2.Controls.Add(this.panel3);
this.tabPage2.Location = new System.Drawing.Point(4, 22);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(642, 388);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "查询计划";
this.tabPage2.UseVisualStyleBackColor = true;
//
// dgvPlanResult
//
this.dgvPlanResult.AllowUserToAddRows = false;
this.dgvPlanResult.AllowUserToDeleteRows = false;
this.dgvPlanResult.BackgroundColor = System.Drawing.SystemColors.Window;
this.dgvPlanResult.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgvPlanResult.Dock = System.Windows.Forms.DockStyle.Fill;
this.dgvPlanResult.Location = new System.Drawing.Point(3, 32);
this.dgvPlanResult.Name = "dgvPlanResult";
this.dgvPlanResult.RowTemplate.Height = 23;
this.dgvPlanResult.Size = new System.Drawing.Size(636, 353);
this.dgvPlanResult.TabIndex = 9;
//
// panel3
//
this.panel3.Controls.Add(this.label3);
this.panel3.Dock = System.Windows.Forms.DockStyle.Top;
this.panel3.Location = new System.Drawing.Point(0, 0);
this.panel3.Location = new System.Drawing.Point(3, 3);
this.panel3.Name = "panel3";
this.panel3.Size = new System.Drawing.Size(320, 29);
this.panel3.TabIndex = 3;
this.panel3.Size = new System.Drawing.Size(636, 29);
this.panel3.TabIndex = 8;
//
// label3
//
this.label3.Location = new System.Drawing.Point(12, 3);
this.label3.Location = new System.Drawing.Point(11, 3);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(209, 23);
this.label3.Size = new System.Drawing.Size(203, 23);
this.label3.TabIndex = 2;
this.label3.Text = "此处为预留功能区,后续完善";
this.label3.Text = "以下为当前SQL语句的查询计划";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// tabPage3
//
this.tabPage3.Controls.Add(this.tbIOResult);
this.tabPage3.Controls.Add(this.panel4);
this.tabPage3.Location = new System.Drawing.Point(4, 22);
this.tabPage3.Name = "tabPage3";
this.tabPage3.Size = new System.Drawing.Size(642, 388);
this.tabPage3.TabIndex = 2;
this.tabPage3.Text = "IO统计";
this.tabPage3.UseVisualStyleBackColor = true;
//
// tbIOResult
//
this.tbIOResult.Dock = System.Windows.Forms.DockStyle.Fill;
this.tbIOResult.Location = new System.Drawing.Point(0, 29);
this.tbIOResult.Multiline = true;
this.tbIOResult.Name = "tbIOResult";
this.tbIOResult.Size = new System.Drawing.Size(642, 359);
this.tbIOResult.TabIndex = 9;
//
// panel4
//
this.panel4.Controls.Add(this.label4);
this.panel4.Dock = System.Windows.Forms.DockStyle.Top;
this.panel4.Location = new System.Drawing.Point(0, 0);
this.panel4.Name = "panel4";
this.panel4.Size = new System.Drawing.Size(642, 29);
this.panel4.TabIndex = 8;
//
// label4
//
this.label4.Location = new System.Drawing.Point(11, 3);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(460, 23);
this.label4.TabIndex = 2;
this.label4.Text = "以下为当前SQL语句的IO统计,请关注:扫描数、逻辑读、lob逻辑读";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// wDbAnlysis
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(824, 551);
this.Controls.Add(this.splitContainer1);
this.ClientSize = new System.Drawing.Size(650, 439);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.toolStrip1);
this.Name = "wDbAnlysis";
this.Text = "简易SQL效率分析";
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel1.PerformLayout();
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
this.splitContainer1.ResumeLayout(false);
this.panel1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
this.splitContainer2.Panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.splitContainer2)).EndInit();
this.splitContainer2.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.dgvPlanResult)).EndInit();
this.panel3.ResumeLayout(false);
this.tabPage3.ResumeLayout(false);
this.tabPage3.PerformLayout();
this.panel4.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
......@@ -256,19 +323,25 @@
private System.Windows.Forms.ToolStrip toolStrip1;
private System.Windows.Forms.ToolStripButton tsbAnlysis;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripButton tsbDbLink;
private System.Windows.Forms.ToolStripLabel tslbDbLinkString;
private System.Windows.Forms.TabControl tabControl1;
private System.Windows.Forms.TabPage tabPage1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.TextBox tbScriptText;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TabPage tabPage2;
private System.Windows.Forms.TextBox tbAnlysisResult;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TabPage tabPage3;
private System.Windows.Forms.Panel panel3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator1;
private System.Windows.Forms.ToolStripButton tsbDbLink;
private System.Windows.Forms.ToolStripLabel tslbDbLinkString;
private System.Windows.Forms.Panel panel4;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox tbIOResult;
private System.Windows.Forms.DataGridView dgvPlanResult;
}
}
\ No newline at end of file
......@@ -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