Help: SQL Server

Sharing my knowlege about SQL Server Troubleshooting Skills

  • Blog Stats

    • 1,744,322 hits
  • Select GETDATE()

    April 2020
    M T W T F S S
     12345
    6789101112
    13141516171819
    20212223242526
    27282930  

Posts Tagged ‘ssms’

Tips and Tricks : Delete Complete History and Settings of Management Studio

Posted by blakhani on July 15, 2014


How many times you have browsed internet and cleared complete history of Internet Explorer (or any other browser)? I have done it many times and due to various reasons. Few days back one of my friend asked that how can same thing be achieved in SQL Server 2014 Management Studio? His goal was to delete all the saved setting of management studio for his login. This would include:

  • Server List and Login credentials saved on login screen.
  • History of reports launched – (Read Pinal’s blogs)
  • Settings change in Object Explorer Details like sorting and ordering of columns.
  • All setting which are saved under Tools > Options in menu bar.

(This does NOT effect registered severs list)

This all information is saved under file SQLStudio.bin.

Below is the file in SQL 2012 Management Studio. (Go to start>run> and paste below path)

%appdata%\Microsoft\SQL Server Management Studio\11.0\SqlStudio.bin

Below is the file in SQL 2014 Management Studio.

%appdata%\Microsoft\SQL Server Management Studio\12.0\SqlStudio.bin

image

In earlier version of SQL Server (before SQL 2012), the path was different. Refer Jonathan’s blog

Hope this helps.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in SQL Server Management Studio, SSMS | Tagged: , , , , , | Leave a Comment »

    Troubleshooting : Slow Delete Database from Management Studio

    Posted by blakhani on July 10, 2014


    Not very long ago, I had a database on my SQL 2012 Instance which was getting log shipped happily at frequency of 1 minute. Long long ago I have done such configuration for a demo purpose and forgot. Today I had “Memory Recall” when space was getting filled up with log backups. Since the demo was complete, I decided to drop the database. So I broke log shipping and tried dropping the database. What you do as a DBA to drop a database? Right Click > Delete .. huh?

    image

    When I clicked on “OK” button it was taking a long time. GUI to seem like its hanging. As usual, troubleshooting started! Ran my standard troubleshooting query to find out what is going on.

     SELECT s.session_id
        ,r.STATUS
        ,r.blocking_session_id 'Blk by'
        ,r.wait_type
        ,wait_resource
        ,r.wait_time / (1000.0) 'Wait Sec'
        ,r.cpu_time
        ,r.logical_reads
        ,r.reads
        ,r.writes
        ,r.total_elapsed_time / (1000.0) 'Elaps Sec'
        ,Substring(st.TEXT, (r.statement_start_offset / 2) + 1, (
                (
                    CASE r.statement_end_offset
                        WHEN - 1
                            THEN Datalength(st.TEXT)
                        ELSE r.statement_end_offset
                        END - r.statement_start_offset
                    ) / 2
                ) + 1) AS statement_text
        ,Coalesce(Quotename(Db_name(st.dbid)) + N'.' + Quotename(Object_schema_name(st.objectid, st.dbid)) + N'.' + Quotename(Object_name(st.objectid, st.dbid)), '') AS command_text
        ,r.command
        ,s.login_name
        ,s.host_name
        ,s.program_name
        ,s.host_process_id
        ,s.last_request_end_time
        ,s.login_time
        ,r.open_transaction_count
    FROM sys.dm_exec_sessions AS s
    INNER JOIN sys.dm_exec_requests AS r ON r.session_id = s.session_id
    CROSS APPLY sys.dm_exec_sql_text(r.sql_handle) AS st
    WHERE r.session_id != @@SPID
    ORDER BY r.cpu_time DESC
        ,r.STATUS
        ,r.blocking_session_id
        ,s.session_id

    Here was the result (I have removed few columns to avoid clutter)

    session_id status cpu_time logical_reads writes Elaps Sec statement_text command_text
    60 runnable 247734 42732577 29196 545.223 DELETE msdb.dbo.backupmediaset

    FROM msdb.dbo.backupmediaset bms

    WHERE bms.media_set_id IN (SELECT media_set_id

         FROM @media_set_id)

        AND ((SELECT COUNT(*)

      FROM msdb.dbo.backupset

      WHERE media_set_

    [msdb].[dbo].[sp_delete_database_backuphistory]

     

    Why would delete database do that? Well, it’s done by a small little checkbox which we never noticed.

    image

    That little checkbox executed this command (along with drop database). If we use “Script” button, this is the outcome

    EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'AdventureWorks2014'
    GO
    USE [master]
    GO
    DROP DATABASE [AdventureWorks2014]
    GO
    
    

    Now we know why it’s taking time but can this be made faster? Well, I check msdb database and there are few indexes which have been added in SQL Server 2014 which would help in this situation. Here is the quick comparison.

    Select @@version
    go
    SELECT 
         TableName = t.name,
         IndexName = ind.name,
         ColumnName = col.name
    FROM 
         sys.indexes ind 
    INNER JOIN 
         sys.index_columns ic ON  ind.object_id = ic.object_id and ind.index_id = ic.index_id 
    INNER JOIN 
         sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id 
    INNER JOIN 
         sys.tables t ON ind.object_id = t.object_id 
    WHERE 
         ind.is_primary_key = 0 
         AND t.is_ms_shipped = 1
         AND t.name in ( 'backupfile', 'backupfilegroup', 'backupmediafamily', 'backupmediaset', 'backupset', 'restorefile', 'restorefilegroup', 
    'restorehistory' ) ORDER BY t.name, ind.name, ind.index_id, ic.index_column_id

    image

    image

    If you are facing the same problem which I described on SQL 2008 or SQL 2012, you may want to try creating new indexes as advised on other blogs (search for “msdb performance tuning” in bing/google) but my only suggestion is that it might be unsupported.

    If you clean msdb backup history regularly, you might not face the issue though. There is maintenance plan to do that. Try it out!

    Hope this helps.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Troubleshooting | Tagged: , , , , | 4 Comments »

    SSMS Tip: How to drop multiple objects?

    Posted by blakhani on June 3, 2014


    Have you even been into situation where you have to drop many tables in a database? Or drop multiple databases? As a developer, you might right click on each table/database and choose “delete”. If you are equipped with little T-SQL knowledge, you might write a script to do the same. Do you know that management studio can do it?

    Let’s create multiple databases and learn the trick. I have used NEW_ID to get completely random name of databases.

    declare @number_of_databases int, @loop int
    select @number_of_databases = 10
    select @loop = 1
    declare @random_name varchar(40)
    declare @str nvarchar(100)
    
    while (@number_of_databases >= @loop)
    begin
    select @str = N'Create database [' + convert(varchar(100),NEWID())  +N']'
    exec sp_executesql @str
    set @loop=@loop+1
    end
    

    Once the script if executed, I have below in my management studio. Your databases would definitely be different.

    image

    Now, my task is – drop those databases which are created today. Without knowing the trick, one would get all databases name by query and then right click on database, choose delete. That would open a new window and we would choose “close existing connection..” and again hit OK. This has to be done 10 times. Now, here is my way of doing it.

    Click F7 on keyboard when SSMS is open. This would open “Object Explorer Details”. You can also use “View” in menu bar and choose “Object Explorer Details” item there. This would open a interface which is “details” on the object selected in “Object Explorer” . I would refer this as OED in later part of the blog. (OE of left and OED on right)

    image

    In OED, we have capability to sort the item based on various columns available. I want to sort databases them using date created. But by default that column is not shown. Let’s Right click on column header in OED and choose the column which we are interested. I have selected “Date Created”

    image

    Once that’s done, I can choose the position of the column by “dragging” the column name

    image

    Now, we can SORT as well by clicking on column name. I have sorted them by date created and as we can see all databases which I have created by script have listed next to each other. OED has capability the select multiple objects. This can be done by:

    • Use Shift key to choose all objects next to each other.
    • Use Ctrl key to choose multiple object using mouse

    Once desired objects are selected, use right click and choose delete. Simple. huh?

    image

    And once we choose delete, we get “Delete Objects” dialogue box which is similar to single delete.

    image

    We can choose the desired option and hit OK.

    Same operation would work for tables as well. We just need to choose proper node on “Object Explorer” and get details under “Object Explorer Details” – OED

    image

    Again, choose multiple tables in OED, right click and delete.

    I always felt that SQL Server Management Studio is one of “unexplored” and “underestimated” tool in SQL Server Product. Hope this blog has shown a little “hidden” gem of SSMS. If you know any such trick, please use comment section to share your knowledge.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in SQL Server Management Studio, SSMS | Tagged: , , , , , , , | 1 Comment »