Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
F
Ftt_Common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Ftt_Common
Commits
a9b014e5
Commit
a9b014e5
authored
Mar 31, 2021
by
tr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1、SQL简易分析:增加执行计划和IO计数
parent
c1c0f6d7
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
232 additions
and
84 deletions
+232
-84
AnlysisHelper.cs
Tools/MyData/AnlysisHelper.cs
+83
-0
Tools.csproj
Tools/Tools.csproj
+1
-0
wDbAnlysis.Designer.cs
Tools/UI/wDbAnlysis.Designer.cs
+141
-68
wDbAnlysis.cs
Tools/UI/wDbAnlysis.cs
+7
-16
No files found.
Tools/MyData/AnlysisHelper.cs
0 → 100644
View file @
a9b014e5
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
;
}
}
}
Tools/Tools.csproj
View file @
a9b014e5
...
@@ -86,6 +86,7 @@
...
@@ -86,6 +86,7 @@
<Compile
Include=
"MyCommon\wEditBasic.Designer.cs"
>
<Compile
Include=
"MyCommon\wEditBasic.Designer.cs"
>
<DependentUpon>
wEditBasic.cs
</DependentUpon>
<DependentUpon>
wEditBasic.cs
</DependentUpon>
</Compile>
</Compile>
<Compile
Include=
"MyData\AnlysisHelper.cs"
/>
<Compile
Include=
"MyData\DBHelper.cs"
/>
<Compile
Include=
"MyData\DBHelper.cs"
/>
<Compile
Include=
"MyData\SqlHelper.cs"
/>
<Compile
Include=
"MyData\SqlHelper.cs"
/>
<Compile
Include=
"MyUtils\DrawHelper.cs"
/>
<Compile
Include=
"MyUtils\DrawHelper.cs"
/>
...
...
Tools/UI/wDbAnlysis.Designer.cs
View file @
a9b014e5
This diff is collapsed.
Click to expand it.
Tools/UI/wDbAnlysis.cs
View file @
a9b014e5
...
@@ -70,6 +70,12 @@ namespace Tools.UI
...
@@ -70,6 +70,12 @@ namespace Tools.UI
}
}
//显示结果
//显示结果
tbAnlysisResult
.
Text
=
string
.
Join
(
"\r\n"
,
result
);
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
)
catch
(
Exception
ex
)
{
{
...
@@ -100,7 +106,7 @@ namespace Tools.UI
...
@@ -100,7 +106,7 @@ namespace Tools.UI
{
{
var
result
=
new
List
<
string
>();
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
partList
.
RemoveAt
(
0
);
//剔除第1个,第一个为select
foreach
(
var
s
in
partList
)
foreach
(
var
s
in
partList
)
{
{
...
@@ -114,20 +120,5 @@ namespace Tools.UI
...
@@ -114,20 +120,5 @@ namespace Tools.UI
return
result
;
return
result
;
}
}
/*
* 下一步功能规划:
*
* 1、保留SQL录入区域,其他区域重构。去除Count(*)执行测试区域
*
* 2、执行时获取执行计划
*
* 3、读取IO数量
*
* 4、读取执行时间(可以基于数据库,也可基于程序)
*
*/
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment