Subject | Re: Questions about SELECT DISTINCT * |
---|---|
Author | robert_difalco |
Post date | 2006-03-06T16:16:22Z |
I think you are getting caught up in details that are not germane to
my question. So let me ask the question again with an example.
Say I have tree structure where leaves can be linked (i.e. present) in
more than one group at a time.
I have a distinct query like this:
SELECT DISTINCT G.oid, G.name, G.description, G.type [, ...]
FROM Groupable G
JOIN Link ON G.oid = Link.childId
JOIN Path ON Link.parentId = Path.descendantId
WHERE (G.type = LEAF AND Path.ancestorId = 12345)
To return all the Groupable leaves that descend from the group with
the unique identity of 12345.
The name and description fields are sizable VARCHAR fields and as a
result greately slow down the query because of the DISTINCT.
I can make the query faster like so:
SELECT G.oid, G.name, G.description, G.type [, ...]
FROM Groupable G
WHERE G.oid IN (
SELECT G.oid
FROM Groupable G
JOIN Link ON G.oid = Link.childId
JOIN Path ON Link.parentId = Path.descendantId
WHERE (G.type = LEAF AND Path.ancestorId = 12345))
I have found that most of the DISTINCT queries in my application are
only interested in being distinct on a SINGLE field (in this case G.oid).
Is there a better (more general) way to achieve this result than what
I have shown in the second version of the Tree query? Or am I already
taking the best approach?
R.
my question. So let me ask the question again with an example.
Say I have tree structure where leaves can be linked (i.e. present) in
more than one group at a time.
I have a distinct query like this:
SELECT DISTINCT G.oid, G.name, G.description, G.type [, ...]
FROM Groupable G
JOIN Link ON G.oid = Link.childId
JOIN Path ON Link.parentId = Path.descendantId
WHERE (G.type = LEAF AND Path.ancestorId = 12345)
To return all the Groupable leaves that descend from the group with
the unique identity of 12345.
The name and description fields are sizable VARCHAR fields and as a
result greately slow down the query because of the DISTINCT.
I can make the query faster like so:
SELECT G.oid, G.name, G.description, G.type [, ...]
FROM Groupable G
WHERE G.oid IN (
SELECT G.oid
FROM Groupable G
JOIN Link ON G.oid = Link.childId
JOIN Path ON Link.parentId = Path.descendantId
WHERE (G.type = LEAF AND Path.ancestorId = 12345))
I have found that most of the DISTINCT queries in my application are
only interested in being distinct on a SINGLE field (in this case G.oid).
Is there a better (more general) way to achieve this result than what
I have shown in the second version of the Tree query? Or am I already
taking the best approach?
R.