concept session in category sql server

appears as: session, sessions, session, A session, sessions
Learn Windows SQL Server Administration in a Month of Lunches

This is an excerpt from Manning's book Learn Windows SQL Server Administration in a Month of Lunches.

Once you’re done, click OK to close the window. You’ll need to right-click the new session in SQL Server Management Studio, and select Start to start the session. When any new blocked process reports are generated, they’ll be listed underneath that session. These reports show what processes are being blocked, what database they were trying to access, what kind of lock they were trying to obtain, and so on. This helps me figure out what exactly is happening, and to provide useful information to a developer or vendor.

Live data isn’t always what you want; sometimes you want to run a session all day, or overnight, and analyze what was captured at some later time. No problem. Take a look at figure 17.8.

Figure 17.8. Expand the session to access its logs or buffer.

You can see that my session expands to show the ring buffer being used to store event information. That’s because, when I created the session, I elected to keep the most recent 1,000 events in memory, remember? The ring buffer is where those live. The buffer stores captured information in XML, and you can click the buffer data to display the XML, as shown in figure 17.9.

17.2. Creating and using a session

Because sessions can be a little hard to set up, SQL Server includes the New Session Wizard, which helps create a new session based on predefined templates provided by Microsoft. I’ll walk you through an example that tracks query batch information, and then discuss some of the permutations offered by the Wizard.

SQL Server DMVs in Action: Better Queries with Dynamic Management Views

This is an excerpt from Manning's book SQL Server DMVs in Action: Better Queries with Dynamic Management Views.

Table 1.1. The major DMV groups

DMV group

Description

Change data capture Change data capture relates to how SQL Server captures change activity (inserts, updates, and deletes) across one or more tables, providing centralized processing. It can be thought of as a combination of trigger and auditing processing in a central area. These DMVs contain information relating to various aspects of change data capture, including transactions, logging, and errors. This group of DMVs occurs in SQL Server 2008 and higher.
Common Language Runtime The Common Language Runtime allows code that runs on the database to be written in one of the .NET languages, offering a richer environment and language and often providing a magnitude increase in performance. These DMVs contain information relating to various aspects of the .NET Common Language Runtime, including application domains (these are wider in scope than a thread and smaller than a session), loaded assemblies, properties, and running tasks.
Database These DMVs contain information relating to various aspects of databases, including space usage, partition statistics, and session and task space information.
Database mirroring The aim of database mirroring is to increase database availability. Transaction logs are moved quickly between servers, allowing fast failover to the standby server. These DMVs contain information relating to various aspects of database mirroring, including connection information and pagerepair details.
Execution These DMVs contain information relating to various aspects of query execution, including cached plans, connections, cursors, plan attributes, stored procedure statistics, memory grants, query optimizer information, query statistics, active requests and sessions, SQL text, and trigger statistics.
Extended events Extended events allow SQL Server to integrate into Microsoft’s wider event-handling processes, allowing integration of SQL Server events with logging and monitoring tools. This group of DMVs occurs in SQL Server 2008 and higher.
Full-text search Full-text search relates to the ability to search character-based data using linguistic searches. This can be thought of as a higher-level wildcard search. These DMVs contain information relating to various aspects of fulltext search, including existing full-text catalogs, index populations currently occurring, and memory buffers/pools.
Index These DMVs contain information relating to various aspects of indexes, including missing indexes, index usage (number of seeks, scans, and lookups, by system or application, and when they last occurred), operational statistics (I/O, locking, latches, and access method), and physical statistics (size and fragmentation information).
Input/Output (I/O) These DMVs contain information relating to various aspects of I/O, including virtual file statistics (by database and file, number of reads/writes, amount of data read/written, and I/O stall time), backup tape devices, and any pending I/O requests.
Object These DMVs contain information relating to various aspects of dynamic management objects; these relate to object dependencies.
Query notification These DMVs contain information relating to various aspects of query notification subscriptions in the server.
Replication These DMVs contain information relating to various aspects of replication, including articles (type and status), transactions, and schemas (table columns).
Resource Governor In the past, running inappropriate ad hoc queries on the database sometimes caused timeout and blocking problems. SQL Server 2008 implements a resource governor that controls the amount of resources different groups can have, allowing more controlled access to resources. These DMVs contain information relating to various aspects of Resource Governor, including resource pools, governor configuration, and workload groups. This group of DMVs occurs in SQL Server 2008 and higher.
Service Broker Service Broker is concerned with providing both transactional and disconnected processing, allowing a wider range of architectural solutions to be created. These DMVs contain information relating to various aspects of Service Broker, including activated tasks, forwarded messages, connections, and queue monitors.
SQL Server Operating System These DMVs contain information relating to various aspects of the SQL Server Operating System (SQLOS), including performance counters, memory pools, schedulers, system information, tasks, threads, wait statistics, waiting tasks, and memory objects.
Transaction These DMVs contain information relating to various aspects of transactions, including snapshot, database, session, and locks.
Security These DMVs contain information relating to various aspects of security, including audit actions, cryptographic algorithms supported, open cryptographic sessions, and database encryption state (and keys).

Now we’ve identified the types of object and the space they’re consuming, it would be worthwhile to drill down on the sessions that have used this space. This would allow us to identify specific sessions (users/connections) that might be causing space usage problems.

9.2.2. Space used and not reclaimed in tempdb by session

This script shows you how much space is still being used by the completed batches of SQL queries. Usually, any allocated space is reclaimed. This script is useful in showing which sessions still have tempdb space associated with them.

Listing 9.5. Space used and reclaimed in tempdb for completed batches
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT CAST(SUM(su.user_objects_alloc_page_count + su.internal_objects_alloc_page_count) * (8.0/1024.0) AS DECIMAL(20,3)) AS [SpaceUsed(MB)] , CAST(SUM(su.user_objects_alloc_page_count - su.user_objects_dealloc_page_count + su.internal_objects_alloc_page_count - su.internal_objects_dealloc_page_count) * (8.0/1024.0) AS DECIMAL(20,3)) AS [SpaceStillUsed(MB)] , su.session_id , ec.connection_id , es.login_name , es.host_name , st.text AS [LastQuery] , ec.last_read , ec.last_write , es.program_name FROM sys.dm_db_session_space_usage su INNER JOIN sys.dm_exec_sessions es ON su.session_id = es.session_id LEFT OUTER JOIN sys.dm_exec_connections ec ON su.session_id = ec.most_recent_session_id OUTER APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) st WHERE su.session_id > 50 GROUP BY su.session_id, ec.connection_id, es.login_name, es.host_name , st.text, ec.last_read, ec.last_write, es.program_name ORDER BY [SpaceStillUsed(MB)] DESC

This script uses three DMVs and one DMF to find the space used and not reclaimed by sessions; a brief description of each is given in table 9.3.

Table 9.3. DMVs/DMF that reveal tempdb space used and not reclaimed by sessions for completed batches

DMV/DMF

Description

sys.dm_db_session_space_usage Contains details of space used by user and internal objects for each session. Specifically, contains the number of pages allocated and de-allocated by each session.
sys.dm_exec_sessions Contains details of sessions on SQL Server.
sys.dm_exec_connections Contains details of connections to SQL Server.
sys.dm_exec_sql_text DMF that returns the SQL text identified by a given sql_handle

Joining the DMVs and the DMF gives us enough information to identify the tempdb space used and not reclaimed by each session, across all the databases on the server. The DMVs sys.dm_db_session_space_usage, sys.dm_exec_sessions, and sys.dm_exec_connections are joined on their common key column session_id. The connection contains the column most_recent_sql_handle, which is passed to the DMF sys.dm_exec_sql_text to retrieve the text of the SQL query. A LEFT OUTER JOIN is used to join sys.dm_exec_sessions and sys.dm_exec_connections because a session can have multiple connections.

Any system sessions that have a session_id of 50 or less are filtered out. The amount of space used by a session is calculated by summing user_objects_alloc_page_count and internal_objects_alloc_page_count. Similarly, the amount of space not reclaimed is calculated as the difference between user_objects_alloc_page_count and user_objects_dealloc_page_count added to the difference between internal_objects_alloc_page_count and internal_objects_dealloc_page_count. The results are sorted by the amount of space each session still owns. An example of the type of output for this query is shown in figure 9.5.

Figure 9.5. Output showing tempdb space used and reclaimed by session

The output shows, for completed batches, the amount of space in tempdb that each session still has associated with it, along with details of the session and connection that allow you to report on where the query is running from (host_name), who is running the SQL query (login_name), and the text of the last-completed SQL query. If space usage is a problem (as reported in the SQL Server error log), you can use the output to determine which session/user/SQL query is using the most space, with a view to taking corrective action. Such action includes rescheduling the queries to run at a time when more resources are available or rewriting the relevant query to use less tempdb space, for example, replacing a hash join with a nested loop join.