使用sql查询执行单模式投影

使用sql查询执行单模式投影,sql,social-networking,projection,bipartite,Sql,Social Networking,Projection,Bipartite,假设我们有一个表,其中包含: UserID, ProjectID 此表在网络分析中有另一种表示形式,称为 我们可以使用SQL查询进行高效的查询吗 单模式投影的示例: 假设该表为: UserId, ProjectID U1, P1 U2, P1 U3, P1 U4, P2 U5, P2 U1, P2 UserId的单模式投影为: U1,U2 U2,U3 U3,U1 U4,U5 U4,U1 U5,U1 P1,P2 类似地,projectd的单模式投影为: U1,U2 U2,U3 U3,U1

假设我们有一个表,其中包含:

UserID, ProjectID
此表在网络分析中有另一种表示形式,称为

我们可以使用SQL查询进行高效的查询吗

单模式投影的示例: 假设该表为:

UserId, ProjectID
U1, P1
U2, P1
U3, P1
U4, P2
U5, P2
U1, P2
UserId
的单模式投影为:

U1,U2
U2,U3
U3,U1
U4,U5
U4,U1
U5,U1
P1,P2
类似地,
projectd
的单模式投影为:

U1,U2
U2,U3
U3,U1
U4,U5
U4,U1
U5,U1
P1,P2

这在SQL中称为
join

select t1.UserId, t2.UserId
from t t1 join
     t t2
     on t1.ProjectId = t2.ProjectId;

注意:如果您有通过多个项目连接的对,并且您不希望重复,则使用
选择distinct

这在SQL中称为
连接

select t1.UserId, t2.UserId
from t t1 join
     t t2
     on t1.ProjectId = t2.ProjectId;

注意:如果您有通过多个项目连接的对,并且您不希望重复,则使用
选择distinct

使用以下命令使其运行更快。通过应用WITH(NOLOCK)语句,SQL不使用任何行级锁,响应速度更快

select t1.UserId, t2.UserId
from t t1 WITH (NOLOCK) join
     t t2 WITH (NOLOCK)
     on t1.ProjectId = t2.ProjectId;

感谢@Gordon Linoff的查询

使用以下命令使其运行更快。通过应用WITH(NOLOCK)语句,SQL不使用任何行级锁,响应速度更快

select t1.UserId, t2.UserId
from t t1 WITH (NOLOCK) join
     t t2 WITH (NOLOCK)
     on t1.ProjectId = t2.ProjectId;

感谢@Gordon Linoff的查询

我尝试了join选项,但对于大量记录,这需要很长时间。这是sql中最有效的方法吗?请用(NOLOCK)检查我的答案statement@M.M . . . 您希望确保该表在
ProjectId
(更好的是
(ProjectId,UserId)
)上有一个索引。我尝试了连接选项,但对于大量记录,这需要很长时间。这是sql中最有效的方法吗?请用(NOLOCK)检查我的答案statement@M.M . . . 您希望确保该表在
ProjectId
(最好是
(ProjectId,UserId)
)上有一个索引。