NetInverse Developers Blog

July 8, 2009
Category: SQL — Tags: — admin @ 10:25 pm

Useful SQL Queries

Showing the plan hash values:

SELECT s.execution_count
      ,s.query_hash
      ,s.query_plan_hash
      ,t.text
FROM   sys.dm_exec_query_stats s
       CROSS APPLY sys.dm_exec_sql_text(s.plan_handle) t

Showing execution plans in the procedure cache:

SELECT * FROM sys.dm_exec_cached_plans

Showing fragmentation:

SELECT  s.avg_fragmentation_in_percent
       ,s.fragment_count
       ,s.page_count
       ,s.avg_page_space_used_in_percent
       ,s.record_count
       ,avg_record_size_in_bytes
FROM    sys.dm_db_index_physical_stats(DB_ID('AdventureWorks2008'),
                                       OBJECT_ID(N'dbo.t1'), NULL, NULL,
                                       'Sampled') AS s

Show SQL memory usages:

CACHESTORE_OBJCP: These are compiled plans for stored procedures, functions and triggers.
CACHESTORE_SQLCP: These are cached SQL statements or batches that aren’t in stored procedures, functions and triggers. This includes any dynamic SQL or raw SELECT statements sent to the server.

SELECT * FROM sys.dm_exec_cached_plans

SELECT * FROM sys.dm_os_memory_cache_clock_hands
WHERE TYPE IN ('CACHESTORE_SQLCP','CACHESTORE_OBJCP')
ORDER BY removed_all_rounds_count desc 

SELECT TOP 512
	st.text,
	cp.cacheobjtype,
	cp.objtype,
	cp.refcounts,
	cp.usecounts,
	cp.size_in_bytes,
	cp.bucketid,
	cp.plan_handle
FROM sys.dm_exec_cached_plans as cp
CROSS APPLY sys.dm_exec_sql_text(cp.plan_handle) as st
WHERE cp.cacheobjtype = 'Compiled Plan' AND cp.objtype = 'Prepared'
ORDER BY cp.usecounts desc

SELECT  TOP 10
	LEFT([name], 20) as [name],
	LEFT([type], 20) as [type],
	[single_pages_kb] + [multi_pages_kb] AS cache_kb,
	[entries_count]
FROM sys.dm_os_memory_cache_counters
ORDER BY single_pages_kb + multi_pages_kb DESC

SELECT TOP 10 type,
	(sum(single_pages_kb) + sum(multi_pages_kb)) as 'Total Pages(KB)',
	sum(single_pages_kb) as 'Single Pages(KB)',
	sum(multi_pages_kb) as 'Multi Pages(KB)',
	sum(virtual_memory_reserved_kb) as 'VM Reserved(KB)',
	sum(virtual_memory_committed_kb) as 'VM Committed(KB)',
	sum(awe_allocated_kb) as 'AWE Allocated(KB)',
	sum(shared_memory_reserved_kb) as 'Shared Memory Reserved(KB)',
	sum(shared_memory_committed_kb) as 'Shared Memory Committed(KB)'
FROM sys.dm_os_memory_clerks
GROUP BY type
ORDER BY sum(single_pages_kb) + sum(multi_pages_kb) desc

SQL Performance Tips

Top SQL Server 2005 Performance Issues for OLTP Applications: http://www.microsoft.com/technet/prodtechnol/sql/bestpractice/oltp-performance-issues.mspx
Troubleshooting Performance Problems in SQL Server 2005: http://technet.microsoft.com/en-us/library/cc966540.aspx
Batch Compilation, Recompilation, and Plan Caching Issues in SQL Server 2005: http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx

March 8, 2009
Category: SQL — Tags: , , , , — admin @ 12:33 am

SQL server only allows you to restrict access to the table columns indirectly. The approach is create views and stored procedures and grant them to ceitain users. Now SQL 2005 allows you to manuplate column-level permissions directly. So let’s look at a SQL sample below:

        CREATE TABLE student (
            id      INT,
            name    NCHAR(20),
            ssn     VARCHAR(11)
        )

        INSERT INTO student
        (id, name, ssn) VALUES (1, 'david', '999-12-1234')

        GRANT SELECT (id, name) ON student TO [DAFFIDIO\David]
        DENY SELECT (ssn) ON student TO [DAFFIDIO\David] --DENY SELECT

        Execute AS LOGIN  = 'DAFFIDIO\David'-- IMPERSONATE
        SELECT SUSER_NAME(), USER_NAME()

        SELECT id, name FROM student

        SELECT ssn FROM student 

        REVERT  -- undo IMPERSONATE

Now David can see id and name, but not ssn. He will see an error message as below:

   Msg 230, Level 14, State 1, Line 2
   SELECT permission denied on column 'ssn' of object 'student',
   database 'Test', schema 'dbo'.

The syntax is:

        GRANT { ALL [ PRIVILEGES ] }
              | permission [ ( column [ ,...n ] ) ] [ ,...n ]
              [ ON [ class :: ] securable ] TO principal [ ,...n ]
              [ WITH GRANT OPTION ] [ AS principal ]

You can use GRANT, REVOKE AND DENY to set granular permissions on column level now.

Category: SQL — Tags: , , — admin @ 12:23 am

SQL server’s non cluster index has two limitations:

  • Only 16 columns can be included
  • The maxium size of the index key can be only 900 bytes

With SQL 2005, you can overcome these limitations by using INCLUDE:

CREATE INDEX IDX_FOO ON FOO (COL1, COL2) INCLUDE (COL3)

Category: SQL — Tags: , , , , — admin @ 12:16 am

What is a deadlock?

A deadlock occurs when two system server process IDs (SPIDs) are waiting for a resource and neither process can advance because the other process is preventing it from getting the resource. For example, Andrew holds a lock on table A and requests a lock on table B; Lindsay holds a lock on table B and requests a lock on table A.

The lock manager(Lock Monitor)’s thread checks for deadlocks. When a lock manager’s deadlock detection algorithm detects a deadlock, the lock manager chooses one of the SPIDs as a victim. The lock manager initiates a 1205 error message that is sent to the client, and the lock manager kills the SPID.

Transaction (Process ID %d) was deadlocked on {%Z} resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

Killing the SPID frees the resources and allows the other SPID to continue. In a well designed application, the front-end application should trap for the 1205 error, reconnect to SQL Server, and then re-submit the transaction. Although deadlocks can be minimized, they cannot be completely avoided. That is why the front-end application should be designed to handle deadlocks.

So what can cause deadlocks?

  1. Locks
  2. Worker threads
  3. Memory
  4. Parallel query execution-related resources
  5. Multiple Active Result Sets (MARS) resources.

How to debug deadlocks

Turn on deadlock trace

          DBCC TRACEON(1204,1222)

This will enable deadlock tracing for all existing connetions and new. You can check out KB832524 for more details. Trace flag 1204 reports deadlock information formatted by each node involved in the deadlock. Trace flag 1222 formats deadlock information, first by processes and then by resources.

Use SQL Profiler to trace deadlock events and get the resource ID of the table or index under contention. The steps to do this are:

  1. Start SQL profiler
  2. On the Trace Properties dialog box, on the General tab, check Save to file and specify a path to save the trace
  3. Click the Events tab, only add Locks\Lock:deadlock and Locks\Lock:deadlock chain
  4. Click the Data columns tab, add DatabaseID, IndexID, ObjectID

This trace will record all deadlocks on this SQL Server instance, along with the ID of the source table of contention. To translate the database id and object id into names (although DatabaseName and ObjectName are selectable columns, the data is not always presented in the trace), you can do

           SELECT database_name(DatabaseID)
           SELECT object_name(ObjectID)

Use SQL Profiler to trace batch statements or RPC statements, to trace which statement causes the deadlock. The restriction of this is that if the repro is far between and you have a busy system, the trace file will grow too large to be useful.

You can also run following query to check which process is being blocked.

            SELECT * FROM sys.sysprocesses WHERE blocked <> 0

            Get the SPID from blocked column

            DBCC inputbuffer (SPID)

            sp_who2

            sp_lock2

You can also use SP_LOCk2 to receive detailed locking view.

Please also check out Detecting and Ending Deadlocks on MSDN.

Category: .Net, SQL — Tags: , — admin @ 12:12 am

Query Notifications is a powerful new feature, built in to SQL 2005, usable from ADO.NET 2.0 and from ASP.Net 2.0 directly.

Query Notifications allows you send a query to SQL server and request that a notification be generated if any row included in the query is changed.

Commands sent to server may include a tag that requires a notification. Whe the server sees this tag, it will create a notification subscription that fires once the result set is change for the query statement.

This is extremely useful for client side caching of results from database server. With this feature, you don’t have to pulling data periodically.

Category: SQL — Tags: , , — admin @ 12:07 am

DtcGetTransactionManager is typically the first DTC call that application programs and resource managers make when using DTC. This helper function establishes the initial connection to DTC. It returns an interface pointer to one of the interfaces on the DTC proxy core object.

DTC had been working properly untill recently a security patch was installed. DtcGetTransactionManager call fails. Actually this is caused by Group Policy object(GPO) setting applied. You can fix the issue by doing following steps:

  1. Click on Start -> Administrative Tools -> Domain Controller Security Policy
  2. Select Security Settings -> System Services
  3. Look for Distributed Transaction Coordinator on the right and right click Properties on it
  4. The security policy setting window will pop up
  5. Check the “Define this policy setting” check box
  6. Change the service startup mode to Automatic
  7. Click on Edit Security and add “Everyone” and give Full Control
  8. Click on OK, Click on OK
  9. Open a command window and type “gpupdate /force”
  10. Restart the “Distributed Transaction Coordinator” NT service
  11. iisreset
Category: SQL — Tags: — admin @ 12:04 am

You may want to check out this list of the deprecated features in SQL 2005.

I noticed that DBCC DBREINDEX is one on the list. SQL 2005 introduces a new REBUILD option to the ALTER INDEX command to replace it. This new REBUILD option can perform online or offline re-indexing operations.

DBCC DBREINDEX can only perform offline reindexing.

Category: SQL — Tags: — admin @ 12:02 am

Normally you use the trigger functionality with Data Manipulation Language (DML) commands such as INSERT, UPDATE, and DELETE. Now SQL 2005 introduces new Data Definition Language (DDL) triggers. You can use it to audit schema changes.

Example of DDL Trigger:

    CREATE TRIGGER safety
    ON DATABASE
    FOR DROP_TABLE, ALTER_TABLE
    AS
    PRINT ‘You must disable Trigger “safety” to drop or alter tables!’
    ROLLBACK;

    DISABLE TRIGGER safety ON DATABASE;

Please refer to MSDN for details: MSDN: Designing DDL Triggers.
An articel about using DDL triggers: Using SQL Server 2005 to document Sarbanes-Oxley compliance

March 7, 2009
Category: SQL — Tags: — admin @ 11:57 pm

In its continued commitment to interoperability, Microsoft released a new Java Database Connectivity (JDBC) driver with SQL Server 2005.

The SQL Server 2005 JDBC Driver download is available to all SQL Server users at no additional charge, and provides access to SQL Server 2000 and SQL Server 2005 from any Java application, application server, or Java-enabled applet. This is a Type 4 JDBC driver that provides database connectivity through the standard JDBC application program interfaces (APIs) available in J2EE (Java2 Enterprise Edition).

The SQL Server 2005 JDBC driver is JDBC 3.0 compliant and runs on the 1.4 JDK and higher. It has been tested against all major application servers including BEA WebLogic, IBM WebSphere, JBoss, and Sun. The new JDBC Driver is also freely redistributable for registered partners. -MSDN

What a great news for Java developers! Now you don’t have to rely on third party drivers.

Category: SQL — Tags: — admin @ 11:56 pm

SQL Server 2005 includes a redesigned enterprise data extraction, transformation, and loading (ETL) platform, called SQL Server Integration Services (SSIS), which replaces DTS 2000.

SSIS (SQL Server Intergration Services) has some new features:

  1. Business Inteligence Development Studio (BIDS) for creating DTS packages
  2. SSIS packages can be saved as XML .
  3. .Net languages can be used for creating SSIS components.
  4. SSIS allows FTP in/out
  5. SSIS has new comand line tools(DTSExec replaces DTSRun, DTSExecUI replaces DTSRunUI, DTSUtil).
  6. And more…
Older Posts »

©2009 NetInverse. All rights reserved. Powered by WordPress