【发布时间】:2010-11-25 11:33:29
【问题描述】:
我有很多这样的查询:
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
请告诉我如何优化和更好地编写此类查询。因为我有记录 5,00,000 的表。所以这需要很多时间,而且有时会超时。
谢谢
【问题讨论】:
-
那是 500 万条还是 5000 万条记录?
标签: sql sql-server-2005 query-optimization