Ad hoc queries sql что это
Перейти к содержимому

Ad hoc queries sql что это

  • автор:

Ad Hoc Query

In SQL, an ad hoc query is a loosely typed command/query whose value depends upon some variable. Each time the command is executed, the result is different, depending on the value of the variable. It cannot be predetermined and usually comes under dynamic programming SQL query. An ad hoc query is short lived and is created at runtime.

Techopedia Explains Ad Hoc Query

As the word “ad hoc” suggests, this type of query is designed for a “particular purpose,”which is in contrast to a predefined query, which has the same output value on every execution. An ad hoc query does not reside in the system for a long time and is created dynamically on demand by the user. It is more efficient to use an ad hoc query in programming as it saves system resources, but, at the same time complex, ad hoc queries (have multiple variables) also challenge the processing speed and runtime memory of the system.

What is an ad hoc query?

Someone recently asked me which queries are ad hoc in SQL Server. An ad hoc query is a single query not included in a stored procedure and not parameterized or prepared. Depending on the server settings, SQL Server can parameterize some statements initially written as ad hoc queries. Ad hoc doesn’t mean dynamic.

Here’s a simple ad hoc query example in SQL Server:

SQL Server will parameterize this simple query:

One answer I found when searching the question “what is an ad hoc query” said that ad hoc queries are built by combining answers from a web form. That’s actually one way to create a dynamic query, which may or may not be ad hoc. If a dynamic query is parameterized, it’s not an ad hoc query.

Here’s an example of a dynamic query that is parameterized (prepared), so it’s not ad hoc:

However, if there are no parameters, the query will remain ad hoc. Here is an example of an ad hoc query that also happens to be dynamic:

Why are ad hoc queries useful?

In many cases, a developer or DBA may run an ad hoc query once and then never run it again. On the other hand, the same query can run thousands of times a day from an application, yet it’s still may be an ad hoc query. Depending on the query, it may not make sense to include it in a stored procedure or parameterize it.

Ad hoc queries are neither bad nor good; like anything else, it all depends on how they are used. Here’s an interesting article from Phil Factor about troubleshooting some poorly performing ad hoc statements.

What is an ad hoc query in a database?

To find out if SQL Server treats the query as ad hoc, you can examine the object type in the plan cache. This query is from “Microsoft SQL Server 2012 Internals” by Kalen Delaney et al. Note that you may need to add more filters on [text] if it returns so many rows that you can’t find your query.

You’ll see the object type Adhoc for an ad hoc query. For parameterized queries, you’ll also see a row with the object type of Prepared. Stored procedure calls will return Proc, and there are a few others.

What is the Optimize for Ad Hoc Workload setting?

As you may imagine, a large number of queries could each run only once on a given system. To avoid having these take up space in the plan cache, enable the Optimize for Ad Hoc Workload setting. Then, the first time a query runs, only a stub of the plan is stored in the cache. If it runs again, then SQL Server will store the entire plan.

Conclusion

It’s easier to say what an ad hoc query isn’t than to say what it is. Ad hoc queries are not necessarily bad things, just part of a typical workload for SQL Server. If you suspect that some ad hoc queries are causing problems, you can begin investigating by using Kalen’s query. A monitoring tool can also help you identify poorly performing queries that need tuning.

Commentary Competition

Enjoyed the topic? Have a relevant anecdote? Disagree with the author? Leave your two cents on this post in the comments below, and our favourite response will win a $50 Amazon gift card. The competition closes two weeks from the date of publication, and the winner will be announced in the next Simple Talk newsletter.

Redgate SQL Monitor

SQL Monitor helps you manage your entire SQL Server estate from a single pane of glass. Proactively mitigate potential risks with instant problem diagnosis and customizable alerting – wherever your databases are hosted. No downtime, customer complaints, or wake-up calls at 3am.

Subscribe for more articles

Fortnightly newsletters help sharpen your skills and keep you ahead, with articles, ebooks and opinion to keep you informed.

What is Ad Hoc Query?

I’m reading a book about SQL. In that book there’s the term Ad Hoc Query, which I don’t understand.

What exactly is an ad hoc query?

Ben's user avatar

11 Answers 11

Ad hoc is latin for «for this purpose». You might call it an «on the fly» query, or a «just so» query. It’s the kind of SQL query you just loosely type out where you need it

Ad-hoc Query

  • this type of query is designed for a "particular purpose,“ which is in contrast to a predefined query, which has the same output value on every execution.
  • An ad hoc query command executed in each time, but the result is different, depending on the value of the variable.
  • It cannot be predetermined and usually comes under dynamic programming SQL query.
  • An ad hoc query is short lived and is created at runtime.

Also want to add that ad hoc query is vulnerable to SQL injection attacks. We should try to avoid using it and use parameterized SQLs instead (like PreparedStatement in Java).

An Ad-Hoc query is:

  1. Pre-planned question.
  2. Pre-scheduled question.
  3. spur of the moment question.
  4. Question that will not return any results.

Paresh Mayani's user avatar

Ad hoc query is type of computer definition. Which means this query is specially design to obtain any information when it is only needed. Predefined. refer this https://www.youtube.com/watch?v=0c8JEKmVXhU

Sanyog Tiwari's user avatar

In Sql Server «Ad Hoc Query» is also used for Ad Hoc Distributed Queries. These are ad hoc queries on an other server via OpenRowset or OpenDatasource. Ad Hoc Distributed Queries are only allowed after configuring so. This is a Server configuration option.

SQL Server Ad Hoc Queries Performance

In today’s article, we will learn how to improve SQL Server Ad Hoc Queries Performance and what Ad Hoc Query means.

Ad Hoc Query

The word ad hoc comes from Latin and means “for purpose”.

In other words, it refers to a query written in case the written query is written for a purpose and has no continuation or rarely.

If I need to explain with an example, imagine that you have a table with user information and let’s assume that a dynamic query is made over this table over a different user number.

In this case, the written query is actually an Ad Hoc query.

When you run a query in SQL Server, the execution plan is created for the query run by the SQL Server Query Optimizer and the query runs through this plan.

The execution of the said execution plan can be completed in a few milliseconds for a simple query,

while it may take longer for complex queries.

For this reason, SQL Server keeps the execution plan information it creates for each running query in its cache in case the same query is run again.

Think about it, even if you run it only once, SQL Server creates an execution plan for your query to run and keeps it in its cache so that you can run it again in the future.

Of course, if there is no suitable execution plan for your query to run,

a new query plan is created by SQL Server Query Optimizer.

In order to increase the cache efficiency of the execution plans of the infrequently used queries that entered our lives with SQL Server 2008,

it introduced “Optimize for Ad hoc Workloads”, a feature at the presentation level.

When the query is compiled for the first time, instead of keeping the execution plan,

the Compiler Plan stores the Stub value. If the query is run again, it converts the Stub value to the execution plan.

There are two methods you can use to determine whether this feature we have described is on or off.

The first one is SQL Server Management Studio (SSMS) Right click on the server in Object Explorer and then click Properties,

then in the advanced menu the “Optimize for ad hoc workloads” section defaults to False,

which means that when a query is compiled and executed, your entire plan will be placed in cache .

You can use TSQL to learn whether this feature is active or passive for GUI -On screen- learning.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *