Postgresql 在两种情况下连接大表而不超时

Postgresql 在两种情况下连接大表而不超时,postgresql,Postgresql,我试图将两个表连接在一起,其中一个表有我想要的两个标识符 select stations.id, stations.name, count (rentals.startstation_id) as station_starts, count (rentals.endstation_id) as station_ends from ny.station as stations Left Join ny.rental as rentals on stations.id = rentals.start

我试图将两个表连接在一起,其中一个表有我想要的两个标识符

select stations.id, stations.name, count (rentals.startstation_id) as station_starts, count (rentals.endstation_id) as station_ends
from ny.station as stations
Left Join ny.rental as rentals on stations.id = rentals.startstation_id
Left Join ny.rental as rentals on station.id = rentals.endstation_id
group by 1,2
但是,ny.rental表非常大,当我运行此查询时,我的SQL Workbench/J崩溃。这张桌子相当小


构造此查询的最佳方法是什么?

为什么要两次加入它并两次以相同的名称引用它。这似乎很令人困惑。您可能只需要使用上面提到的and或orAs@JoeLove加入一次,问题是您加入了两次
ny.rent
。您需要做的是将连接条件分组为一个条件,使用
这两个条件中最适合您的情况。