SQl Query takes too long

Posted on 2009-02-12
Last Modified: 2012-05-06
Why does this query take forever. And it never even results in anything. Ive waited 5 mins finally ending up cancelling it.

Is it something to do with indexes or corrupt logs.

SELECT DISTINCT TOP 100 cn_id, cn_fname, cn_lname, cn_age, cn_years_experience,cn_max_edu_txt,cn_present_position, cn_present_salary, cn_salary_sought, cn_incoming, cn_cart, cn_placed, cn_english_ability FROM v_get_candidates_jobs WHERE 0=0   AND cn_placed=0  ORDER BY cn_id  DESC

Open in new window

Question by:isaackhazi
    LVL 39

    Accepted Solution

    remove 0=0

    SELECT DISTINCT TOP 100 cn_id, cn_fname, cn_lname, cn_age,
    cn_present_salary, cn_salary_sought, cn_incoming, cn_cart,
    cn_placed, cn_english_ability FROM v_get_candidates_jobs
    WHERE  cn_placed=0  ORDER BY cn_id  DESC
    LVL 57

    Assisted Solution

    by:Raja Jegan R
    Create nonclustered index on the column cn_placed
    And the below syntax will perform much metter.
    SELECT cn_id,
           row_number() OVER ( partition BY cn_id ORDER BY cn_id DESC) rnum
    FROM   v_get_candidates_jobs
    WHERE  cn_placed = 0) temp
    WHERE rnum <= 100

    Open in new window

    LVL 8

    Author Comment

    removed it.

    Still takes forever.

    The problem is that this query is being executed by an ASP app. So I keep getting the TIMEOUT message
    LVL 23

    Assisted Solution

    What indexes do you have on the table? Presumably, cn_id is your primary key and so that will have an index on? Does cn_placed have an index on?

    The other thing to consider, is there a long running transaction on the table? If there is, it could be blocking the query returning the results (a quick test would be to temporarily add "WITH (NOLOCK)" after the table name in the query (FROM v_get_candidates_jobs WITH (NOLOCK)...) and try the query again. If that comes back with results when the original query doesn't, then it's because there is a long running transaction/lock being held on the data. Note, don't keep WITH (NOLOCK) in there permanently unless it is what you actually want as it means you could read data that hasn't actually been committed yet.
    LVL 39

    Assisted Solution

    by:Pratima Pharande
    what is this v_get_candidates_jobs  ?
    Is it table ?  or View ?

    LVL 57

    Expert Comment

    by:Raja Jegan R
    Have you tried my approach and query
    LVL 8

    Author Comment

    Yes i tried all the above queries but they all take for ever.

    Is this because the database was copied from an online server and then attached to the local server

    Do you think any files are corrupt or something like that. may be log file...????
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    can you remove the DISTINCT?
    can you post the explain plan?
    can you run the UPDATE STATISTICS on the table?
    LVL 8

    Assisted Solution

    If there are so many rows in that table that using DISTINCT is degrading performance that dramatically, I'd recommend periodically generating a "cube" of sorts.  It'd be really easy and wouldn't be that cube-like, but it could speed things up if you inserted distinct rows into a staging table and you could select top 100 from that.  That'll work only if you don't really need live data and can deal with a slight delay.

    The other alternative may be to create a composite index over all of the columns you're returning.  This'll dramatically expedite the DISTINCT operation.  I ran a test against a test table with about 4.2 million rows (3 int columns randomly assigned values between 0 and 99).  I ran 5 trials per condition and excluded 2 outliers.  Then, I averaged the remaining 3 results.  Here are the results of my tests:

    non-indexed: 1856 ms
    nonclustered-index: 1016 ms
    non-indexed: 1795 ms
    clustered-index: 1063 ms
    non-indexed: 1911 ms
    nonclustered, non-composite: 1771 ms

    The composite index won't help with your where clause so you'll still need an index on each column you'll be searching within.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
    If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 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

    12 Experts available now in Live!

    Get 1:1 Help Now