-
How to Get SQL Server’s IP Address
When managing or troubleshooting SQL Server, you might need to get the IP address your SQL Server instance is listening on. While servers can listen on multiple IP addresses, SQL Server offers a straightforward way to determine which address is used for your current connection.
In this post I’ll share a few quick ways to obtain the server’s IP address directly from SQL Server.
1. Query to Get the SQL Server IP Address
We can query the
sys.dm_exec_connections
DMV to get IP address info for the SQL Server instance. You’ll need the VIEW SERVER STATE permission to run this:-- Get Running SQL Server IP SELECT local_net_address FROM sys.dm_exec_connections WHERE session_id = @@SPID; -- filter to current session
This approach avoids using
xp_cmdshell
, a method that should be disabled or tightly controlled on production servers. While grantingVIEW SERVER STATE
permissions may be necessary, it’s a lower security risk compared to enablingxp_cmdshell
.2. Get SQL Server IP Using
CONNECTIONPROPERTY()
Run the following query to get the IP address of the server for the current connection:
-- Get IP for SQL Server SELECT CONNECTIONPROPERTY('local_net_address') AS 'Server IP Address'; -- Get SQL hostname instead SELECT SERVERPROPERTY(N'MachineName');
This command will return the IP address if the connection uses the TCP transport protocol. Keep in mind that this method may not work in environments like Azure SQL Database or for connections that use other transport types, as it will return
NULL
in those cases.For more details, check out MS Docs on
CONNECTIONPROPERTY
.3. Checking Listening IPs & Ports for All Services
I have another post that will show you how to check all listening IPs and Ports on a Windows Computer. It’s a guide using Resource Monitor and TcpView, which might be useful for checking which service is tied to a specific port.
Hope all this helps!
-
Identify Missing & Unused Indexes in SQL Server
Indexes are crucial for SQL Server query performance. Everyone needs an Index. However, improper indexing strategies can degrade database performance, leading to slow queries and cause resource bottlenecks.
Effectively managing indexes involves not only identifying the missing ones that could improve performance, but also pinpointing unused indexes that unnecessarily consume system resources.
The following is shared in this post:
> Script 1: Check for Missing Indexes
> Script 2: Check for Un-used Indexes
> More InformationScript 1: Check for Missing Indexes
The script below returns missing index suggestions for the current database:
Use this script to identify and rank missing indexes based on SQL Server’s query analysis, prioritizing those with the highest impact. Refine suggestions based on your workload and performance goals. We should look at combining missing index suggestions with existing indexes to optimize performance.
For more information on this from Microsoft, have a look at this page: Tune nonclustered indexes with missing index suggestions
Script 2: Check for Unused Indexes
This script is for the indexes you don’t want. We’re able to check for un-used indexes indexes by analyzing index usage stats. Indexes with no recent seeks, scans, or lookups are considered candidates for removal.
To check for unused indexes in SQL Server, run the following:
Removing unused indexes can free up storage, reduce index maintenance overhead, and improve write performance. Before removing an index, ensure it is not indirectly supporting features such as indexed views or constraints.
More Information
To summarise, identifying missing indexes helps improve query performance, while removing unused indexes conserves resources and enhances system efficiency.
For more good information from myself, an experienced Microsoft SQL Database Admin, check out my post SQL Server Performance Tuning: Index Fragmentation Scripts, which is particularly useful to follow-on from this. The topic moves towards applying regular maintenance to the indexes you do want to keep around.
-
SQL Server Performance Tuning: Index Fragmentation Scripts
In this post I’m sharing more scripts to help with SQL Server performance tuning. This time, we’re looking at index and heap fragmentation which are useful for pinpointing inefficiencies on your database.
Fragmentation, whether in indexes or heaps, can lead to slower queries, inefficient storage, and increased maintenance times. Rebuilding fragmented indexes, converting heaps to clustered indexes, and maintaining good statistics can improve overall SQL Server health and performance.
The following scripts and information is included here:
> Script 1: Get Index Fragmentation Levels
> Script 2: Identifying Heap Fragmentation
> Addressing Fragmentation & Stale Stats
> More Performance Troubleshooting TipsScript 1: Get Index Fragmentation Levels
This script helps retrieves key information about your SQL Server indexes and their fragmentation levels:
Script 2: Identifying Heap Fragmentation
Heaps, or tables without clustered indexes, are prone to fragmentation. Fragmentation in heaps can impact query performance, and potentially increase the time it takes for full backups to complete.
Use this script to identify fragmented heaps:
The
ForwardRecordCount
column is of particular interest, if you see a huge number on a heap then perhaps it’s the reason queries are running slow. If it’s a small table, I wouldn’t worry about this much.Addressing Fragmentation & Stale Stats
After running the scripts above and identifying fragmentation, here’s how to address these issues:
Maintain Optimal Statistics
We don’t need to go too granular in explaining how to check if your statistics are good. We just need to make sure we keep them up to date, which can be done by having a Maintenance Plan for Updating Statistics.
For more information on updating stats, checkout this MS Docs Page: Update Statistics Task (Maintenance Plan). Stale statistics can seriously hurt query performance, be sure to refresh them periodically.
Index Maintenance
We should rebuild our indexes on tables to reduce fragmentation by using the
ALTER INDEX REBUILD
command. If on SQL Server Enterprise Edition you can perform this without impacting table availability using one of the available options. For indexes with lower fragmentation levels, consider using theALTER INDEX REORGANIZE
command. This is a more lightweight operation than rebuilding indexes but still helps to improve performance.It’s a good idea to set up maintenance plans for addressing fragmentation, especially on large tables, and schedule them during off-peak hours.
For more information, these Microsoft Documentation Pages will help:
> Index Rebuild vs Reorganize
> ALTER INDEX DocsRebuild or Avoid Heaps
Rebuilding a heap can help to eliminate fragmentation and optimize the table’s data storage.
You can convert a heap to a clustered index to resolve fragmentation and enhance sequential data access:
-- Create clustered index on a table with no clustered index (heap) CLUSTERED INDEX IX_ClusteredIndexName ON TableName(ColumnName);
More Performance Troubleshooting Tips
To provide you with some general follow-up material, I’ll recommend my other post, SQL Server Performance Troubleshooting Scripts. It covers identifying SQL worker thread issues, monitoring running queries, and optimizing stored procedures. It also contains links to check the health of High Availability (HA) environments, like Always On, replication, and Mirroring.
Feel free to leave a comment if there’s something I’ve missed or if you have any suggestions for improvement. Ty!
-
Show User Activity and Blocking in SQL Server
In this post I’m sharing a SQL script that will show currently running and blocking queries in SQL Server, with some other useful info along the way.
This is script I run often as a DBA. It’s useful for identifying blocking or long running transactions, or checking in on what’s throttling your server. We can use this information to get a view of what’s causing our SQL Server to run slow.
The following areas are covered in this one:
> Script to Show Current SQL Sessions
> Other Ways to Show SQL Activity
> Problem Queries or Performance IssuesScript to Show Current SQL Sessions
This script will show currently running SQL sessions, including commands being run, blocking sessions and other useful columns for review.
We can re-run the query a few times to get a better picture of the active queries coming and going. If it’s a busy SQL Server you might see 100’s of rows returned by this, some might have been running for a long time which we can see by reviewing the elapsed time column.
Other Ways to Show SQL Activity
I wanted to quickly highlight a few other ways to get this same SQL activity information.
sp_who2
: A built-in SQL Server stored procedure that provides a quick overview of active sessions, including session IDs, status, and blocking information.sp_whoisactive
: A popular community-provided stored procedure that offers detailed insights into currently running queries, waits, and resource usage.SQL Activity Monitor
: A graphical tool in SQL Server Management Studio (SSMS) that displays active queries, performance metrics, and resource usage in real time.3rd Party Tools
: Monitoring solutions like SolarWinds or Redgate, which offer advanced analytics, alerting, and customizable dashboards for SQL activity.Problem Queries or Performance Issues
When dealing with problem queries or performance issues in SQL Server, you might need to terminate problematic SPIDs. I’ve shared the syntax and guidelines in my post on killing SQL SPIDs—definitely check that out first to handle it properly.
For broader performance troubleshooting, take a look at my Performance Troubleshooting Scripts post. It also includes follow-up links for checking and maintaining high availability setups like Mirroring, Replication, and Availability Groups (AG).
-
Download and Install SQL Server Management Studio (SSMS)
In this short post, I’ll share how to easily download and install SQL Server Management Studio (SSMS) on your Windows computer. I’ll include some tips to help improve your SSMS experience too!
Step 1: Download SSMS
Go to the official Microsoft website to download the latest version of SQL Server Management Studio (SSMS).
Step 2: Install SSMS
1. Locate the installer file you just downloaded.
2. Run the installer and follow the simple on-screen instructions.That’s it! You now have SSMS ready to connect to your SQL Server. Search for ‘SSMS’ in your Start menu to launch the application.
Tips to Improve Your SSMS Experience
1. Enable the Dark Theme
This is a new feature as of SSMS v21 which is currently in preview release (coming soon!). There is another way enable Dark Mode in SSMS on older versions which needs a local config file edit.
To enable the SSMS Dark Mode:
1. Go to Tools > Options > General.
2. Under the Color theme dropdown, choose Dark Mode.2. Use Keyboard Shortcuts
ALT+X
: Alt + ‘x’ will run your query, I use that instead of F5.Ctrl+N
: Open new Query Window.Ctrl+R
: Toggle Results Pane (Hide/Show).Ctrl+L
: Display Query Execution Plan.Ctrl+T
: Results to Text (Output mode).Ctrl+Shift+F
: Results to File.Ctrl+M
: Include Execution Plan.Ctrl+K,Ctrl+C
: Comment Selected Code.Ctrl+K,Ctrl+U
: Uncomment Selected Code.Ctrl+F
: Find Text in Query Window.Ctrl+H
: Replace Text.Ctrl+Shift+U
: Convert Selected Text to Uppercase.Ctrl+Shift+L
: Convert Selected Text to Lowercase.Ctrl+G
: Go to Specific Line Number.Ctrl+Shift+R
: Refresh IntelliSense Cache.3. Show Line Numbers in Query Window
Check out my other post, enabling line numbers in SQL Management Studio for more info.
4. Use 3rd Party Tools or Upcoming Features
Tools like Redgate SQL Toolbelt can prefill queries, tables, and objects, streamlining repetitive tasks. With the release of SQL Server 2025 and future SSMS versions, many of these capabilities will be built directly into the platform for improved efficiency.
5. Install SSMS via Command
We can install SSMS using software such as SCOM, or using custom scripts. My other post, How to Silently Install SSMS might help you get SSMS installed quickly.