Commit ef527f7e authored by tr's avatar tr

1、增加SQL效率分析工具

parent 2f1a3a52
No preview for this file type
No preview for this file type
......@@ -6,4 +6,10 @@
<UserId>sa</UserId>
<Pwd>sa</Pwd>
</links>
<links>
<Link>192.168.1.204</Link>
<DbName>FTT_PDM</DbName>
<UserId>sa</UserId>
<Pwd>sa</Pwd>
</links>
</DocumentElement>
\ No newline at end of file
......@@ -100,6 +100,16 @@ namespace Tools.Properties {
}
}
/// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary>
internal static System.Drawing.Bitmap 分析 {
get {
object obj = ResourceManager.GetObject("分析", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary>
/// 查找 System.Drawing.Bitmap 类型的本地化资源。
/// </summary>
......
......@@ -130,6 +130,9 @@
<data name="使用说明" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\使用说明.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="分析" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\分析.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="实体类" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\实体类.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
......
......@@ -86,6 +86,12 @@
<Compile Include="MyUtils\wMsgBox.Designer.cs">
<DependentUpon>wMsgBox.cs</DependentUpon>
</Compile>
<Compile Include="UI\wDbAnlysis.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="UI\wDbAnlysis.Designer.cs">
<DependentUpon>wDbAnlysis.cs</DependentUpon>
</Compile>
<Compile Include="UI\wDBTools.cs">
<SubType>Form</SubType>
</Compile>
......@@ -120,6 +126,9 @@
<DependentUpon>Resources.resx</DependentUpon>
<DesignTime>True</DesignTime>
</Compile>
<EmbeddedResource Include="UI\wDbAnlysis.resx">
<DependentUpon>wDbAnlysis.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="UI\wDBTools.resx">
<DependentUpon>wDBTools.cs</DependentUpon>
</EmbeddedResource>
......@@ -186,5 +195,8 @@
<Name>UpdateExecuter</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="Resources\分析.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
This diff is collapsed.
using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//
using Tools.MyData;
using Tools.MyUtils;
using System.Collections.Generic;
namespace Tools.UI
{
public partial class wDbAnlysis : MyCommon.wBasic
{
/// <summary>
/// 数据库访问对象
/// </summary>
SmpDB _db = new SmpDB();
/// <summary>
/// 数据库链接
/// </summary>
string _link = "";
public wDbAnlysis()
{
InitializeComponent();
}
private void wDbAnlysis_Load(object sender, EventArgs e)
{
//工具栏-事件
this.tsbAnlysis.Click += TsbAnlysis_Click;
this.tsbDbLink.Click += TsbDbLink_Click;
}
/// <summary>
/// 事件:查询效率分析
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TsbAnlysis_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this._link))
{
Utils.Tip("请先选择数据库!");
return;
}
Stopwatch stopwatch = new Stopwatch();
try
{
var text = this.tbScriptText.Text.Trim().ToLower();
var nameList = this.GetScriptTableNameList(text);
var result = new List<string>();
for (int i = 0; i < nameList.Count; i++)
{
var objName = nameList[i];
var anlysisSql = $@"
IF EXISTS(SELECT 1 FROM sys.objects WHERE name = '{objName}')
BEGIN
SELECT COUNT(*) FROM {objName}
END";
stopwatch.Restart();
this._db.ExecNonSql(this._link, new List<string> { anlysisSql });
stopwatch.Stop();
result.Add($"{stopwatch.ElapsedMilliseconds / 1000F}秒\t{objName}");
}
//显示结果
tbAnlysisResult.Text = string.Join("\r\n", result);
}
catch (Exception ex)
{
Utils.Tip("性能分析出错!" + ex.Message);
}
}
/// <summary>
/// 事件:数据库
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TsbDbLink_Click(object sender, EventArgs e)
{
var w = new wDbSetting();
if (w.ShowDialog() == DialogResult.OK)
{
this._link = w.LinkModel.LinkString;
this.tslbDbLinkString.Text = w.LinkModel.ToString();
}
}
/// <summary>
/// 获取SQL脚本中的表名称
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private List<string> GetScriptTableNameList(string text)
{
var result = new List<string>();
//只按常用连接字符分段
var partList = text.Split(new string[] { "from", "left join", "inner join" }, StringSplitOptions.RemoveEmptyEntries).ToList();
partList.RemoveAt(0); //剔除第1个,第一个为select
foreach (var s in partList)
{
//先初略按照常见空白字符区分
var objName = s.Trim().Split(new char[] { ' ', '\t', '\r', '\n' })[0]; //第一个为表或视图名
//去重;临时表和带变量的不处理;忽略带括号的子查询或表值函数
if (result.IndexOf(objName) >= 0 || objName.Contains("@") || objName.Contains("#") || objName.Contains("(") || objName.Contains(")"))
continue;
result.Add(objName);
}
return result;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="toolStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>
\ No newline at end of file
......@@ -20,6 +20,7 @@ namespace Tools
case "UpdateExecuter": result = new UpdateExecuter.frmMain(); break;
case "PatchPack": result = new Ftt.PatchPack.FrmMain(); break;
case "DbDevelopTool": result = new UI.wDBTools(); break;
case "DbAnlysis": result = new UI.wDbAnlysis(); break;
default: break;
}
......
......@@ -33,6 +33,7 @@
this.btnPatchPack = new System.Windows.Forms.Button();
this.btnDbDevelopTool = new System.Windows.Forms.Button();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.btnDbAnlysis = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnUpdateExecuter
......@@ -68,12 +69,24 @@
this.btnDbDevelopTool.Text = "数据库开发工具";
this.btnDbDevelopTool.UseVisualStyleBackColor = false;
//
// btnDbAnlysis
//
this.btnDbAnlysis.BackColor = System.Drawing.SystemColors.Control;
this.btnDbAnlysis.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
this.btnDbAnlysis.Location = new System.Drawing.Point(58, 142);
this.btnDbAnlysis.Name = "btnDbAnlysis";
this.btnDbAnlysis.Size = new System.Drawing.Size(135, 25);
this.btnDbAnlysis.TabIndex = 3;
this.btnDbAnlysis.Text = "简易SQL效率分析";
this.btnDbAnlysis.UseVisualStyleBackColor = false;
//
// wMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Window;
this.ClientSize = new System.Drawing.Size(257, 164);
this.ClientSize = new System.Drawing.Size(257, 198);
this.Controls.Add(this.btnDbAnlysis);
this.Controls.Add(this.btnDbDevelopTool);
this.Controls.Add(this.btnPatchPack);
this.Controls.Add(this.btnUpdateExecuter);
......@@ -90,5 +103,6 @@
private System.Windows.Forms.Button btnPatchPack;
private System.Windows.Forms.Button btnDbDevelopTool;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.Button btnDbAnlysis;
}
}
\ No newline at end of file
......@@ -28,6 +28,7 @@ namespace Tools
this.btnUpdateExecuter.Click += Button_Click;
this.btnPatchPack.Click += Button_Click;
this.btnDbDevelopTool.Click += Button_Click;
this.btnDbAnlysis.Click += Button_Click;
}
private void Button_Click(object sender, EventArgs e)
......
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