小编pLu*_*umo的帖子

为什么我使用 LIKE 的查询执行 seq 扫描?

我有一个包含 220 万行的表。

                                     Table "public.index"
  Column   |            Type             |                      Modifiers                      
-----------+-----------------------------+-----------------------------------------------------
 fid       | integer                     | not null default nextval('index_fid_seq'::regclass)
 location  | character varying           | 
Indexes:
    "index_pkey" PRIMARY KEY, btree (fid)
    "location_index" btree (location text_pattern_ops)
Run Code Online (Sandbox Code Playgroud)

该位置是文件的完整路径,但我需要使用文件所在文件夹的名称进行查询。该文件夹名称在表中是唯一的。

为了避免%一开始,我搜索我知道的完整路径:

select fid from index where location like '/path/to/folder/%'
Run Code Online (Sandbox Code Playgroud)

解释分析:

    QUERY PLAN                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Seq Scan on index  (cost=0.00..120223.34 rows=217 width=4) (actual time=1181.701..1181.701 rows=0 loops=1)
   Filter: ((location)::text ~~ '/path/to/folder/%'::text)
   Rows Removed by Filter: 2166034
 Planning time: 0.954 ms
 Execution time: 1181.748 ms
(5 rows) …
Run Code Online (Sandbox Code Playgroud)

sql postgresql indexing

3
推荐指数
1
解决办法
884
查看次数

标签 统计

indexing ×1

postgresql ×1

sql ×1