Tuesday, June 14, 2011

Temp DB Full and not able to Shrink? Restart SQL Server is a good option..

I know there are many different blogs about tempdb, but I just wanted to share what I have experienced in my environment.  TempDB was growing very fast and it already grew ~ 120GB but the used space is 100MB, general idea would be to restart the SQL Server services that way new TempDB is re-created.

So I tried first shrinking the files.

DBCC SHRINKFILE ('tempdev', 1024)

DBCC SHRINKFILE ('tempdev2', 1024)
Still not able to shrink
Command(s) completed. It is of no use hmmm interesting. 


tempdb_ID 
SELECT database_id,*,session_id FROM sys.dm_exec_requests it will give you session ID and by default I believe database_id for tempDB is 2.
Now we will see if there are any locks or open transactions by running (DBCC OPENTRAN) on tempBD

SELECT * FROM sys.dm_tran_locks where resource_database_id= 2
and to check open transactions you can also write query
SELECT * FROM sys.dm_exec_requests WHERE database_id = 2
I verified no locks and no open transactions. 

Now I have seen all the tables in tempDB
SELECT * FROM tempdb..sysobjects 
This may be due to many reasons, may be a developer or user has run a query or 
Stored Proc that caused tempDB grow and ran out of space, by creating cached objects on tempDB. When a table is created in Cache they are TRUNCATED instead DELETE because these tables can be reused when the stored procedure is run again.

So now what do you do? 
Clean Procedure Cache or Restart SQL Server Services? And of-course stored procedures have to be recompiled if you clean up Procedure cache, that is going to be a different topic.  But in my case I simply Cleared Procedure cache in SQL Server 2005 by executing 
DBCC FREEPROCCACHE WITH NO_INFOMSGS (this is used to prevent information messages from begin displayed) there by allowing my tempdb to shrink it to bare minimum. 

In sql server 2008 

Gives you plan_handle and cached entries that are reused

SELECT
             plan_handle, 
             objtype, 
             usecounts,
             cacheobjtype, 
             objtype, 
             text
FROM 
            sys.dm_exec_cached_plans
CROSS APPLY 
             sys.dm_exec_sql_text(plan_handle)
WHERE
             usecounts > 1
ORDER BY
              usecounts DESC;

Gives you memory breakdown of all cached compiled plans

SELECT
        plan_handle,
        ecp.memory_object_address AS CompiledPlan_MemoryObject,
        omo.memory_object_address,
        pages_allocated_count,
        type,
        page_size_in_bytes
FROM
        sys.dm_exec_cached_plans AS ecp
JOIN    sys.dm_os_memory_objects AS omo
   
    ON
        ecp.memory_object_address = omo.memory_object_address
    OR    ecp.memory_object_address = omo.parent_address


WHERE
        cacheobjtype = 'Compiled Plan';


So I took the plan_handle and cleared only that particular cache there by allowing me shrink TempDB
You can clear any specific plan from cache there by allowing tempdb to shrink and gain back space and avoid ing SQL Server services restart.
DBCC FREEPROCCACHE (0x0600040057AFE42740A14D80000000000000000000000000);



Sunday, June 12, 2011

Cannot connect to WMI provider.Invalid namespace[0x80041010]

Few days before I had experienced issue with the WMI not able to connect as shown in the below figure.

"Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 and later servers with the SQL Server Configuration Manager. Invalid namespace[0x80041010]") when you install a 32-bit version of Microsoft SQL Server 2008 on 64-bit machine and you install an 64-bit version of SQL Server 2008 on the same machine. If you uninstall any instances you will receive the error message when you open SQL Server Configuration Manager.
When you install sql server 2008 WMI provider also gets installed and if 2005already existed on the machine then WMI provider is common for both versions, Soif you uninstall SQL Server 2008 it deletes WMI Provider. This file is located in the %programfiles(x86)% folder.
The WMI (Windows Management Instrumentation) provider is a published layer that is used with the SQL Server Configuration Manager snap-in for Microsoft Management Console (MMC) and the Microsoft SQL Server Configuration Manager. It provides a unified way for interfacing with the API calls that manage the registry operations requested by SQL Server Configuration Manager and provides enhanced control and manipulation over the selected SQL Server services. The SQL Server WMI Provider is a DLL and a MOF file, which are compiled automatically by SQL Server Setup.
The resolution is, open command prompt run as ADMINISTRATOR.
mofcomp "%programfiles(x86)%\Microsoft\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"
 Note: The location may be different as shown in the figure below.


Tuesday, June 7, 2011

DMV sys.dm_os_waiting_tasks and sys.dm_exec_requests – Wait Type

This Query gives the current processes running in SQL Server. Also if you want to know what is stored in the Cache or what is running .

Once this is run you will get the following info.

wait_duration_ms – Means current wait of the query which is run at that time.
wait_type – Means the current wait type of the query
text – Has the Query in TEXT FORMAT
query_plan – Gives you the Query plan which could be very helpful to find some missing inxexes, etc and all that fun stuff.
Session_id, and all others(Columns) are pretty much self explanatory.


    select
            db_name(exec_re.database_id) databasename,
            wait_tsk.wait_duration_ms,
            wait_tsk.wait_type,
            exec_txt.text,
            exec_qpln.query_plan,
            wait_tsk.session_id,
            exec_ssion.cpu_time,
            exec_ssion.memory_usage,
            exec_ssion.logical_reads,
            exec_ssion.total_elapsed_time,
            exec_ssion.program_name,
            exec_ssion.status
    from
            sys.dm_os_waiting_tasks wait_tsk
           
            inner join sys.dm_exec_requests exec_re
                        on
                        wait_tsk.session_id = exec_re.session_id
           
            inner join sys.dm_exec_sessions exec_ssion
                        on
                        exec_ssion.session_id = exec_re.session_id
           
            cross apply sys.dm_exec_sql_text (exec_re.sql_handle) exec_txt
           
            cross apply sys.dm_exec_query_plan (exec_re.plan_handle) exec_qpln
           
    where
            exec_ssion.is_user_process = 1

Monday, June 6, 2011

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED NO LOCK

Everyone undergoes a blocking or locking issues when one forgets to write
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED NO LOCK or what ever...

In order to avoid such situation and which can also save a lot of time, you can setup READ UNCOMMITTED Option From SSMS>Tools>Options> Query Execution SQL Server> Advanced as shown below. also there is whole bunch of options available where you can play with .. 

Setting up Isolation Level









Sunday, June 5, 2011

Linked Server errors Microsoft SQL Server, Error:7411)

Server 'ServerName' is not configured for DATA ACCESS (Microsoft SQL Server, Error: 7411)

This occurs if any of the following are not configured, even if you are trying to security.
Not be Made,be made without using a security context, login;s security context and using security context.
Data access, RPC, RPC Out, Use Remote collation has to be configured in-order to set up linked server without having 7411 error.