Create nonclustered index on the column cn_placed
And the below syntax will perform much metter.
Main Topics
Browse All TopicsWhy 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.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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.
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.
Business Accounts
Answer for Membership
by: pratima_mcsPosted on 2009-02-12 at 01:42:47ID: 23620459
remove 0=0
_edu_txt,c n_present_ position,
SELECT DISTINCT TOP 100 cn_id, cn_fname, cn_lname, cn_age,
cn_years_experience,cn_max
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