简体   繁体   English

连接表时未使用索引

[英]Index not being used when joining tables

We have four tables involved: 我们涉及四个表:

  • lists 名单
  • influencer_lists (join table) impactr_lists(联接表)

This is the query: 这是查询:

SELECT influencer_lists.influencer_id 
FROM influencer_lists
LEFT OUTER JOIN lists ON lists.id = influencer_lists.list_id
WHERE influencer_lists.influencer_id IN (12, 95, 33, 23, 35, 36, 27, 41, 42, 43, 45, 30) 
AND "lists"."user_id" = 1

When doing explain, two things are noticed: 在进行解释时,注意两件事:

  1. No index is being used at some point when looking for influencer_id 寻找impacter_id时,某些时候未使用任何索引
  2. Index is being used for 索引用于

EXPLAIN output: 解释输出:

Nested Loop  (cost=0.28..73.59 rows=9 width=4) (actual time=0.031..0.187 rows=4 loops=1)
->  Seq Scan on influencer_lists  (cost=0.00..10.82 rows=9 width=8) (actual time=0.016..0.152 rows=5 loops=1)
Filter: (influencer_id = ANY ('{12,95,33,23,35,36,27,41,42,43,45,30}'::integer[]))
Rows Removed by Filter: 308
->  Index Scan using lists_pkey on lists  (cost=0.28..6.96 rows=1 width=4) (actual time=0.005..0.005 rows=1 loops=5)
Index Cond: (id = influencer_lists.list_id)
Filter: (user_id = 1)
Rows Removed by Filter: 0
Planning time: 0.621 ms
Execution time: 0.235 ms

We want to improve the query time. 我们希望缩短查询时间。 How can we do it? 我们该怎么做?

Thanks. 谢谢。

The first thing i would suggest is to update the stats for the tables involved. 我建议的第一件事是更新所涉及表的统计信息。 If you are experience performance this is a great first start. 如果您有表演的经验,那么这是一个很好的开始。

 ANALYZE [tablename] ;

Secondly because there is an index on this column it does not mean it will always be used. 其次,由于此列上有一个索引,并不意味着将始终使用它。 The range of values in the 'IN' clause may consist of too many id's to warrant using the index. “ IN”子句中的值范围可能包含太多ID,以至于无法使用索引。 You could try individual id = [value] 您可以尝试使用个人ID = [值]

If the SELECT returns more than approximately 5-10%(depends on configuration settings and the storage of the data as well. It's not a hard number) of all rows in the table, a sequential scan is much faster than an index scan. 如果SELECT返回表中所有行的大约5-10%(还取决于配置设置和数据的存储。这不是一个硬数字),则顺序扫描比索引扫描快得多。

Index scan requires several IO operations for each row (look up the row in the index, then retrieve the row from the heap). 索引扫描需要为每行进行几次IO操作(在索引中查找该行,然后从堆中检索该行)。 Sequential scan only requires a single IO for each row - or even less because a block (page) on the disk contains more than one row, so more than one row can be fetched with a single IO operation. 顺序扫描每行只需要一个IO-甚至更少,因为磁盘上的一个块(页面)包含多于一行,因此可以通过一次IO操作获取多于一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM