我需要合并来自不同表的多个列。我的代码需要很长时间才能查询。这是合并多个表的正确方法吗?有没有更有效的方法。我正在使用PostgreSQL 9.6.5。

SELECT
  i.id, i.hdm_id, i.tos,
  c.itemid, c.cid,
  pr.code,
  p.lag

  FROM Table1 i
  JOIN Table2 c ON
  i.id = c.id

  JOIN Table3 pr ON
  c.id = pr.id

 JOIN Table4 p ON
 pr.id = p.id ;

最佳答案

我将尝试创建覆盖您的查询的索引

create index ix_table1 on table1(id, hdm_id, tos)
create index ix_table2 on table2(id, itemid)
create index ix_table3 on table3(id, code)
create index ix_table4 on table4(id, lag)

你应该认真考虑这种询问的必要性。你真的需要所有的数据吗?

关于sql - 合并来自不同表的多个列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46804009/

10-12 17:44
查看更多