Snitz Forums 2000
Snitz Forums 2000
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Community Forums
 Code Support: ASP (Non-Forum Related)
 SQL: Getting Forum Topics 1 per User?
 New Topic  Reply to Topic
 Printer Friendly
Previous Page
Author Previous Topic Topic Next Topic
Page: of 2

HuwR
Forum Admin

United Kingdom
20593 Posts

Posted - 03 August 2011 :  11:09:20  Show Profile  Visit HuwR's Homepage  Reply with Quote
you could rewrite my query as follows to use an in emory table

SELECT DISTINCT TOP (20) T_AUTHOR, MAX(T_LAST_POST) OVER(PARTITION BY T_AUTHOR) AS LastPost
INTO @TempTopics
FROM FORUM_TOPICS
ORDER BY LastPost DESC;

SELECT * FROM FORUM_TOPICS Tp INNER JOIN @TempTopics ON
Tp.T_AUTHOR = @TempTopics.T_AUTHOR AND Tp.T_LAST_POST = @TempTopics.LastPost


actually to use an in memory table i is a bit more long winded.

DECLARE @TempTopics TABLE (
T_AUTHOR INT,
LastPost VARCHAR(14) );

INSERT @TempTopics(T_AUTHOR,LASTPOST)
SELECT DISTINCT TOP (20) T_AUTHOR, MAX(T_LAST_POST) OVER(PARTITION BY T_AUTHOR) AS LastPost
FROM FORUM_TOPICS
ORDER BY LastPost DESC;

SELECT * FROM FORUM_TOPICS Tp INNER JOIN @TempTopics Tt ON
Tp.T_AUTHOR = Tt.T_AUTHOR AND Tp.T_LAST_POST = Tt.LastPost

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 03 August 2011 :  11:11:00  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
quote:
Originally posted by HuwR

well, you are infact creating a temp table that is what @topics is


Yeah, just in memory...


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20593 Posts

Posted - 03 August 2011 :  11:17:14  Show Profile  Visit HuwR's Homepage  Reply with Quote
code posted above to do the same with just an inline query, no functions needed

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 03 August 2011 :  11:21:16  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
quote:
Originally posted by HuwR

code posted above to do the same with just an inline query, no functions needed


Go away, mine is much neater .


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 03 August 2011 :  11:25:52  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
quote:
Originally posted by HuwR

code posted above to do the same with just an inline query, no functions needed


Oh yeah?

Look, no temp tables:


SELECT T.* FROM FORUM_TOPICS T INNER JOIN 
 (SELECT T_AUTHOR, (SELECT TOPIC_ID FROM FORUM_TOPICS T2 WHERE T2.T_AUTHOR=T1.T_AUTHOR AND T2.T_LAST_POST=T1.LASTPOST) As T_ID
  FROM (
	SELECT TOP (10) T_AUTHOR, MAX(T_LAST_POST) AS LastPost
	FROM FORUM_TOPICS
	GROUP BY T_AUTHOR
	ORDER BY LastPost DESC
        )T1 
 ) As MyT ON MyT.T_ID=T.TOPIC_ID
 ORDER BY T_LAST_POST DESC


This one also runs on MySQL, just by replacing TOP by LIMIT (and placing limit at the end of the subquery).


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 03 August 2011 :  11:34:58  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
This was fun, now to get back to work . Of course, I am hoping no one noticed that I contradicted myself and ended up solving it all with a very, very, simple () SQL statement.


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20593 Posts

Posted - 03 August 2011 :  11:50:51  Show Profile  Visit HuwR's Homepage  Reply with Quote
well, to be honest four nested selects isn't exactly simple

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 03 August 2011 :  11:55:52  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
quote:
Originally posted by HuwR

well, to be honest four nested selects isn't exactly simple


It's not simple ?!!
I promise I wrote them one at a time .


Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20593 Posts

Posted - 03 August 2011 :  11:59:45  Show Profile  Visit HuwR's Homepage  Reply with Quote
I still think my first attempt was the simplest I just should have dropped the #TempTopics table after doing the select

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page

ruirib
Snitz Forums Admin

Portugal
26364 Posts

Posted - 03 August 2011 :  12:26:56  Show Profile  Send ruirib a Yahoo! Message  Reply with Quote
From an execution point of view, I tested both ways with a database from a live Snitz forum and your option had a higher subtree cost (0,8 vs. 3.17) and a higher number of scans and logical reads (rather big difference here). Probably a disadvantage on a high traffic forum.

I had performed a similar comparison before, with Snitz too (query performance has always been of interest to me) and using derived tables wasn't actually faster than using temporary tables, probably because the number of records involved was much higher. I thought it was interesting to compare it in this situation.

This is fun, but I have already spent 2 or 3 hours with this. I better go do something more boring .



Snitz 3.4 Readme | Like the support? Support Snitz too
Go to Top of Page

HuwR
Forum Admin

United Kingdom
20593 Posts

Posted - 03 August 2011 :  13:10:22  Show Profile  Visit HuwR's Homepage  Reply with Quote
yes, I ought to finish what I was doing beore getting side tracked.

MVC .net dev/test site | MVC .net running on Raspberry Pi
Go to Top of Page
Page: of 2 Previous Topic Topic Next Topic  
Previous Page
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
Snitz Forums 2000 © 2000-2021 Snitz™ Communications Go To Top Of Page
This page was generated in 0.07 seconds. Powered By: Snitz Forums 2000 Version 3.4.07