SQL Query cost calculation before firing the query

Posted on 2011-10-24
Last Modified: 2012-05-12
I am working on a Windows Forms based .Net application that uses SQL 2008 database as back-end. We have a few search screens that allow the user to enter search criteria. Some queries take a lot of time to execute while most of the queries take only a few seconds. Is there a way to prevent a query from being fired if the cost of the query is expected or calculated to be very high?I'd like to display a warning message to the user that the query would potentially run for a while. The user should given the choice of refining the query or agree to wait several seconds.

We are using select top 100 is the queries but is there a different solution that would warn the user before firing the query?

Question by:shekhar_shashi
    LVL 51

    Accepted Solution

    maybe this query will help

    select b.text, *
    from sys.dm_exec_query_stats as a
    cross apply sys.dm_exec_sql_text(sql_handle) as b

    check your query above and try to guess time (use total_ellapsed_time & text to find a similar query)

    but there is no guarantee to find the actual time...
    LVL 75

    Assisted Solution

    by:Anthony Perkins
    Very good question and unfortunately no good answer, at least that I know of.  Yes, you can use TOP and that will reduce network traffic, but it may not diminish the actual execution of the query.

    One approach you could take is set a very short timeout (as in CommandTimeout = 10).  That means that any query that takes longer than 10 seconds would fail and you could trap the error.  You can then display an appropriate message, so that the user can refine their query.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
    In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now