开发者

TSQL query for finding rows that match a set of properties

I have got the following simplified schema:

CREATE TABLE [file]
( 
    id UNIQUEIDENTIFIER NOT 开发者_如何转开发NULL, 
    uri NVARCHAR(MAX) NOT NULL, 
    CONSTRAINT PK_file PRIMARY KEY (id ASC) ON [PRIMARY] 
)

CREATE TABLE [property]
( 
    id UNIQUEIDENTIFIER NOT NULL, 
    name NVARCHAR(MAX) NOT NULL, 
    CONSTRAINT PK_property PRIMARY KEY (id ASC) ON [PRIMARY] 
)

CREATE TABLE [metadata]
( 
    fileid UNIQUEIDENTIFIER NOT NULL,
    propertyid UNIQUEIDENTIFIER NOT NULL,
    value NVARCHAR(MAX) NOT NULL, 
    CONSTRAINT PK_metadata PRIMARY KEY (fileid, propertyid ASC) ON [PRIMARY] 
)

Where [fileid] FKs to [file].[id] and [propertyid] FKs to [property].[id]. Assume that [properyid] is CLUSTERED and [value] is a NON-CLUSTERED.

I want to select all of the files that match a certain set of metadata; for example every file that has a property value pair of size = 1 Kb and extension = 'txt'.

The query that I've come up with, for example with three properties specified, is this:

SELECT [uri] FROM [file] WHERE [id] IN (
    SELECT a.[fileid] FROM (
        SELECT COUNT(*) [count], [fileid] FROM [metadata]
        WHERE ([propertyid] = '597ddddf-afd2-414f-9774-36f067038064' AND 
                [value] = N'moo') OR
              ([propertyid] = 'd83d12de-e4bc-4d18-be12-743504df3318' AND 
                [value] = N'foo') OR
              ([propertyid] = 'c00c3966-5034-4818-8567-abd660f37f15' AND 
                [value] = N'boo')
        GROUP BY [fileid]
    ) a
    WHERE a.[count] = 3
)

Can I do any better?


;WITH propertylist AS (
  SELECT propertyid = '597ddddf-afd2-414f-9774-36f067038064', value = N'moo' UNION ALL
  SELECT propertyid = 'd83d12de-e4bc-4d18-be12-743504df3318', value = N'foo' UNION ALL
  SELECT propertyid = 'c00c3966-5034-4818-8567-abd660f37f15', value = N'boo'
)
SELECT uri
FROM file
WHERE id IN (
  SELECT m.fileid
  FROM metadata m
    INNER JOIN propertylist p ON m.propertyid = p.propertyid AND m.value = p.value
  GROUP BY m.fileid
  HAVING COUNT(*) = (SELECT COUNT(*) FROM propertylist)
)


Maybe something like this?

SELECT 
   [uri] 
FROM 
   [file] 
WHERE 
   EXISTS(
        SELECT 
           NULL
        FROM 
           [metadata]
        WHERE 
              ([propertyid] = '597ddddf-afd2-414f-9774-36f067038064' AND 
                [value] = N'moo') OR
              ([propertyid] = 'd83d12de-e4bc-4d18-be12-743504df3318' AND 
                [value] = N'foo') OR
              ([propertyid] = 'c00c3966-5034-4818-8567-abd660f37f15' AND 
                [value] = N'boo') AND 
              [File].[id] = [metadata].[fileid]
        GROUP BY 
           [fileid]
        HAVING 
           COUNT(*) = 3
       )


I'm maybe missing something, but if you want that all 3 conditions are satisfied, why don't just use "AND" each time instead of counting the number of properties set? You will avoid a GROUPING that isn't necessary.

I'll write :

    SELECT [uri] FROM [file] 
    WHERE EXISTS ( SELECT [fileid] FROM [metadata]
           WHERE [propertyid] = '597ddddf-afd2-414f-9774-36f067038064'
             AND [value] = N'moo'
             AND [propertyid] = 'd83d12de-e4bc-4d18-be12-743504df3318' 
             AND [value] = N'foo'
             AND [propertyid] = 'c00c3966-5034-4818-8567-abd660f37f15'
             AND [value] = N'boo'
             AND [file].[id] = [metadata].[fileid])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜