我有一個數據庫定義如下:SQL Server的查詢相交
create table Classes (
Id INT not null,
Text NVARCHAR(255) null,
primary key (Id)
)
create table Documents (
Id INT not null,
Title NVARCHAR(MAX) null,
Abstract NVARCHAR(MAX) null,
Year INT null,
primary key (Id)
)
create table Documents_Tokens (
DocumentFk INT not null,
TokenFk INT not null
)
create table Documents_Classes (
DocumentFk INT not null,
ClassFk INT not null
)
create table Tokens (
Id INT not null,
Text NVARCHAR(255) null,
primary key (Id)
)
有A M:文件和類文件和令牌米之間的關係。
我想確定某些統計數據。一個屬性是A,它衡量類和代幣的共同出現。我目前確定這個統計是這樣的:
with combs as
(
select
a.Id as classid,
a.text as class,
b.Id as tokenid,
b.text as token
from dbo.Classes as a
cross join dbo.Tokens as b
)
,A as
(
select token, class, count(distinct DocumentFk) as A from
(
select
token,
class,
DocumentFk
from combs
inner join dbo.Documents_Classes on classid = ClassFk
group by token, DocumentFk, class
intersect
select
token,
class,
DocumentFk
from combs
inner join dbo.Documents_Tokens on tokenid = tokenFk
group by token, DocumentFk, class
) T group by token, class
)
...
不幸的是,這個查詢需要年齡(我已經運行查詢分析器後添加索引)。這是確定A的最有效方法嗎?如果沒有更好的方法?我也可以改變潛在的數據庫結構,以加快速度...
任何反饋將非常感激。
梳子從哪裏來? – RichardTheKiwi 2011-02-01 09:13:34