Skip to content

Searching SQL Server objects across large estates

Plan object search across a large SQL Server estate: live queries versus a central index, permissions, offline servers, triaging results and keeping sensitive code safe.

Searching for an object on one server is a quick query. Searching hundreds of servers and thousands of databases is a different problem. Some servers are slow, some are offline, some you don't have access to, the same database exists in five environments, and a search for a common word returns ten thousand rows.

This guide is about the strategy rather than the query: how to choose between searching live and searching an index, how to handle access and unreachable servers, how to make sense of large result sets and how to keep sensitive code safe. For the queries themselves, see our guide to [searching SQL Server objects](/guides/searching-sql-server-objects).

Live search or a central index?

There are two basic approaches.

**Live, federated search** runs the search query against every server at the moment you need it, through a Central Management Server, PowerShell or a similar tool, and combines the results. It is always up to date and needs no extra storage, but it is only as fast as the slowest server, fails for servers that are unreachable and puts load on every server each time.

**A central index** copies object metadata, such as names, columns, definitions and dependencies, from every server into one place on a schedule, and searches that. It is fast, works when servers are offline and keeps history, but it is only as current as its last refresh, and it needs building and securing. Our guide to [SQL object indexing](/guides/sql-object-indexing) covers how.

For occasional searches across a few dozen servers, live search is often enough. For frequent searches, or hundreds of servers, a central index is usually the better choice.

Scope the search first

On a large estate, an unscoped search is rarely what you want. Narrow it before running it:

  • **Environment.** Production only, or all environments? Searching test and development too may triple the results with near-identical copies.
  • **Application or area.** If you know the system involved, search only its servers and databases.
  • **Object type.** Tables only, or code too?
  • **Time.** For change investigations, objects modified in a particular window.

Server groups in a Central Management Server, organised by environment and application, make this scoping easy.

Sort out permissions

Searching metadata needs the right access on every server. To see object names and columns, a login needs access to each database. To read code definitions, it needs `VIEW DEFINITION`, which can be granted at database or server level. Rather than running estate-wide searches with a highly privileged account, create a dedicated login with only the read permissions needed for searching, and use it for both live searches and index collection. Databases where it has no access will simply be missing from results, so keep a list of exclusions so you know what wasn't searched.

Handle servers you can't reach

In a large estate, some servers will always be offline, slow or blocked by a firewall at the moment you search. A good search process:

  • Sets a sensible connection and query timeout, so one slow server doesn't hold up everything.
  • Records which servers failed, so the results say "searched 412 of 420 servers" rather than silently missing some.
  • Lets you retry just the failed servers.

With a central index, the problem mostly disappears, since you search the last known copy. But record when each server was last indexed, so stale data is visible.

Make large results usable

A broad search across an estate can return thousands of matches. A few techniques make them manageable:

  • **Group identical objects.** The same procedure in the same database on five environments is one object, not five. Group by database and object name, and show where it exists.
  • **Highlight differences.** If copies of the same object have different definitions, which you can check by comparing hashes, that is often the most interesting result.
  • **Rank by relevance.** Exact name matches first, then partial name matches, then matches inside code.
  • **Show context.** For code matches, a snippet around the match saves opening each object.

Protect sensitive code

Collecting or searching code across an estate concentrates a lot of sensitive information in one place: business logic, server names and occasionally, unfortunately, credentials embedded in code. Treat search results and any central index as sensitive data. Limit who can run estate-wide code searches, log who searches for what, and consider excluding databases whose code shouldn't be widely visible. Finding hard-coded passwords is a good reason to search, but the results need careful handling.

Troubleshooting Scenarios

An estate-wide search takes too long

Some servers are slow or unreachable. Set timeouts, scope the search to the servers you need, or move to a central index.

Results are full of duplicates

The same objects exist in several environments. Group results by database and object name, and show the environments each exists in.

Some databases never appear in results

The search login has no access to them, or lacks VIEW DEFINITION. Check its permissions and keep a list of excluded databases.

Nobody knows how current the index is

Record when each server was last indexed and show it alongside results.

FAQ

How do I search for objects across hundreds of SQL Servers?

Either run a federated search through a Central Management Server or PowerShell, or search a central index of object metadata collected from every server.

Should I search live or use an index?

Live search suits occasional searches on smaller estates. A central index suits frequent searches and large estates.

What permissions does object search need?

Access to each database, plus VIEW DEFINITION to read code. A dedicated read-only search login is best.

How do I deal with duplicate results across environments?

Group results by database and object name, and show which environments each object exists in.

Is it safe to index SQL code centrally?

It can be, if the index is treated as sensitive, access is limited and logged, and sensitive databases are excluded where needed.

Suggested Related Articles