Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shinilkp
Starting Member
2 Posts |
Posted - 2008-12-31 : 10:02:20
|
| This is my query which is creating problem for meSELECT top 5 m.messageid, m.parentidFROM [message] m WHERE parentid in ( SELECT m.messageid FROM [message] m join [groupmessage] gm on m.messageid = gm.messageid join [group] g on gm.groupid = g.groupid and gm.reftypeid = 'C' and m.inactive = 0 and g.portalid=20 and g.grouptypeid=0 and g.groupcategoryid in (22)) OR m.messageid in ( SELECT m.messageid FROM [message] m join [groupmessage] gm on m.messageid = gm.messageid join [group] g on gm.groupid = g.groupid and gm.reftypeid = 'C' and m.inactive = 0 and g.portalid=20 and g.grouptypeid=0 and g.groupcategoryid in (22)) ORDER BY m.updatedt descThe problem here is If the m.messageid in the last select is less than 5 (i am taking top 5), its taking a long time. But if I remove Top 5 or if there are more than 5 messageid for groupcategoryid = 22, its like less than a sec which is expected. What am I missing here?I even thought of seperating the two queries and union all it. But its little slower when the data is large. Any help is appreciated |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-12-31 : 10:35:18
|
When setting TOP 5, the query optimizer needs to deal with ALL records in order to sort them.Unless you have an INDEX over updateDT column. which makes the TOP 5 comparison much faster. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
shinilkp
Starting Member
2 Posts |
Posted - 2008-12-31 : 11:21:23
|
| Thanks for the reply. yes the updateDT is indexed |
 |
|
|
Thiyagu_04
Starting Member
37 Posts |
Posted - 2009-01-01 : 07:35:09
|
| Try to replace with In with Inner join and check the query plan and execution time. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-01 : 13:19:35
|
quote: Originally posted by Thiyagu_04 Try to replace with In with Inner join and check the query plan and execution time.
or use where exists |
 |
|
|
|
|
|