Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 在使用多个联接运行查询时,使用表中的特定索引_Postgresql_Join_Indexing - Fatal编程技术网

Postgresql 在使用多个联接运行查询时,使用表中的特定索引

Postgresql 在使用多个联接运行查询时,使用表中的特定索引,postgresql,join,indexing,Postgresql,Join,Indexing,我在postgres表上有多个索引- 主id上(用户id上的通知权限) 在三个不同的列上(通知\通道\路由\类型) 我正在运行一个包含两个联接的查询,第二个联接更符合我的要求。但是当我分析查询时,它会自动使用主索引,我假设这是由postgres本身根据查询决定的 Index Scan using notification_permission_on_user_id on notification_permission (cost=0.28..0.30 rows=1 width=21) (n

我在postgres表上有多个索引-

  • 主id上(用户id上的通知权限)
  • 在三个不同的列上(通知\通道\路由\类型)
我正在运行一个包含两个联接的查询,第二个联接更符合我的要求。但是当我分析查询时,它会自动使用主索引,我假设这是由postgres本身根据查询决定的

Index Scan using notification_permission_on_user_id on notification_permission  (cost=0.28..0.30 rows=1 width=21) (never executed)
           Index Cond: (user_id = u.id)
           Filter: ((channel = 1) AND (type = 2))

如何确保我的查询使用第二个索引,这符合我的需要,并将真正加快处理过程?

更改条件,使该索引无法使用:

... WHERE/ON notification_permission.user_id + 0 = u.id

为什么你认为第二个指数更适合你的需要?它很难比主键更具选择性。另外,
(channel,route,type)
上的索引并不完全适合于指定“type”而不是“route”的查询。实际上,在我的查询中,我使用了所有三个参数:(channel,route,type),对于每个查询,我都需要具有特定值的结果。因此,我认为我应该使用第二个索引,因为这将根据我的确切要求过滤我的数据,并且花费更少的时间,因为我将对更大的数据集进行过滤。但是您的计划片段没有显示过滤器中使用的“路线”。它是否适用于计划中的其他地方?