sql 查询多值属性
sql query for multi valued attributes
我有资源,每个资源都由一个 guid 表示,并且它们有属性名称-值对。我想查询
对于具有给定属性名称值对的资源。
所以,假设 table 看起来像:
GUID ATTR_SUBTYPE ATTR_VAL
63707829116544a38c5a508fcde031a4 location US
63707829116544a38c5a508fcde031a4 owner himanshu
44d5bf579d9f4b9a8c41429d08fc51de password welcome1
44d5bf579d9f4b9a8c41429d08fc51de host retailHost
c67d8f5d1a9b41428f029d55b79263e1 key random
c67d8f5d1a9b41428f029d55b79263e1 role admin
并且我想要位置为美国且所有者为 olaf 的所有资源。
一个可能的查询是:
select guid from table where attr_subtype = 'location' and attr_value = ‘US'
INTERSECT
select guid from table where attr_subtype = 'owner' and attr_value = ‘himanshu';
查询中可以有任意数量的属性名称值对,因此每对有一个额外的交集
在查询中。我想知道我们是否可以构建一个更好的查询,因为交集很昂贵。
将您的目标插入临时文件table,然后加入其中。
select t.guid
from table as t
join temp
on t.attr_subtype = temp.attr_subtype
and t.attr_value = temp.attr_value
一般来说,JOIN 比 INTERSECT 好。它提供了在几个完整 table 扫描完成之前获得第一条记录的机会。但是无论如何,你 select 一个慢数据结构,所以如果它变慢就不会很好了。
试试
select *
from
(select * from table where attr_subtype = 'location' and attr_value = 'US') t1
join
(select * from table where attr_subtype = 'owner' and attr_value = 'himanshu') t2
on (t1.guid = t2.guid)
...
假设每个 GUID 没有 重复 属性,则无需 JOIN
:
即可获得所需结果
SELECT "GUID" FROM T
WHERE ( "ATTR_SUBTYPE" = 'location' AND "ATTR_VAL" = 'US' )
OR ( "ATTR_SUBTYPE" = 'owner' AND "ATTR_VAL" = 'himanshu' )
GROUP BY "GUID"
HAVING COUNT(*) = 2 -- <-- keep only GUID have *both* attributes
我有资源,每个资源都由一个 guid 表示,并且它们有属性名称-值对。我想查询 对于具有给定属性名称值对的资源。
所以,假设 table 看起来像:
GUID ATTR_SUBTYPE ATTR_VAL
63707829116544a38c5a508fcde031a4 location US
63707829116544a38c5a508fcde031a4 owner himanshu
44d5bf579d9f4b9a8c41429d08fc51de password welcome1
44d5bf579d9f4b9a8c41429d08fc51de host retailHost
c67d8f5d1a9b41428f029d55b79263e1 key random
c67d8f5d1a9b41428f029d55b79263e1 role admin
并且我想要位置为美国且所有者为 olaf 的所有资源。
一个可能的查询是:
select guid from table where attr_subtype = 'location' and attr_value = ‘US'
INTERSECT
select guid from table where attr_subtype = 'owner' and attr_value = ‘himanshu';
查询中可以有任意数量的属性名称值对,因此每对有一个额外的交集 在查询中。我想知道我们是否可以构建一个更好的查询,因为交集很昂贵。
将您的目标插入临时文件table,然后加入其中。
select t.guid
from table as t
join temp
on t.attr_subtype = temp.attr_subtype
and t.attr_value = temp.attr_value
一般来说,JOIN 比 INTERSECT 好。它提供了在几个完整 table 扫描完成之前获得第一条记录的机会。但是无论如何,你 select 一个慢数据结构,所以如果它变慢就不会很好了。
试试
select *
from
(select * from table where attr_subtype = 'location' and attr_value = 'US') t1
join
(select * from table where attr_subtype = 'owner' and attr_value = 'himanshu') t2
on (t1.guid = t2.guid)
...
假设每个 GUID 没有 重复 属性,则无需 JOIN
:
SELECT "GUID" FROM T
WHERE ( "ATTR_SUBTYPE" = 'location' AND "ATTR_VAL" = 'US' )
OR ( "ATTR_SUBTYPE" = 'owner' AND "ATTR_VAL" = 'himanshu' )
GROUP BY "GUID"
HAVING COUNT(*) = 2 -- <-- keep only GUID have *both* attributes