Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| db:mssql [2021/09/16 17:22] – [MSSQL rebuild all indexes in database] admin | db:mssql [2021/11/03 09:53] (current) – admin | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| ===== MSSQL. Info ===== | ===== MSSQL. Info ===== | ||
| + | * https:// | ||
| + | * mssql video traning http:// | ||
| + | * https:// | ||
| * https:// | * https:// | ||
| - | * https://habr.com/ru/post/316064/ - информация по mssql | + | * https://docs.microsoft.com/ru-ru/sql/ssms/download-sql-server-management-studio-ssms? |
| * https:// | * https:// | ||
| * [[https:// | * [[https:// | ||
| * mssql update pack https:// | * mssql update pack https:// | ||
| - | * mssql video traning http:// | ||
| * sp_command help https:// | * sp_command help https:// | ||
| ===== MSSQL. Docker ===== | ===== MSSQL. Docker ===== | ||
| Docker mssql [[https:// | Docker mssql [[https:// | ||
| - | <code bash> | + | <code bash> |
| - | sudo docker run -e " | + | sudo docker run -e " |
| - | | + | |
| - | | + | -v / |
| + | -d mcr.microsoft.com/ | ||
| </ | </ | ||
| Line 23: | Line 25: | ||
| sudo docker exec -it sql3 " | sudo docker exec -it sql3 " | ||
| / | / | ||
| + | |||
| + | -- включить mssql agent | ||
| + | sp_configure 'show advanced options', | ||
| + | GO | ||
| + | RECONFIGURE; | ||
| + | GO | ||
| + | sp_configure 'Agent XPs', 1; | ||
| + | GO | ||
| + | RECONFIGURE | ||
| + | GO | ||
| </ | </ | ||
| Line 49: | Line 61: | ||
| <code SQL> | <code SQL> | ||
| -- # Backup Restore/ | -- # Backup Restore/ | ||
| + | |||
| + | -- get backup set | ||
| + | SELECT TOP 100 | ||
| + | name, | ||
| + | type, * | ||
| + | FROM | ||
| + | msdb..backupset | ||
| + | WHERE 1=1 | ||
| + | -- and database_name like ' | ||
| + | ORDER BY | ||
| + | backup_set_id DESC | ||
| -- delete database | -- delete database | ||
| Line 63: | Line 86: | ||
| # backup full | # backup full | ||
| - | /* | + | /* */ |
| USE WAdmIsk; | USE WAdmIsk; | ||
| BACKUP DATABASE WAdmIsk TO DISK = ' | BACKUP DATABASE WAdmIsk TO DISK = ' | ||
| Line 99: | Line 122: | ||
| </ | </ | ||
| ++++ | ++++ | ||
| + | |||
| + | |||
| + | ++++ --- MSSQL. turn on agent| | ||
| + | <code SQL> | ||
| + | -- Get data for mssql | ||
| + | sp_configure 'show advanced options', | ||
| + | GO | ||
| + | RECONFIGURE; | ||
| + | GO | ||
| + | sp_configure 'Agent XPs', 1; | ||
| + | GO | ||
| + | RECONFIGURE | ||
| + | GO | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | ++++ --- SCRIPT - for archive all files except ZIP| | ||
| + | <code BASH> | ||
| + | Function Zip | ||
| + | { | ||
| + | Param | ||
| + | ( | ||
| + | [string[]]$toBeZipped | ||
| + | ) | ||
| + | if ($null -eq $toBeZipped) {return} | ||
| + | $CurDir = Get-Location | ||
| + | Set-Location " | ||
| + | foreach ( $f in $toBeZipped) | ||
| + | { | ||
| + | echo " | ||
| + | | ||
| + | } | ||
| + | Set-Location $CurDir | ||
| + | } | ||
| + | |||
| + | |||
| + | Function ZipFiles | ||
| + | { | ||
| + | Param | ||
| + | ( | ||
| + | [string]$zipFile | ||
| + | , | ||
| + | [string[]]$toBeZipped | ||
| + | ) | ||
| + | $CurDir = Get-Location | ||
| + | Set-Location " | ||
| + | .\7z.exe A -tzip $zipFile $toBeZipped | Out-Null | ||
| + | Set-Location $CurDir | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | # Code | ||
| + | # get-childitem $path -recurse -exclude *.cs, | ||
| + | $bckpdir=$args[0] | ||
| + | if ($null -eq $bckpdir){ | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | |||
| + | $CheckFile = Test-Path -Path $bckpdir | ||
| + | |||
| + | |||
| + | if (!($CheckFile)) | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | echo " | ||
| + | |||
| + | |||
| + | |||
| + | $files = Get-ChildItem $bckpdir\*.* -exclude *.zip | Where-Object {$_.CreationTime -lt ((Get-date).adddays(+1))} | ||
| + | Zip $files | ||
| + | foreach ( $f in $files) | ||
| + | { | ||
| + | | ||
| + | { | ||
| + | | ||
| + | echo " | ||
| + | } | ||
| + | } | ||
| + | |||
| + | echo " | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | ++++ --- SCRIPT - for archive ZIP all files except last one for sort in time| | ||
| + | <code BASH> | ||
| + | Function Zip | ||
| + | { | ||
| + | Param | ||
| + | ( | ||
| + | [string[]]$toBeZipped | ||
| + | ) | ||
| + | if ($null -eq $toBeZipped) {return} | ||
| + | $CurDir = Get-Location | ||
| + | Set-Location " | ||
| + | foreach ( $f in $toBeZipped) | ||
| + | { | ||
| + | echo " | ||
| + | | ||
| + | } | ||
| + | Set-Location $CurDir | ||
| + | } | ||
| + | |||
| + | |||
| + | Function ZipFiles | ||
| + | { | ||
| + | Param | ||
| + | ( | ||
| + | [string]$zipFile | ||
| + | , | ||
| + | [string[]]$toBeZipped | ||
| + | ) | ||
| + | $CurDir = Get-Location | ||
| + | Set-Location " | ||
| + | .\7z.exe A -tzip $zipFile $toBeZipped | Out-Null | ||
| + | Set-Location $CurDir | ||
| + | } | ||
| + | |||
| + | |||
| + | |||
| + | # Code | ||
| + | # get-childitem $path -recurse -exclude *.cs, | ||
| + | $bckpdir=$args[0] | ||
| + | if ($null -eq $bckpdir){ | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | |||
| + | $CheckFile = Test-Path -Path $bckpdir | ||
| + | |||
| + | |||
| + | if (!($CheckFile)) | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | echo " | ||
| + | |||
| + | |||
| + | |||
| + | $files = Get-ChildItem $bckpdir\*.* -exclude *.zip | sort -Property CreationTime | ||
| + | |||
| + | if ($files -is [array]) | ||
| + | { | ||
| + | $files = $files[0..($files.Count - 2)] | ||
| + | Zip $files | ||
| + | foreach ( $f in $files) | ||
| + | { | ||
| + | | ||
| + | { | ||
| + | | ||
| + | echo " | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | | ||
| + | } | ||
| + | echo " | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| + | ++++ --- SCRIPT - clear age files in directory | | ||
| + | <code BASH> | ||
| + | $bckpdir=$args[0] | ||
| + | if ($null -eq $bckpdir){ | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | |||
| + | $numdays=$args[1] | ||
| + | if ($null -eq $numdays -or -not $numdays -match ' | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | |||
| + | |||
| + | $CheckFile = Test-Path -Path $bckpdir | ||
| + | |||
| + | |||
| + | if (!($CheckFile)) | ||
| + | echo " | ||
| + | exit 1 | ||
| + | } | ||
| + | echo " | ||
| + | |||
| + | $files = Get-ChildItem –Path $bckpdir | Where-Object {($_.CreationTime -lt (Get-Date).AddDays(-$numdays))} | ||
| + | if ($null -eq $files){ | ||
| + | echo " | ||
| + | exit 0 | ||
| + | } | ||
| + | echo $($files| Format-Table | ||
| + | $files | Remove-Item | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | |||
| ===== MSSQL.bulk copy to file ===== | ===== MSSQL.bulk copy to file ===== | ||
| Line 143: | Line 368: | ||
| ===== MSSQL rebuild all indexes in database ===== | ===== MSSQL rebuild all indexes in database ===== | ||
| + | <code SQL >-- Short way | ||
| + | Exec sp_msforeachtable 'SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REBUILD'</ | ||
| ++++ Rebuild indexes| | ++++ Rebuild indexes| | ||
| <code SQL> | <code SQL> | ||
| --https:// | --https:// | ||
| + | |||
| + | --check fragmentation | ||
| + | SELECT dbschemas.[name] as ' | ||
| + | dbtables.[name] as ' | ||
| + | dbindexes.[name] as ' | ||
| + | indexstats.avg_fragmentation_in_percent, | ||
| + | indexstats.page_count | ||
| + | FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS indexstats | ||
| + | INNER JOIN sys.tables dbtables on dbtables.[object_id] = indexstats.[object_id] | ||
| + | INNER JOIN sys.schemas dbschemas on dbtables.[schema_id] = dbschemas.[schema_id] | ||
| + | INNER JOIN sys.indexes AS dbindexes ON dbindexes.[object_id] = indexstats.[object_id] | ||
| + | AND indexstats.index_id = dbindexes.index_id | ||
| + | WHERE indexstats.database_id = DB_ID() AND dbtables.[name] like ' | ||
| + | ORDER BY indexstats.avg_fragmentation_in_percent desc | ||
| + | |||
| + | -- process fragmentation | ||
| + | use ISOJ; | ||
| DECLARE @TableName VARCHAR(255) | DECLARE @TableName VARCHAR(255) | ||
| Line 160: | Line 404: | ||
| SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3), | SET @sql = 'ALTER INDEX ALL ON ' + @TableName + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3), | ||
| EXEC (@sql) | EXEC (@sql) | ||
| + | DECLARE @msg VARCHAR(200) = ' | ||
| + | RAISERROR(@msg, | ||
| + | WAITFOR DELAY ' | ||
| FETCH NEXT FROM TableCursor INTO @TableName | FETCH NEXT FROM TableCursor INTO @TableName | ||
| END | END | ||
| Line 171: | Line 418: | ||
| https:// | https:// | ||
| + | ===== MSSQL. syncronize tables ===== | ||
| + | |||
| + | ++++Check syncrhonize| | ||
| + | <code SQL> | ||
| + | |||
| + | -- Sector | ||
| + | select ' | ||
| + | year(case when c.date is null then co.date else c.date end) as year, | ||
| + | count(*) as cnt, | ||
| + | sum(case when co.id is null then 1 else 0 end) as cntForLoad | ||
| + | from ISOJ2018.dbo.cAppeal c | ||
| + | full outer join isoj.dbo.cAppeal co on co.id=c.id | ||
| + | -- where co.id is null | ||
| + | group by rollup(year(case when c.date is null then co.date else c.date end) | ||
| + | ); | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | ++++ Syncronize tables| | ||
| + | <code SQL> | ||
| + | USE ISOJ; | ||
| + | --- synchronize tables | ||
| + | |||
| + | DECLARE @DATABASE NVARCHAR(255) | ||
| + | DECLARE @TABLE NVARCHAR(255) | ||
| + | DECLARE @cmd NVARCHAR(1000) | ||
| + | |||
| + | |||
| + | -- ' | ||
| + | | ||
| + | | ||
| + | FROM [ISOJ].INFORMATION_SCHEMA.TABLES | ||
| + | WHERE table_type = 'BASE TABLE' | ||
| + | |||
| + | OPEN DatabaseCursor | ||
| + | |||
| + | FETCH NEXT FROM DatabaseCursor INTO @TABLE | ||
| + | WHILE @@FETCH_STATUS = 0 | ||
| + | BEGIN | ||
| + | FETCH NEXT FROM DatabaseCursor INTO @TABLE | ||
| + | |||
| + | --set @TABLE = ' | ||
| + | |||
| + | SET @cmd = ' | ||
| + | INSERT INTO [isoj].[dbo].[' | ||
| + | select top 1000000 * | ||
| + | from ISOJ2018.[dbo].[' | ||
| + | where c.id in ( | ||
| + | select c.id | ||
| + | from ISOJ2018.[dbo].[' | ||
| + | left join isoj.[dbo].[' | ||
| + | where co.id is null | ||
| + | ) | ||
| + | order by c.DateModified asc; | ||
| + | ' | ||
| + | |||
| + | BEGIN TRY | ||
| + | |||
| + | PRINT @TABLE -- uncomment if you want to see commands | ||
| + | EXEC (@cmd) | ||
| + | END TRY | ||
| + | BEGIN CATCH | ||
| + | PRINT ' | ||
| + | PRINT @cmd | ||
| + | PRINT ERROR_MESSAGE() | ||
| + | PRINT ' | ||
| + | END CATCH | ||
| + | |||
| + | | ||
| + | CLOSE DatabaseCursor | ||
| + | DEALLOCATE DatabaseCursor | ||
| + | |||
| + | GO | ||
| + | </ | ||
| + | ++++ | ||
| + | ===== TSQL example ===== | ||
| + | ++++ TSQL example| | ||
| + | <code SQL> | ||
| + | use ISOJ; | ||
| + | |||
| + | |||
| + | | ||
| + | DECLARE @Table NVARCHAR(255) | ||
| + | DECLARE @cmd NVARCHAR(1000) | ||
| + | |||
| + | DECLARE DatabaseCursor CURSOR READ_ONLY FOR | ||
| + | SELECT name FROM master.sys.databases | ||
| + | WHERE name IN (' | ||
| + | AND state = 0 -- database is online | ||
| + | AND is_in_standby = 0 -- database is not read only for log shipping | ||
| + | ORDER BY 1 | ||
| + | |||
| + | OPEN DatabaseCursor | ||
| + | |||
| + | FETCH NEXT FROM DatabaseCursor INTO @Database | ||
| + | WHILE @@FETCH_STATUS = 0 | ||
| + | BEGIN | ||
| + | |||
| + | SET @cmd = ' | ||
| + | | ||
| + | |||
| + | -- create table cursor | ||
| + | EXEC (@cmd) | ||
| + | OPEN TableCursor | ||
| + | |||
| + | FETCH NEXT FROM TableCursor INTO @Table | ||
| + | WHILE @@FETCH_STATUS = 0 | ||
| + | BEGIN | ||
| + | BEGIN TRY | ||
| + | SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD' | ||
| + | PRINT @cmd -- uncomment if you want to see commands | ||
| + | EXEC (@cmd) | ||
| + | END TRY | ||
| + | BEGIN CATCH | ||
| + | PRINT ' | ||
| + | PRINT @cmd | ||
| + | PRINT ERROR_MESSAGE() | ||
| + | PRINT ' | ||
| + | END CATCH | ||
| + | |||
| + | FETCH NEXT FROM TableCursor INTO @Table | ||
| + | | ||
| + | |||
| + | CLOSE TableCursor | ||
| + | | ||
| + | |||
| + | FETCH NEXT FROM DatabaseCursor INTO @Database | ||
| + | END | ||
| + | CLOSE DatabaseCursor | ||
| + | DEALLOCATE DatabaseCursor | ||
| + | GO | ||
| + | </ | ||
| + | ++++ | ||