SQL Server Plan Cache 執行計畫Cache
Brad Chen's Developer Blog Azure Database Data science Machine Learning Cloud
2025年1月11日 星期六
2023年4月7日 星期五
2022年7月7日 星期四
2022年6月14日 星期二
2022年6月13日 星期一
2022年5月30日 星期一
2022年4月19日 星期二
2022年3月28日 星期一
SQL Server Performance Counter
sys.dm_os_performance_counters (Transact-SQL)
Windows Performance Monitor Overview
1.磁碟IO效能相關效能計數器
Average Disk sec/Read
Average Disk sec/Read value is below 8ms
Average Disk sec/Read is proportional to time needed for one disk rotation. For example, a disk that makes 3,600 round per minute needs 60s/3600 = 0.016 seconds, i.e. 16 milliseconds to make one rotation. Average Disk sec/Read for that disk should be a multiple of 16 milliseconds. The time added to one disk rotation is the queuing time and the time needed for data transit across the I/O bus
| Value (ms) | Performance |
| < 8 (< 0.008 sec) | Excellent |
| 8 – 12 (0.008 ~ 0.012 sec) | OK |
| 12 – 20 (0.012 ~ 0.020 sec) | Fair |
| > 20 ( > 0.020 sec)) | Bad |
Maximum peaks during excessive I/O operations can be up to 25 milliseconds, but values constantly higher than 20 milliseconds indicate poor performance
Average Disk sec/Write
non-cached writes are the same as for Average Disk sec/Read
Usually, the read and write speed on a disk are different. The recommended values for non-cached writes are the same as for Average Disk sec/Read. In case of cached writes, the values are very different – values higher than 4 milliseconds indicate poor performance, while the values less than 1 milliseconds indicate the best performance
| Value (ms) | Performance |
| < 1 ( < 0.001 sec) | Excellent |
| 1 – 2 (0.001 ~ 0.002 sec) | OK |
| 2 – 4 (0.002 ~ 0.004 sec) | Fair |
| > 4 (> 0.004 sec) | Bad |
If the Average Disk sec/Read and Average Disk sec/Write values are constantly above the recommended values, it’s an indication of a disk bottleneck and additional analysis is required
https://www.sqlshack.com/sql-server-disk-performance-metrics-part-1-important-disk-performance-metrics/
Use the SQLIOSim utility to simulate SQL Server activity on a disk subsystem
https://docs.microsoft.com/en-US/troubleshoot/sql/tools/sqliosim-utility-simulate-activity-disk-subsystem
Understanding SQLIOSim Output
https://www.itprotoday.com/sql-server/understanding-sqliosim-output
https://docs.microsoft.com/en-us/azure-stack/hci/manage/diskspd-overview
http://sharedderrick.blogspot.com/2011/09/sqliosim-sql-server-2008.html
2.記憶體相關效能計數器
Target Server Memory(KB)
The total amount of dynamic memory the server can consume
Target Server Memory (KB) is the amount of memory that SQL Server is willing (potential) to allocate to the buffer pool under its current load
Total Server Memory(KB)
The Total Server Memory is what SQL currently has allocated
The Total Server Memory is the current amount of memory that SQL Server is using
The Total Server Memory is the current amount of memory currently assigned to SQL Server.
If this counter is still growing the server has not yet reached its steady-state, and it is still trying to populate the cache and get pages loaded into memory. Performance will likely be somewhat slower during this time since more disk I/O is required at this stage. This behavior is normal. Eventually Total Server Memory should approximate Target Server Memory.
Buffer Cache Hit Ratio
Percent of page requests satisfied by data pages from the buffer pool
It gives the ratio of the data pages found and read from the SQL Server buffer cache and all data page requests. The pages that are not found in the buffer cache are read from the disk, which is significantly slower and affects performance
The recommended value for Buffer Cache Hit Ratio is over 90. When better performance is needed, the minimal acceptable value is 95. A lower value indicates a memory problem
Page Life Expectancy
Duration, in seconds, that a page resides in the buffer pool
SQL Server has more chances to find the pages in the buffer pool if they stay there longer. If the page is not in the buffer pool, it will be read from disk, which affects performance. If there’s insufficient memory, data pages are flushed from buffer cache more frequently, to free up the space for the new pages
When there’s sufficient memory on the server, pages have a high life expectancy. The normal values are above 300 seconds (5 minutes) and the trend line should be stable. It’s recommended to monitor the values over time, as frequent quick drops indicate memory issues. Also, a value drop of more than 50% is a sign for deeper investigation
SQL Server memory performance metrics – Part 4 – Buffer Cache Hit Ratio and Page Life Expectancy
3.處理器CPU相關效能計數器
Processor:% Processor Time
Processor (_Total) \ % Processor Time
Sustained values > 90% on a single processor machine, or > 80% on a multiprocessor machine should be investigated
This counter monitors the amount of time the CPU spends executing a thread that is not idle. A consistent state of 80 percent to 90 percent might indicate the need to upgrade your CPU or add more processors.
For multiprocessor systems, monitor a separate instance of this counter for each processor. This value represents the sum of processor time on a specific processor. To determine the average for all processors, use the System: %Total Processor Time counter instead.
Processor: % Privileged Time
Corresponds to the percentage of time the processor spends on execution of Microsoft Windows kernel commands, such as processing of SQL Server I/O requests. If this counter is consistently high when the Physical Disk counters are high, consider installing a faster or more efficient disk subsystem.
Note
Different disk controllers and drivers use different amounts of kernel processing time. Efficient controllers and drivers use less privileged time, leaving more processing time available for user applications, increasing overall throughput.
Processor: %User Time
Corresponds to the percentage of time that the processor spends on executing user processes such as SQL Server.
System: Processor Queue Length
Current Depth of the thread Scheduler Ready Queue.
If the Ready threads per processor value is > 2 with some frequency this may indicate a processor bottleneck.
Corresponds to the number of threads waiting for processor time. A processor bottleneck develops when threads of a process require more processor cycles than are available. If more than a few processes attempt to utilize the processor's time, you might need to install a faster processor. Or, if you have a multiprocessor system, you could add a processor.
Monitor CPU Usage
[SQL SERVER]SQL Server Perfmon Counters
| 名稱 | 建議值 | 說明 |
SQLServer: Buffer Manager: Buffer cache hit ratio | >95% 優:99.999% | 從buffer讀取data page擊中率,該值越高表示資料都是從記憶體中讀取 |
SQLServer: Buffer Manager: Page life expectancy | >300 停留超過5分鐘 | data page留在buffer的時間,停留時間越長,表示SQL Server不需要從disk讀取資料到memory,停留時間越短,表示Server可能需要增加記憶體 |
SQLServer: Buffer Manager: Checkpoint Pages / Sec | 將記憶體中的Dirty Page 和log page寫入disk,checkpoint頻率和 recovery interval設定有關聯,如果該值太高可能表示記憶體不足或是 recovery interval設定太高(預設0表示每分鐘處理) | |
SQL Server: Databases:Log Flushes/sec | log page每秒寫入transaction log file頻率,該值太高可能是交易處理邏輯不良導致交易次數太高,如1000筆資料每一次commit該值就會增加1000,如果批次分成2個交易(每500筆commit)該值只會增加2 | |
SQL Server: Databases:Log Growths | 0 | transaction log file被擴大次數,每一次當transaction log file被擴大時,所有使用者活動必須被停止,直到transaction log file成長完畢,這通常表示transaction log file空間沒有被reuse或是初始值設定不良所導致,要避免使用者請求處理期間被擴大影響效能 |
| SQLServer: SQL Statistics: Batch Requests/Sec | 每一秒處理請求的數量,該值通常和硬體等級有關,也通常表示SQL Server吞吐量,基本上透過該值需和其他值互相搭配參考。 | |
| SQLServer: SQL Statistics: SQL Compilations/Sec | < Batch Requests/Sec *10% | 每秒TSQL發生編譯次數(包含重新編譯),編譯是一個昂貴操作,基本上該值越低越好,越高可能 ad hoc查詢過多,或使用不必要的recompile提示,可能須適當改寫TSQL降低編譯次數。 |
| SQLServer: SQL Statistics: SQL Re-Compilations/Sec | <Compilations/Sec *10% | 每秒發生重新編譯次數,該值應該要接近0,因為重新編譯可能導致deadlock和exclusive compile locks,該值需參考Batch Requests/Sec和SQL Compilations/Sec |
| SQLServer: Access Methods: Page Splits / Sec | < 100 Batch Requests/Sec *20% | 每秒發生頁面分割次數,頁面分割是昂貴操作,要減少頁面分割次數,需要查看資料表和索引設計,避免非順序資料新增或是針對高寫資料表調整索引填充因子。 |
SQLServer: General Statistic: Processes Block | process blocked another process數量,理想情況下該值應該接近0,但實務上短暫封鎖是可接受的,如果該值太高你應該詳細調查是那些因素造成,以我個人經驗大多都是不良的平行處理所造成(參考SQL SERVER]找出封鎖的處理序) | |
| SQLServer: Locks: Lock Wait Time (ms) | <10 ms | thread請求lock等待時間,該值過高可能是blocking、不必要的鎖定擴大或是TSQL不佳所造成,也有可能是I/O問題(硬碟壞一顆),透過 sys.dm_os_waiting_tasks 查看鎖定資源,調整TSQL、設計正確索引或是最小化鎖定(nolock) |
| SQLServer: Locks: Lock Waits / Sec | 0 | thread請求lock等待時間。 整體lock時間應該要越短越好,例如應該盡量縮短交易處理時間,避免大交易處理(可拆多個小交易) |
| SQLServer: Locks: Avg Wait Time (ms) | <500 ms | 每個lock請求等待時間,一般來說平均超過500ms表示有過多的封鎖,該值需參考Lock Waits/sec 取得一個平衡。 |
| SQLServer: Locks: Deadlock/s | <1 | 每秒死結次數,該值應該等於0,該值太高表示交易處理邏輯不良,或是沒有使用正確索引導致都是full scan,需重新調整邏輯處理或是改善TSQL效能。 |
| SQL Server: Latches : Latch Waits/sec | (Total Latch Wait Time) / (Latch Waits/ Sec) < 10 | 該數值表示無法立即授予Latch的請求。 Latch是SQL Serve內部用來同步資源一種資料結構,和操作系統的read/write lock類似,Latch保護資源使得資料存取井然有序,例如說當某一個session取得某個資源latch獨佔時,其他session如果也需要該資源latch時,則必須等待該latch釋放才可存取。 大方向來說,latch分兩大類,一buffer latch、另一I/O latch,buffer latch一般<1 ms,I/O latch一般<10 ms。 note:PAGELATCH_SH wait for PFS or SGAM pages in tempdb,表示tempdb有嚴重的資源競爭,透過切割tempdb datafile和啟用TF1118可獲得改善(參考[SQL SERVER]了解制式範圍和混和範圍) |
| SQL Server: Latches : Avg Latch Wait Time (ms) | <1 ms | 請求latch所等待時間。 |
| SQL Server: Latches : Total Latch Wait Time (ms) | (Total Latch Wait Time) / (Latch Waits/ Sec) < 10 | 該數值表示最後Latch請求的總等待時間,該值可提供其他計數器參考使用 |
SQL Server - Performance Counter Guidance
2021年4月16日 星期五
Pssdiag
Pssdiag/Sqldiag Manager
https://github.com/microsoft/DiagManager
Getting Started
https://github.com/Microsoft/DiagManager/wiki/Getting-Started
Step by Step tasks
- Installation of pssdiag/sqldiag manager
- Creating a pssdiag package
- [Running Pssdiag an SQL Server instance](Running PSSDiag)
- [Running pssdiag on a cluster](Running Pssdiag on a Cluster)
- [Creating your own custom collectors](Custom collector)
- Frequently Asked Questions
Reference:
SQL Server: How to Configure pssdiag utility for SQL Server 2016
Using PSSDiag and SQL Nexus to monitor SQL Server performance (Youtube Video)
https://www.youtube.com/watch?v=5xo_S1nx_nA
2020年12月21日 星期一
SQL Server tempdb
What is the TempDB database used for in SQL Server?
- User Objects
- User-defined tables and indexes
- System tables and indexes
- Global temporary tables and indexes
- Local temporary tables and indexes
- Table variables
- Tables returned in table-valued functions
- Internal Objects
- Work tables for CURSOR or SPOOL operations and temporary large object (LOB) storage.
- Work files for HASH JOIN or HASH aggregate operations.
- Intermediate sort results for operations such as creating or rebuilding indexes (if SORT_IN_TEMPDB is specified), or certain GROUP BY, ORDER BY, or UNION queries.
- Version Stores
- Row versions that are generated by data modification transactions in a database that uses snapshot or read committed using row versioning isolation levels. (Snapshot Isolation and Read-Committed Snapshot Isolation (If long running transaction exist cause tempdb growing up))
- Row versions that are generated by data modification transactions for features such as: Online Index Operations, Multiple Active Result Sets (MARS), and AFTER triggers.
tempdb檔案設定最佳化:
(1) 為tempdb交易紀錄檔設定足夠大的大小,以避免自動成長。
(2) 為每一個 CPU 建置一個 Tempdb 的資料檔案,或是以 CPU 個數的一半建置 (除以 2 的倍數)。
(3) 每個資料檔案大小必須相同。
(4) 每個資料檔案必須預留足夠的交易量空間。例如觀察平時 Tempdb 使用量約 3GB的話,建議設定總共 4GB 空間。(也就是所有tempdb資料檔加起來4GB,例如建立4個1GB資料檔)。
(5) 符合上述的配置條件,每個 CPU 可以獨立取用對應的 Tempdb 資料檔案,可以增進效能及併發性。
資料治理實施
資料治理實施
-
SQL Server Enterprise 與 Standard 基本差異比較如下 : SQL Server Enterprise Edition SQL Server Standard Edition ...
-
Oracle version support matrix Oracle Release 19 Oracle Database Client Software Requirements https://docs.oracle.com/en/database/oracle/orac...
-
1.安裝了Oracle Client,就可以用Oracle Net Manager工具來設定TNS連線設定(Tnsnames.ora) Oracle Client 18.3 Installation 安裝Oracle Client 18.3 2.啟動Oracle Ne...