List of DMV in SQL Server 2005 and 2008

Posted by Ravi Khanal on Jan 31, 2009

Dynamic Management Views(DMVs) are system views that surface the internal counters of the database engine and help present an easily comprehendible dashboard of the database engine performance that can be used to monitor the health of a server instance, diagnose problems, and tune performance.

List of DMV in SQL Server 2005

1. sys.dm_broker_activated_tasks
2. sys.dm_broker_connections
3. sys.dm_broker_forwarded_messages
4. sys.dm_broker_queue_monitors
5. sys.dm_clr_appdomains
6. sys.dm_clr_loaded_assemblies
7. sys.dm_clr_properties
8. sys.dm_clr_tasks
9. sys.dm_db_file_space_usage
10. sys.dm_db_index_usage_stats
11. sys.dm_db_mirroring_connections
12. sys.dm_db_missing_index_details
13. sys.dm_db_missing_index_group_stats
14. sys.dm_db_missing_index_groups
15. sys.dm_db_partition_stats
16. sys.dm_db_session_space_usage
17. sys.dm_db_task_space_usage
18. sys.dm_exec_background_job_queue
19. sys.dm_exec_background_job_queue_stats
20. sys.dm_exec_cached_plans
21. sys.dm_exec_connections
22. sys.dm_exec_query_memory_grants
23. sys.dm_exec_query_optimizer_info
24. sys.dm_exec_query_resource_semaphores
25. sys.dm_exec_query_stats
26. sys.dm_exec_query_transformation_stats
27. sys.dm_exec_requests
28. sys.dm_exec_sessions
29. sys.dm_fts_active_catalogs
30. sys.dm_fts_index_population
31. sys.dm_fts_memory_buffers
32. sys.dm_fts_memory_pools
33. sys.dm_fts_population_ranges
34. sys.dm_io_backup_tapes
35. sys.dm_io_cluster_shared_drives
36. sys.dm_io_pending_io_requests
37. sys.dm_os_buffer_descriptors
38. sys.dm_os_child_instances
39. sys.dm_os_cluster_nodes
40. sys.dm_os_hosts
41. sys.dm_os_latch_stats
42. sys.dm_os_loaded_modules
43. sys.dm_os_memory_allocations
44. sys.dm_os_memory_cache_clock_hands
45. sys.dm_os_memory_cache_counters
46. sys.dm_os_memory_cache_entries
47. sys.dm_os_memory_cache_hash_tables
48. sys.dm_os_memory_clerks
49. sys.dm_os_memory_objects
50. sys.dm_os_memory_pools
51. sys.dm_os_performance_counters
52. sys.dm_os_ring_buffers
53. sys.dm_os_schedulers
54. sys.dm_os_stacks
55. sys.dm_os_sublatches
56. sys.dm_os_sys_info
57. sys.dm_os_tasks
58. sys.dm_os_threads
59. sys.dm_os_virtual_address_dump
60. sys.dm_os_wait_stats
61. sys.dm_os_waiting_tasks
62. sys.dm_os_worker_local_storage
63. sys.dm_os_workers
64. sys.dm_qn_subscriptions
65. sys.dm_repl_articles
66. sys.dm_repl_schemas
67. sys.dm_repl_tranhash
68. sys.dm_repl_traninfo
69. sys.dm_tran_active_snapshot_database_transactions
70. sys.dm_tran_active_transactions
71. sys.dm_tran_current_snapshot
72. sys.dm_tran_current_transaction
73. sys.dm_tran_database_transactions
74. sys.dm_tran_tran_locks
75. sys.dm_tran_session_transactions
76. sys.dm_tran_top_version_generators
77. sys.dm_tran_transactions_snapshot
78. sys.dm_tran_version_store

New DMV in SQL Server 2008
1. sys.dm_audit_actions
2. sys.dm_audit_class_type_map
3. sys.dm_cdc_errors
4. sys.dm_cdc_log_scan_sessions
5. sys.dm_cryptographic_provider_properties
6. sys.dm_database_encryption_keys
7. sys.dm_db_mirroring_auto_page_repair
8. sys.dm_db_mirroring_past_actions
9. sys.dm_db_persisted_sku_features
10. sys.dm_db_script_level
11. sys.dm_exec_procedure_stats
12. sys.dm_exec_trigger_stats
13. sys.dm_filestream_file_io_handles
14. sys.dm_filestream_file_io_requests
15. sys.dm_fts_fdhosts
16. sys.dm_fts_outstanding_batches
17. sys.dm_os_dispatcher_pools
18. sys.dm_os_dispatchers
19. sys.dm_os_memory_brokers
20. sys.dm_os_memory_node_access_stats
21. sys.dm_os_memory_nodes
22. sys.dm_os_nodes
23. sys.dm_os_process_memory
24. sys.dm_os_spinlock_stacks
25. sys.dm_os_sys_memory
26. sys.dm_resource_governor_configuration
27. sys.dm_resource_governor_resource_pools
28. sys.dm_resource_governor_workload_groups
29. sys.dm_server_audit_status
30. sys.dm_tran_commit_table
31. sys.dm_xe_map_values
32. sys.dm_xe_object_columns
33. sys.dm_xe_objects
34. sys.dm_xe_packages
35. sys.dm_xe_session_event_actions
36. sys.dm_xe_session_events
37. sys.dm_xe_session_object_columns
38. sys.dm_xe_session_targets
39. sys.dm_xe_sessions


How to Synchronize Logins and manage Orphaned Users

Posted by Ravi Khanal on Dec 19, 2008

Database movement between servers is one of the common task for the DBA. But when the database is moved from one server to another or from one instance to other, the logins do not get transferred. So as a DBA you have to manually transfer the logins to remove this problem.

So let’s discuss what is the cause behind this. SQL Server logins are stored in the master database and they are mapped against individual databases. The Login informations on SQL Server 2005 are visible through sys.server_principals system catalog view and the sys.logins view. The mapping information of the database user is stored in the sysusers system table. This table includes the name of the database user and the SID of the corresponding SQL Server Login. The SID is the security identifier and SID tracks the logins throughout the entire systems.

So when we move or copy our database from one server to another or from one instance to another then the windows login will have the same SID but the SQL logins may have the different SIDs. So when the SID of the new server instance and the older instance doesn’t matches we have a Orphaned User.

Read the rest of this entry »


SSIS Package for merging data sources and applying transformations

Posted by Ravi Khanal on Dec 14, 2008

Scenario:
I have file in different heterogeneous sources like Text file and Excel File and I want to merge it into the SQL Server Table. But before loading the data into SQL Server table I have to change the First Name Column into Upper Case and also I need to give 20% raise in the salary. So I should have the proposed salary column added to the table.

Solution:

So First I need to have the data flow task for this.

Then in the data flow the package would be like this:

Read the rest of this entry »


How to remove fragmentation in table?

Posted by Ravi Khanal on Dec 8, 2008

Hey, Our Application is down!

This is the common thing you hear while you are working as a DBA. The reason for this is not always evident. Perhaps the number of transactions issued has increased or maybe the volume of the data has increased. But sometimes this might not be the cause of the problem. The problem might be due to disorganization of the database. Database disorganization occurs when a database’s logical and physical storage allocation contains many scattered areas of storage that are too small, not physically contiguous, or too disorganized to be used.

Database disorganization or fragmentation can occur when modifying data with INSERT, UPDATE, or DELETE statements, which over time cause gaps in each page. If a query search is based on a table scan or partial table scan, then it will create overhead for the SQL Server process with additional page reads, leading to high CPU activity and unresponsiveness.

Read the rest of this entry »


Sample SSIS Package

Posted by Ravi Khanal on Dec 6, 2008

Scenario:

In the inbound location, I have to create a directory which would be like “DealerSales and Current Date” and then download files from the remote location to that directory. Then I have to move the files from there to the working directory. Then I have to unzip the zip files and load the text files to the database and delete the text files from the working directory and move the zip from the working directory to the Archive folder. The directory’s date format should be in MMDDYYYY format.

Solution:

There could be a different approach to make the SSIS Package. One of the easier way to make the Package for this type of scenario could be like this:

  1. Let’s first have the File System Task which will create a directory like “DealerSales12052008″ inside download directory, if it doesn’t exist.
  2. Then let’s have the FTP Task which will download files from remote location to that folder.
  3. Then let’s have the File System Task which will create a directory like “DealerSales12052008″ inside the working directory, if it doesn’t exist.
  4. Then let’s have the For Each Loop Container and File System task which will move files from download directory to the working directory.
  5. Then let’s have the For Each Loop Container and Execute Process Task which will unzip the Zip files.
  6. Then let’s have the For Each Loop Container and Data Flow Task which will load all the text files into the SQL Server database.
  7. Then let’s have the File System Task which will create a directory like “DealerSales12052008″ inside the Archive Directory.
  8. Then let’s have the For Each Loop Container and File System Task which will move the zip files to the Archive Directory.
  9. Then let’s have the For Each Loop Container and File System Task which will delete the text files from the working directory.
  10. Then let’s have a File System tasks which will clean up our download directory and working directory.

So now let’s begin by making the skeleton of the package by dragging the task and which would look like this:

Read the rest of this entry »


SQL Injection Attack

Posted by Ravi Khanal on Nov 10, 2008

SQL Injection is an attack technique used to exploit web sites by altering backend SQL Statements through manipulating application input. It is a technique that takes advantage of non-validated user input data to inject SQL Statements dynamically into the existing ones for execution, and hence, poses a severe security threat to the supposedly secure systems. If attacker can get through some of the system stored procedures and some database server functions, then not only they can steal data from the databases, but also modify and delete it.

Some Examples of SQL Injection attack:

Example 1:

If there is a form which gets value from a Form.

If the form looks like:

Enter Product ID: 124 OR 2=2

Then the corresponding SQL Statement for that will be
select * from products where ProductId = 124 OR 2=2
This statement will always return a Value since 2=2 is always true.

Example 2:

If there is a login validation form and a user inputs like this then,
Login: ‘OR”=’
Password: ‘OR”=’

then the corresponding SQL statement will be

Select IsAuthorized FROM Users where Login = ' ' OR"=" AND Password = ' ' OR"="
Read the rest of this entry »


T-SQL vs PL-SQL

Posted by Ravi Khanal on Oct 15, 2008

Microsoft’s T-SQL is similar to Oracle’s PL-SQL to some extent, but they are very different in some important and basic ways. Some of the basic differences are:

  1. The difference in the datatype:
  2. T-SQL  PL-SQL
    Integer, SmallInt, TinyInt, Bit NUMBER
    Varchar Varchar2
    Text CLOB
    IMAGE BLOB
    BINARY(n), VARBINARY RAW(n) or BLOB
    MONEY NUMBER(19,4)
    DATETIME, SMALL-DATETIME DATE
    NVARCHAR(n) VARCHAR(n*2)
    TIMESTAMP NUMBER

     

  3. In T-SQL Datetime is represented as 1/300th Second and in PL-SQL date is represented as 1 second and Timestamp as 1/100 millionth Second.
  4.  

  5. The default date format of PL-SQL doesnot include time part.
  6.  

  7. In T-SQL, there is no need of dual. The T-SQL Syntax will look like:
        select getdate()
    The PL-SQL will look like
       slect sysdate from dual
  8.  

  9. INSTEAD OF trigger is used in SQL Server as an equivalent to Oracle’s BEFORE trigger.
  10.  

  11. CASE expressions can be used in SQL Server for Oracle’s DECODE() function.
  12.  

  13. In T-SQL there is an IDENTITY function which is more simpler than the SEQUENCE of PL-SQL. For implementing Sequence in PL-SQL, you have to create a new object called SEQUENCE and also we should have the trigger.
  14.  

  15. You cant have multiple databases on an Oracle Instance but you can have multiple databases on a SQL Server Instance.
  16.  

  17. In T-SQL there is SELECT INTO. In PL-SQL you have to use INSERT INTO.
    T-SQL: select getdate() newcolumn into newtable;
    PL-SQL: insert into newtable(newcolumn) values (sysdate);
  18.  
     

  19. In PL-SQL there is MINUS operator. In T-SQL you have to use NOT EXISTS clause in Select statement to generate the same result.
  20.  

  21. T-SQL’s RAISEERROR does not break the flow. It simply returns an error string or message but the stored procedure still returns normally. PL-SQL’s raise_application_error throws an exception, exits the current stored procedure, rolls back till implicit savepoint at the beginning of the stored procedure.
  22.  

  23. In T-SQL, stored procedure do not rollback automatically if something fails. You can use TRY CATCH but still the roll back is optional. In PL-SQL, stored procedures are atomic. Any error inside a stored procedure rolls back up to the point where the stored procedure was called.
  24.  


Transaction log is full

Posted by Ravi Khanal on Sep 21, 2008

Transaction log is also major cause for slowing the Performance of SQL Server. As a Database Administrator you should monitor your transaction log and check whether it is full or not. Transaction log is a WRITE AHEAD LOG. So whenever there is any data modification in the database, then “before image” and “after image” of any data would be written in the transaction log. If the transactions that are making the data modifications is running or its not being committed then these logs in the transaction log would be the active portion of transaction log. The committed transaction log would be inactive log in the transaction log. The lazy writer thread wakes up after certain period of time and it would write those logs to the physical disks. But sometime if the transaction are too long and its doing lots of updates then it would make the transaction log full and you will get “Out of disk” error. If you are maintaing full Recovery mode and never taking any log backup then its another major cause for transaction log to become full. Transaction log is the important component of SQL Server for recovering from any disaster scenarios. So as a Database Administrator you should always have a close look on the Transaction Log.

So you should use some DBCC command on the regular basis to find out about the Transaction Log.

                       This will tell you how much transaction log is being used.

                       This will tell you about the virtual logs inside the transaction log.

                      This will show you the open transactions or the transaction that are not yet committed. Read the rest of this entry »


Tempdb best practices

Posted by Ravi Khanal on Aug 26, 2008

Tempdb database provides temporary space for various operations and is recreated every time SQL Server is restarted. It is a temporary database to store temporary tables, table variables, cursors, work tables, row versioning. Proper optimization of the tempdb database increases the performance of your SQL Server.

Some of the best Practices for tempdb are: Read the rest of this entry »


Tips for Performance Monitoring

Posted by Ravi Khanal on Jul 20, 2008

Performance Tuning is the most important job you should perform as a Database Administrator. The most important thing you should consider for monitoring the performance of the server are:

Troubleshoot the Slow running queries:

                   DBCC SHOWCONTIG(’JOBS’)

                   DBCC INDEXDEFRAG(NORTHWIND, ORDERS, CUSTOMERS )

                   DBCC DBREINDEX(AUTHORS, ‘clustered index’, 70)

  Read the rest of this entry »