Solved

SQL Superset subset

Posted on 2011-09-20
12
802 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

25 Experts available now in Live!

Get 1:1 Help Now