【问题标题】:Doing the one mode projection using sql query使用 sql 查询进行单模式投影
【发布时间】:2016-07-02 13:14:26
【问题描述】:

假设我们有一个包含以下内容的表:

UserID, ProjectID

此表在网络分析中有另一种表示形式,称为bipartite graph.

我们可以使用 SQL 查询创建一个高效的one mode projection 吗?

一种模式投影的示例: 假设该表是:

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

同样,ProjectID 的一种模式投影是:

P1,P2

【问题讨论】:

    标签: sql social-networking projection bipartite


    【解决方案1】:

    这在 SQL 中称为join

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

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

    【讨论】:

    • 我尝试了加入选项,但是对于大量记录,它需要很长时间。这是他们在 sql 中最有效的方式吗?
    • 检查我对 WITH (NOLOCK) 语句的回答
    • @M.M. . .您要确保该表在ProjectId 上有一个索引(更好的是(ProjectId, UserId))。
    【解决方案2】:

    使用以下方法使其运行得更快。通过应用 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 的提问

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多