Solved

SQL Superset subset

Posted on 2011-09-20
12
787 Views
Last Modified: 2012-05-12
I have following table table with 3 millions records where I need help to find Superset Subset relationship.

SetPackage
--------------
SetID int
PackageName varchar(35)

Say I have inserted few sample rows like following:

INSERT into SetPackage  VALUES
(1,'A')
INSERT into SetPackage VALUES
(2,'A')
INSERT into SetPackage VALUES
(2,'B')
INSERT into SetPackage VALUES
(3,'A')
INSERT into SetPackage VALUES
(3,'B')
INSERT into SetPackage VALUES
(3,'C')
INSERT into SetPackage VALUES
(3,'E')
INSERT into SetPackage VALUES
(4,'B')
INSERT into SetPackage VALUES
(4,'C')

I want to know efficient way to know SuperSetID,SubSetID combination from this large amount of data.

I wrote following query but it takes long time and fills tempdb space:

SELECT A.SetID SupersetID, B.SetID SubsetID
FROM SetPackage A, SetPackage B
WHERE A.SetID <> B.SetID
  AND A.Package = B.Package
GROUP BY A.SetID, B.SetID
HAVING NOT EXISTS
(
    SELECT * FROM SetPackage C
    WHERE C.SetID = B.SetID
       AND NOT EXISTS (
        SELECT * FROM SetPackage D
        WHERE D.SetID = A.SetID
          AND D.Package = C.Package
        )
)
0
Comment
Question by:mayankvindhya
  • 3
  • 2
  • 2
  • +2
12 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 36569582
The filtering part in the HAVING could be in the WHERE. Why because they will be earlier filtered out so they won't be grouped anymore.  
Wonder also if its an explicit choise not to join C and D but writing them in a separate not exists.


0
 

Author Comment

by:mayankvindhya
ID: 36569616
Do you have better query?Please share.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36569733
>>Do you have better query?<<
Have you checked the suggestion made?  Does it return the correct results?  Does it perform adequately?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36569749
what would you want returned if the following rows are added?


INSERT INTO setpackage
VALUES (5, 'D');

INSERT INTO setpackage
VALUES (6, 'D');


each setid could be the superset of the other
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:mayankvindhya
ID: 36570383
I don't want set id 5,6 to be superset or subset as the values are same
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 36570454
ok,  your query above returns both, that's why I sought confirmation thanks
0
 
LVL 25

Accepted Solution

by:
jogos earned 500 total points
ID: 36571733
Seemed to me my sugestions did meet your sql scils to type out.

In example I did implemented my 2 sugestions at same time, but the join C/D could also be implemented in the HAVING-sollution
But in performance tuning it's always beter to measure (profiler) the situation before changement, make a first change and compare new measurement and result, make next change ...
So you won't end up making 10 changes don't know where your gain was or which of the 10 changes was the cause of an altered result.
SELECT A.SetID SupersetID, B.SetID SubsetID
FROM SetPackage A, SetPackage B
WHERE A.SetID <> B.SetID
  AND A.Package = B.Package
AND NOT EXISTS 
(
    SELECT * FROM SetPackage C
         inner join SetPackage D on  AND D.Package = C.Package
    WHERE C.SetID = B.SetID
       AND D.SetID = A.SetID
) 
GROUP BY A.SetID, B.SetID

Open in new window

0
 
LVL 25

Expert Comment

by:jogos
ID: 37196812
No feedback on suggestions from author on both theorecical as copy/past sollutions that can make a difference in time and tempdb-usage.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 37242424
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now