2022年3月15日 星期二

[SQL] 找出執行時間最長的10條SQL

 

如果想對SQL作篩選,可將

not like '%fetch%'  換成  like '%user%'就可以找出SQL語句中含有user關鍵字的SQL

原文:

http://blog.csdn.net/silvanus/article/details/8424227


SELECT top 10
	(total_elapsed_time / execution_count)/1000 N'平均時間ms'
	,total_elapsed_time/1000 N'總花費時間ms'
	,total_worker_time/1000 N'所用的CPU總時間ms'
	,total_physical_reads N'物理讀取總次數'
	,total_logical_reads/execution_count N'每次邏輯讀次數'
	,total_logical_reads N'邏輯讀取總次數'
	,total_logical_writes N'邏輯寫入總次數'
	,execution_count N'執行次數'
	,creation_time N'語句編譯時間'
	,last_execution_time N'上次執行時間'
	,SUBSTRING(
		st.text, 
		(qs.statement_start_offset/2) + 1, 
		(
			(CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2
		) + 1
	) N'執行語句'
	,qp.query_plan
FROM
sys.dm_exec_query_stats AS qs CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) st CROSS APPLY sys.dm_exec_query_plan(qs.plan_handle) qp
WHERE
	SUBSTRING(
		st.text, 
		(qs.statement_start_offset/2) + 1,
		(
			(CASE statement_end_offset WHEN -1 THEN DATALENGTH(st.text) ELSE qs.statement_end_offset END - qs.statement_start_offset)/2
		) + 1
	) not like '%fetch%'
ORDER BY
	total_elapsed_time / execution_count DESC;










2022年3月14日 星期一

[.NET Core] 解決MVC網頁服務的dll常常被IIS咬住

 

在開發.Net Core MVC時,因為專案有多個web和api服務依附在iis上做運行,會需要邊跑網頁邊開發,因此常發生project編譯出來的dll被iis咬住,這時候就得手動重啟iis,但等IIS重啟實在太慢了


Severity Code Description Project File Line Suppression State

Error MSB3027 Could not copy "******\bin\Debug\net6.0\******.dll" to "bin\Debug\net6.0\*****.dll". Exceeded retry count of 10. Failed. The file is locked by: "IIS Worker Process (6404)" Company C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets\


無法將 "****\bin\Debug\net6.0\****.dll" 複製到 "bin\Debug\net6.0\****.dll"。已超過重試次數 10。失敗。檔案鎖定者: "w3wp.exe (6404)" Company C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets 4967


C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(4967,5): error MSB3021: 無法將檔案 "****\bin\Debug\net6.0\****.dll" 複製到 "bin\Debug\net6.0\****.dll"。由於另一個處理序正在使用檔案 'bin\Debug\net6.0\****.dll',所以無法存取該檔案。





這邊提供比較快速的方法


1. 建立一個iisrestart1.bat批次執行檔

指令內容如下

C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:"WebAPIPool"

C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:"XXXwebPool"


2. 針對bat 批次檔建立捷徑,接著在捷徑的"內容"中調整"目標"值

C:\Windows\System32\cmd.exe /C "C:\Users\XXXX\Documents\iisrestart1.bat"




3. 釘選到工作列,即可完成



之後只要執行批次檔就可以快速釋放IIS  POOL,進而能順利做編譯,若在DEBUG模式下沒有反應時,則需整個重啟IIS或是砍掉w3wp執行緒



* 指令筆記:


//砍掉所有w3wp執行緒

taskkill /F /IM w3wp.exe /T


//回收指定的IIS Pool

C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:"WebAPIPool"

C:\Windows\System32\inetsrv\appcmd.exe recycle apppool /apppool.name:"XXXwebPool"

2022年1月13日 星期四

[工具] WebService測試工具SoapUI

 

 關於使用SoapUI來測試WebService的筆記





貼上網址,後一定要加上 ?WSDL







 

<![CDATA[   ]]>包覆電文內容,但頭尾都不能換行哦


 






2021年12月21日 星期二

[.net] 檢查Server安裝.net 版本

 


檢查server上安裝的.net framework版本



cd C:\Windows\System32

dir %WINDIR%\Microsoft.Net\Framework\v* O-N /B




檢查server上安裝的.net core版本

dotnet --list-sdks




參考 https://docs.microsoft.com/zh-tw/dotnet/core/install/how-to-detect-installed-versions?pivots=os-windows



2021年9月1日 星期三

[MSSQL] 預存程序引用DLL參考



*在組件中加入DLL參考



*可將DLL檔案轉成bytes 直接加入參考



CREATE ASSEMBLY SQLCom

FROM 0x4D5A900003.................

WITH PERMISSION_SET = SAFE;



DLL to bytes string 參考

https://stackoverflow.com/questions/2885335/clr-sql-assembly-get-the-bytestream





 *DLL程式碼


    [Microsoft.SqlServer.Server.SqlFunction]

    public static string GZip(string rawString)

    {

       ..................

            return (string)(Convert.ToBase64String(zippedData));

    }








    * 於資料新增 擴充的 預存程序

    CREATE FUNCTION dbo.GZip(@src nvarchar(MAX))

    RETURNS nvarchar(MAX)

    EXTERNAL NAME SQLCom.UserDefinedFunctions.GZip





*SQL指令
    SELECT dbo.GZip(S_String)




2021年1月25日 星期一

[C#] 利用svcutil.exe下指令產生WCF client端的程式碼

 


在做異質系統間接時,若是透過WCF連線,此時專案就必須加入服務參考,但開發本機端若因為防火牆因素無法直接連到對方的WCF服務來取的服務參考,但目標機器又無法安裝VS2019開發工具,這時可在目標機器上安裝SDK,並利用svcutil.exe來下指令產生clinet端程式碼


以下兩道指令

svcutil.exe /t:metadata http://******/******/MsgService.svc

svcutil *.wsdl *.xsd /language:cs /out:MsgService.cs /config:app.config




2020年6月2日 星期二

[win10] 在Windows 10中以 系統管理員身分

擷取自:https://walker-a.com/archives/5966

參考:https://marcus116.blogspot.com/2018/12/how-to-run-visual-studio-as-administrator-by-default.html




方法:捷徑上指定系統管理員身分為預設的執行權限
在捷徑上按下滑鼠右鍵並選擇「內容」。

【捷徑】分頁畫面上按下〔進階〕。
在〔進階〕內容的畫面上勾選「以系統管理員身分執行」,並按下〔確定〕來離開。


如果出現以下拒絕存取的對話框,請點選〔繼續〕按鈕來完成。


你也可以使用【相容性】分頁上的「以系統管理員身分執行此程式」。

2020年1月3日 星期五

[windows] 控制win10更新時間或停止更新


參考 https://www.bnext.com.tw/article/48840/turn-down-win-10-update

為了掌控windows自動更新自動重開機,必須設定的



在搜尋框內輸入「編輯群組原則」
透過左側的目錄樹轉到「電腦設定 – 系統管理範本 - Windows 元件 - Windows Update」找到「設定自動更新」選項














2019年7月29日 星期一

[windows] 允許多人遠端磚面連線


轉自:https://is.gd/iA0dzF



預設情況下,同一個帳號,只能一個連線,如果建立新的連線,舊的連線會被斷線

以下是開放的方式:

執行 【gpedit.msc】



【電腦設定】->【系統管理範本】->【Windows元件】

>【遠端桌面服務】->【遠端桌面工作階段主機】->【連線】

在右邊的視窗,找到【限制遠端桌面服務的使用者只能使用一個遠端桌面服務工作階段】,預設是【尚未設定】

改成【已停用】




2019年4月4日 星期四

[C#] 用CLR讓 SQL 可以用GZIP壓縮字串


C#  程式碼







     
        /// <summary>
        /// 将传入字符串以GZip算法压缩后,返回Base64编码字符
        /// </summary>
        /// <param name="rawString">需要压缩的字符串</param>
        /// <returns>压缩后的Base64编码的字符串</returns>
        public static string GZip(string rawString)
        {
            return rawString;   //off test
            if (string.IsNullOrEmpty(rawString) || rawString.Length == 0)
            {
                return "";
            }
            else
            {
                byte[] rawData = System.Text.Encoding.UTF8.GetBytes(rawString.ToString());
                byte[] zippedData = GZip_Compress(rawData);
                return (string)(Convert.ToBase64String(zippedData));
            }

        }
        /// <summary>
        /// GZip压缩
        /// </summary>
        /// <param name="rawData"></param>
        /// <returns></returns>
        public static byte[] GZip_Compress(byte[] rawData)
        {
            MemoryStream ms = new MemoryStream();
            GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Compress, true);
            compressedzipStream.Write(rawData, 0, rawData.Length);
            compressedzipStream.Close();
            return ms.ToArray();
        }
        /// <summary>
        /// 将传入的二进制字符串资料以GZip算法解压缩
        /// </summary>
        /// <param name="zippedString">经GZip压缩后的二进制字符串</param>
        /// <returns>原始未压缩字符串</returns>
        public static string UnGZip(string zippedString)
        {
            return zippedString;   //off test
            if (string.IsNullOrEmpty(zippedString) || zippedString.Length == 0)
            {
                return "";
            }
            else
            {
                byte[] zippedData = Convert.FromBase64String(zippedString.ToString());
                return (string)(System.Text.Encoding.UTF8.GetString(UnGZip_Decompress(zippedData)));
            }
        }
        /// <summary>
        /// ZIP解压
        /// </summary>
        /// <param name="zippedData"></param>
        /// <returns></returns>
        public static byte[] UnGZip_Decompress(byte[] zippedData)
        {
            MemoryStream ms = new MemoryStream(zippedData);
            GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress);
            MemoryStream outBuffer = new MemoryStream();
            byte[] block = new byte[1024];
            while (true)
            {
                int bytesRead = compressedzipStream.Read(block, 0, block.Length);
                if (bytesRead <= 0)
                    break;
                else
                    outBuffer.Write(block, 0, bytesRead);
            }
            compressedzipStream.Close();
            return outBuffer.ToArray();
        }
 







產生SQL共用函式

-----------------------1.set SQL Server可執行CLR-----------------------
sp_configure 'clr enabled', 1
GO
RECONFIGURE
go

-----------------------2.建立ASSEMBLY-----------------------

CREATE ASSEMBLY Net_SQLCom  FROM '******\bin\Debug\SQLCom.dll'
WITH PERMISSION_SET = SAFE;
--drop ASSEMBLY Net_SQLCom
go

--chk  有沒有看到Net_SQLCom
SELECT * FROM sys.assemblies
go

-----------------------3.建立function-----------------------
--drop function GZip
--drop function UnGZip

CREATE FUNCTION [dbo].[GZip](@str [nvarchar](max) )
RETURNS [nvarchar](MAX) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [Net_SQLCom].[UserDefinedFunctions].[GZip]
GO


CREATE FUNCTION [dbo].[UnGZip](@str [nvarchar](max) )
RETURNS [nvarchar](MAX) WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [Net_SQLCom].[UserDefinedFunctions].[UnGZip]
GO

--chk  有沒有看到function
SELECT * FROM sys.assembly_modules;

-----------------------4.Test-----------------------
select [dbo].[UnGZip]([dbo].[GZip]('setse' ))





注意資料庫型別

char資料有固定長度,並且都為英文數字。
nchar資料有固定長度,但不確定是否皆為英文數字。
varchar資料沒有固定長度,並且都為英文數字。
nvarchar資料沒有固定長度,且不確定是否皆為英文數字。
 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template