Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Triangle join on same table Expand / Collapse
Author
Message
Posted Saturday, December 22, 2012 3:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, December 28, 2012 2:17 AM
Points: 1, Visits: 4
Hello,

I have a problem that I need to have a solution for. The problem today is performance.

I have a row with approx 3 million rows looking like this

Id1, Id2
1, 3
1, 2
1, 4
1, 7
2, 5
2, 9
3, 1
3, 2
5, 1
9, 1
etc.

Its basically a list of apartments matching eachother for swapping. So apartment 1 want to swap with apartment 2, 3, 4, 7.. Apartment 2 want to swap with 5, 9 etc..

What I want to achieve here is to build triangle or rectangles for swap.

Meaning, I want to see how many swap alternatives Id(1) has building a triangle or rectangle chain something like this:

Triangle:
Id1 (my id), Id2, Id3

1, 2, 5
1, 2, 9

5 and 9 must have a match for 1 (5, 1 & 9, 1).

I hope you understand. I have done this today using 2 joins, but its slow as hell on large amount of datas.

I've read something about CTE but have no clue on how it works yet.

Thanks in advance.
Post #1399667
Posted Tuesday, December 25, 2012 7:05 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 7:58 PM
Points: 3,422, Visits: 5,366
As a first time poster, you should learn to follow forum etiquette and read up on posting questions with DDL and consumable sample data. There are many links you can find this information on. As it is Christmas and I'm feeling charitable, I'll help you out this time.

I'm not going to promise that the following will be any faster than your JOIN, but it might be, or at the very least is another alternative to try.

-- DDL
CREATE TABLE #ApartmentSwaps (Id1 INT, Id2 INT
,PRIMARY KEY (id1, id2))

-- Consumable sample data
INSERT INTO #ApartmentSwaps
SELECT 1, 3 UNION ALL SELECT 1, 2 UNION ALL SELECT 1, 4
UNION ALL SELECT 1, 7 UNION ALL SELECT 2, 5 UNION ALL SELECT 2, 9
UNION ALL SELECT 3, 1 UNION ALL SELECT 3, 2 UNION ALL SELECT 5, 1
UNION ALL SELECT 9, 1

DECLARE @MyID INT = 1

SELECT id1, id2, id3
FROM #ApartmentSwaps
CROSS APPLY (SELECT id3=id1 FROM #ApartmentSwaps WHERE id2 = @MyID) a
WHERE id1 = @MyID AND id1 <> id2 AND id2 <> id3 AND id1 <> id3

-- Always use temp tables in your questions and remember to drop them
DROP TABLE #ApartmentSwaps


Note how I specified a PRIMARY KEY on the table, which creates a CLUSTERED INDEX. This should help the query perform better.

You could also do this with a set-based WHILE loop, and I've seen cases where that beats out the JOIN and CROSS APPLY attacks to this kind of a problem.

Also, beware of parameter-sniffing the way I've written the query (using @MyID). You may also want to try the same query with OPTION (RECOMPILE) to see if it runs any faster.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1400117
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse