Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3423512
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:21:50+00:00 2026-05-18T06:21:50+00:00

i have got lot of queries which are written like: select thread_id as topic_id,title

  • 0

i have got lot of queries which are written like:

     select  thread_id as topic_id,title as topic            
  ,            
    isnull((select count(*) from tblmessages b where thread_id=a.thread_id and is_approved='Y' and sort_level>1            
     group by b.thread_id            
     ),0) as replies,            
    isnull((select count(*) from tblmessages b where thread_id=a.thread_id and isnull(new_post,'Y')='Y' and sort_level>1            
     group by b.thread_id            
     ),0) as NewPost,            
    isnull((select count(*) from tblmessages b where thread_id=a.thread_id and isnull(is_approved,'N')='N' and sort_level>1            
     group by b.thread_id            
     ),0) as NotClear,            

    sort_level,sort_index,  from tblMessages a            
    where   sort_level=1 and category=@category 
    order by topic_id desc

Please tell me how to optimize and better way to write such queries. because i have got tables with records 5,00,000. so it takes lots of time and some times gets time out.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T06:21:51+00:00Added an answer on May 18, 2026 at 6:21 am

    You should group the variuos subquery in a single one with different count and use a join to put the data together

    the subquery should be:

    select thread_id
         count(when isnull(is_approved,'N')='N' then 1 end) as replies,
         count(when isnull(new_post,'Y')='Y' then 1 end) as NewPost,
         count(when isnull(is_approved,'N')='N' then 1 end) as NotClear
         from tblmessages 
         where sort_level>1  
         group by thread_id
    

    While the finalquery is the following

    select  thread_id as topic_id,title as topic,
       sort_level,sort_index , B.replies, B.NewPost, B.NotClear
       from tblMessages a            
       join
         (select thread_id
         count(when isnull(is_approved,'N')='N' then 1 end) as replies,
         count(when isnull(new_post,'Y')='Y' then 1 end) as NewPost,
         count(when isnull(is_approved,'N')='N' then 1 end) as NotClear
         from tblmessages 
         where sort_level>1  
         group by thread_id) as B
         on a.thread_id = B.thread_id
        where   sort_level=1 and category=@category 
        order by topic_id desc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got map a lot of ranges to a value like 0-300 =
I've got two tables: Topics -name and Queries -topic_id A query can have a
I've got a program where a lot of classes have really complicated configuration requirements.
We've got into a very tricky scenario in a project. We have used lot
I have got a form into which information is entered, and a drop down
I have got a table [newsletter] in the db, which saves the email address,
I've got three tables which share a lot of columns, so I thought to
I have got a file with a lot of two pairs of string, namely
I have a lot of strings like this to find and replace in visual
I have got a requirement from a client. They need three blogging web application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.