Currently Browsing: SQL Server

SQL Server / SSMS / Expanding Columns List Query

SQL Server / SSMS / Expanding Columns List Query
Same as in previous post example Expanding Tables List Query I usually do not notice this type of queries working in the background. But recently I end up working with a customer having DB with 150K tables, some of the tables have more than 500 columns, and expanding columns list operation can end up being slower than usual. Here is the code: SELECT clmns.name AS [Name], 'Server[@Name=' + quotename(CAST( serverproperty(N'Servername') AS sysname),'''') + ']' + '/Database[@Name=' + quotename(db_name(),'''') + ']' + '/Table[@Name=' + quotename(tbl.name,'''') + ' and @Schema=' +...
read more

SQL Server / SSMS / Expanding Tables List Query

SQL Server / SSMS / Expanding Tables List Query
Usually I do not even notice this query working in the background, since this operation is quite fast. But recently I end up working with a customer having DB with 150K tables, and expanding table list operation can be really slow and expensive. Here is the code: SELECT tbl.name AS [Name], SCHEMA_NAME(tbl.schema_id) AS [Schema], 'Server[@Name=' + quotename(CAST( serverproperty(N'Servername') AS sysname),'''') + ']' + '/Database[@Name=' + quotename(db_name(),'''') + ']' + '/Table[@Name=' + quotename(tbl.name,'''') + ' and @Schema=' + quotename(SCHEMA_NAME(tbl.schema_id),'''') + ']' AS [Urn],...
read more

PowerShell / SQL Server / Generate SQL Script to Restore Multiple Transaction Logs

PowerShell / SQL Server / Generate SQL Script to Restore Multiple Transaction Logs
Restore multiple TRN logs using PowerShell. In this example I will show you really simple way how you can use PowerShell to restore multiple logs at once. After restoring full backup WITH NORECOVERY you need to figure it out last log sequence number of the last transaction in the backup set. TRN Log backups must be restored in the order in which they were created. Log backups contain log sequence numbers (LSN), and they must be restored in that particular order. There are few ways to find last LSN. USE msdb GO SELECT TOP 10 bck.database_name , bck.first_lsn , bck.last_lsn , bck.backup_start_date ,...
read more

Azure / SQL Server / Azure SQL Database Disaster Recovery / Orphaned Users / Different SIDs

Azure / SQL Server / Azure SQL Database Disaster Recovery / Orphaned Users / Different SIDs
If you are using Azure SQL Database with geo-replication or auto failover group and if you are using logins and users rather than contained users you can end up with the following error when you try to connect to the secondary/failover database after failover occured.   If you already created a login on the failover server using SQL command -- ====================================================================================== -- Create SQL Login template for Azure SQL Database and Azure SQL Data Warehouse Database --...
read more

SQL Server / ALTER INDEX / CREATE INDEX Progress

SQL Server / ALTER INDEX / CREATE INDEX Progress
Creating a new and altering existing indexes on the big tables can be time-consuming. There is a simple way to see the progress of the CREATE / ALTER INDEX command. Since SQL Server 2014 we can use sys.dm_exec_query_profiles DMV to monitor real-time query progress. sys.dm_exec_query_profiles (SQL SERVER 2014 and above) “Monitors real-time query progress while the query is in execution. For example, use this DMV to determine which part of the query is running slow. Join this DMV with other system DMVs using the columns identified in the description field”   The first thing we need to...
read more

PowerShell / SQL Server / ConnectionContext.StatementTimeout

PowerShell / SQL Server / ConnectionContext.StatementTimeout
Recently I had an issue with a database backup PowerShell script one of our customer using. The script was working fine until trying to do a backup of the large database. During backup attempts script starts failing with the following error: Exception calling "SqlBackup" with "1" argument(s): "Backup failed for Server 'ServerName'. " At line:67 char:1 + $dbBackup.SqlBackup($s) + ~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : FailedOperationException Exception calling "SqlBackup" with "1" argument(s): "Backup failed for Server...
read more

PowerShell / SQL Server Backup / Cannot find type Microsoft.SqlServer.Management.Smo.Backup

PowerShell / SQL Server Backup / Cannot find type Microsoft.SqlServer.Management.Smo.Backup
In one of my previous posts, I show you how to use PowerShell to manage your SQL Server database backups and how to install the SQL Server PowerShell module. To be able to manage SQL Server Database Backups you will need to load Microsoft.SqlServer.SmoExtended assembly. If not you can end up with the following error. PS>TerminatingError(New-Object): “Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: verify that the assembly containing this type is loaded.” new-object : Cannot find type [Microsoft.SqlServer.Management.Smo.Backup]: verify that the assembly containing this type...
read more

PowerShell / SQL Server / Cannot find type Microsoft.SqlServer.Management.Smo.Server

PowerShell / SQL Server / Cannot find type Microsoft.SqlServer.Management.Smo.Server
Some users complained that they cannot use my PowerShell script published on TechNet https://gallery.technet.microsoft.com/Use-PowerShell-to-Backup-3bb0a397 It is a PowerShell script to Backup all user databases to Azure Storage. They experienced the following error: 8/14/2018 11:18 AM – Setting Folders PS>TerminatingError(New-Object): “Cannot find type [Microsoft.SqlServer.Management.Smo.Server]: verify that the assembly containing this type is loaded.” new-object : Cannot find type [Microsoft.SqlServer.Management.Smo.Server]: verify that the assembly containing this type is...
read more

SQL Server / Missing Indexes Query / DMV / Email Alert

SQL Server / Missing Indexes Query / DMV / Email Alert
This is an example of missing index query (DMV) of the database within email alert, we already had examples how you can use sp_send_dbmail stored procedure from msdb database to send backup size email daily,  long-running queries report and how to send missing indexes (Cached Plans) report via email. This is just another use case of sp_send_dbmail stored procedure. I used following arguments to achieve this @profile_name (name of an existing Database Mail profile, type sysname, default NULL), If you are not sure about your profile account use the following script, it shows all the accounts for all...
read more

SQL Server / Query Optimization / TempDB Page Splits / Table Spool (Lazy Spool)

SQL Server / Query Optimization / TempDB Page Splits / Table Spool (Lazy Spool)
Recently I had a customer with a really slow query execution and a case of large page splits number on the temporary database. In this example, I tried to replicate the similar issue on my local SQL instance using StackOverflow database. You can download this StackOverflow DB from Brent Ozar web page, just follow the instructions How to Download the Stack Overflow Database via BitTorrent   Firstly I created an Extended Event session to follow up if the page splits occur. DBCC DROPCLEANBUFFERS CREATE EVENT SESSION [PageSplits] ON SERVER ADD EVENT sqlserver.page_split ADD TARGET...
read more

« Previous Entries Next Entries »