顯示具有 MSSQL 標籤的文章。 顯示所有文章
顯示具有 MSSQL 標籤的文章。 顯示所有文章

2014年6月28日 星期六

CMD & SQL script reference

CMD:
set res=false  //設定變數
set TODAY=%date:~0,4%-%date:~5,2%-%date:~8,2%   // %變數% = 取得變數值
if %TODAY:~5,5% == 06-30 (     // 比較變數值
    set res=true
        set /A year=%date:~0,4%-1            //計算變數
    set BKDAY=%year%-12-31
)

sqlcmd -d VipDataSQL -v BKDAY='%BKDAY%' -i c:\VIPSQL\DataMove-VIPDATASQL.SQL -o c:\VIPSQL\%BKDAY%.log  
// -d database名稱
// -v 傳入sqlcmd的參數值
// -i 輸入腳本路徑及檔名
// -o 輸出執行記錄路徑及檔名
net use M: /delete                //刪除網路磁碟機
net use M: \\fs\edp_dept$ /user:sean.hsu 19190504           //連接網路磁碟機
move C:\VIPSQL\*.log "M:\3 應用系統\3.10 CRM\Logs\VIPSQLDATA"
//移動檔案至磁碟機目錄下
set res=             //刪除變數

sql script:
// print 輸出結果,相當於cmd的echo
print '查詢轉到 VIPOrditem' + @ESTR + '筆數';  //混合參數與文字敘述的表式方法
set @EDATE = $(BKDAY)           // 提取傳入參數BKDAY的值,並指派給@EDATE

2014年3月28日 星期五

MS SQL Database size history

轉貼:
http://gallery.technet.microsoft.com/scriptcenter/f1df9f50-9cd9-4c75-a8d9-e2faba6b8574
程式碼: 
-- Transact-SQL script to analyse the database size growth using backup history. 
DECLARE @endDate datetime@months smallintSET @endDate = GetDate();  -- Include in the statistic all backups from today 
SET @months = 6;           -- back to the last 6 months. 
 
;WITH HIST AS 
   (SELECT BS.database_name AS DatabaseName 
          ,YEAR(BS.backup_start_date) * 100 
           + MONTH(BS.backup_start_dateAS YearMonth 
          ,CONVERT(numeric(101), MIN(BF.file_size / 1048576.0)) AS MinSizeMB 
          ,CONVERT(numeric(101), MAX(BF.file_size / 1048576.0)) AS MaxSizeMB 
          ,CONVERT(numeric(101), AVG(BF.file_size / 1048576.0)) AS AvgSizeMB 
    FROM msdb.dbo.backupset as BS 
         INNER JOIN 
         msdb.dbo.backupfile AS BF 
             ON BS.backup_set_id = BF.backup_set_id 
    WHERE NOT BS.database_name IN 
              ('master''msdb''model''tempdb') 
          AND BF.file_type = 'D' 
          AND BS.backup_start_date BETWEEN DATEADD(mm, - @months@endDateAND @endDate 
    GROUP BY BS.database_name 
            ,YEAR(BS.backup_start_date) 
            ,MONTH(BS.backup_start_date)) 
SELECT MAIN.DatabaseName 
      ,MAIN.YearMonth 
      ,MAIN.MinSizeMB 
      ,MAIN.MaxSizeMB 
      ,MAIN.AvgSizeMB 
      ,MAIN.AvgSizeMB  
       - (SELECT TOP 1 SUB.AvgSizeMB 
          FROM HIST AS SUB 
          WHERE SUB.DatabaseName = MAIN.DatabaseName 
                AND SUB.YearMonth < MAIN.YearMonth 
          ORDER BY SUB.YearMonth DESCAS GrowthMB 
FROM HIST AS MAIN 
ORDER BY MAIN.DatabaseName 
        ,MAIN.YearMonth

2014年1月4日 星期六

1134345 - Using locked pages for SQL Server

1134345 - Using locked pages for SQL Server
加入lock page的使用者權限群組
避免windows page out,影響sql server效能
有用網域帳號才需要(例如做cluster)
因為local的預設已有權限
需重啟sql service才能生效

2014年1月3日 星期五

SAP 建議手動維護DATA FILE的增長

1238993 - Proportional File Auto-Growth with SQL Server 2008

1744217 - MSSQL: Improving the database performance

Symptom
You experience bad general system performance and suspect the database as root cause.

Environment
ABAP-, Java- or dual-stack based SAP systems based on Microsoft SQL Server

Cause
In the majority of cases left out maintenance and housekeeping activities on database level are the root cause for database performance related issues.
Without proper and up-to-date system operation procedures in place, the overall performance of the database (and the BW system if in use) in turn can degrade significantly over time.

1744217 - MSSQL: Improving the database performance

SAP note 1558087 - SQL Server Statistics Maintenance

Windows NT下
先將批次檔與note的附件放在以下路徑
(1)D:\usr\sap\PRD\command\
SM69 增加外部指令,範例:
(2)D:\usr\sap\PRD\command\sap_IndexStatsMaintenance.bat
批次檔 sap_IndexStatsMaintenance.bat 內容如下:
sqlcmd -i D:\usr\sap\PRD\command\sap_IndexStatsMaintenance.sql -o D:\usr\sap\PRD\Log\sap_IndexStatsMaintenance.txt
(3)建立 D:\usr\sap\PRD\Log\目錄
(4) sap_IndexStatsMaintenance.sql 增加三行(紅字部份,prd = sid,要分大小寫):
 /********************************************************/
/*  Object  :Stored Procedure sap_IndexStatsMaintenance */
/*  Author  : C5024907                                  */
/*  Note    : 1558087                                   */
/*  Version : 1.5                                       */
/*                                                      */
/*  05/13/2011 - Initial version                        */
/*  08/23/2011 - C5002239 found more fields             */
/*  08/09/2012 - I035208 found some more in INDX        */
/*  11/30/2012 - I044729 found the systemobject         */
/*               problem                                */
/*  04/09/2013 - Added PA* and HRP* tables              */
/********************************************************/
use PRD
setuser 'prd'
go


(5)@NoExecute        int           = 0,
改成 @NoExecute        int           = 1,

(6)存檔
(7)排程每月月中或每年一月執行