V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ryan19961996
V2EX  ›  PostgreSQL

洗数据:如何将 Contact 和 ContactTag 多对多关联起来

  •  
  •   ryan19961996 · 98 天前 · 555 次点击
    这是一个创建于 98 天前的主题,其中的信息可能已经有所发展或是发生改变。
    model Contact {
      id               String              @id @default(cuid())
      metadata         Json?               @db.JsonB
      contactTags      ContactTag[]
      ...
      @@index([metadata(ops: JsonbPathOps)], type: Gin)
    }
    
    model ContactTag {
      id          String      @id @default(cuid())
      slug        String
      contacts    Contact[]
      ...
    }
    

    由于历史遗留原因,Contact 和 ContactTag 之前没有关联关系。Contact 的 metadata 里面冗余了一份 tags,类似 {tags:["newsletter","new-customer"]}。通过标签筛选 Contact 的时候,是通过 查找 metadata 的 tags 。但是效率确实太低了, Contact 表的数量很多,大概是 1350 万行。所以我们想额外加一个表存储关联关系。请教一下各位大佬,相应的 sql 怎么写?

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1114 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 23:57 · PVG 07:57 · LAX 16:57 · JFK 19:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.