Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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

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
Ruby on rails 如何提高ILIKE性能?_Ruby On Rails_Postgresql_Rails Activerecord - Fatal编程技术网

Ruby on rails 如何提高ILIKE性能?

Ruby on rails 如何提高ILIKE性能?,ruby-on-rails,postgresql,rails-activerecord,Ruby On Rails,Postgresql,Rails Activerecord,我的应用程序有一个页面,其中显示了一个州所有以特定字母开头的城市 例如: State: Alabama, Page A --> All cities in Alabama starting with alphabet 'A' 这是我的问题 City.where(state: 'Alabama').where("name ilike?", "a%") 这个查询大约需要110-140毫秒。有没有什么方法可以把查询时间缩短到PostgreSQL不使用常用的索引作为LIKE运算符 postgr

我的应用程序有一个页面,其中显示了一个州所有以特定字母开头的城市

例如:

State: Alabama, Page A
--> All cities in Alabama starting with alphabet 'A'
这是我的问题

City.where(state: 'Alabama').where("name ilike?", "a%")

这个查询大约需要110-140毫秒。有没有什么方法可以把查询时间缩短到PostgreSQL不使用常用的索引作为LIKE运算符

postgres=# create index on obce(nazev);
CREATE INDEX
Time: 120.605 ms
postgres=# explain analyze select * from obce where nazev like 'P%';
┌─────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                             QUERY     PLAN                                              │
╞═════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ Seq Scan on obce  (cost=0.00..137.12 rows=435 width=41) (actual time=0.023..2.345 rows=450 loops=1) │
│   Filter: ((nazev)::text ~~ 'P%'::text)                                                             │
│   Rows Removed by Filter: 5800                                                                      │
│ Planning time: 0.485 ms                                                                             │
│ Execution time: 2.413 ms                                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────────────────┘
(5 rows)
您应该对varchar\u pattern\u ops关键字使用特殊语法

注:Nazev是捷克语的名称

另一种可能是使用扩展和三元索引。它在两个方面都有效,比如ILIKE,但是索引要大得多——对于相对较小的静态表来说,这不是问题

create extension pg_trgm ;
create index on obce using gin (nazev gin_trgm_ops);

postgres=# explain analyze select * from obce where nazev like 'P%';
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                         QUERY PLAN                                                          │
╞═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ Bitmap Heap Scan on obce  (cost=15.37..79.81 rows=435 width=41) (actual time=0.327..0.933 rows=450 loops=1)                 │
│   Recheck Cond: ((nazev)::text ~~ 'P%'::text)                                                                               │
│   Rows Removed by Index Recheck: 134                                                                                        │
│   Heap Blocks: exact=58                                                                                                     │
│   ->  Bitmap Index Scan on obce_nazev_idx1  (cost=0.00..15.26 rows=435 width=0) (actual time=0.287..0.287 rows=584 loops=1) │
│         Index Cond: ((nazev)::text ~~ 'P%'::text)                                                                           │
│ Planning time: 0.359 ms                                                                                                     │
│ Execution time: 1.056 ms                                                                                                    │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘    
(8 rows)

PostgreSQL不使用LIKE运算符的常用索引

postgres=# create index on obce(nazev);
CREATE INDEX
Time: 120.605 ms
postgres=# explain analyze select * from obce where nazev like 'P%';
┌─────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                             QUERY     PLAN                                              │
╞═════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ Seq Scan on obce  (cost=0.00..137.12 rows=435 width=41) (actual time=0.023..2.345 rows=450 loops=1) │
│   Filter: ((nazev)::text ~~ 'P%'::text)                                                             │
│   Rows Removed by Filter: 5800                                                                      │
│ Planning time: 0.485 ms                                                                             │
│ Execution time: 2.413 ms                                                                            │
└─────────────────────────────────────────────────────────────────────────────────────────────────────┘
(5 rows)
您应该对varchar\u pattern\u ops关键字使用特殊语法

注:Nazev是捷克语的名称

另一种可能是使用扩展和三元索引。它在两个方面都有效,比如ILIKE,但是索引要大得多——对于相对较小的静态表来说,这不是问题

create extension pg_trgm ;
create index on obce using gin (nazev gin_trgm_ops);

postgres=# explain analyze select * from obce where nazev like 'P%';
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                         QUERY PLAN                                                          │
╞═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
│ Bitmap Heap Scan on obce  (cost=15.37..79.81 rows=435 width=41) (actual time=0.327..0.933 rows=450 loops=1)                 │
│   Recheck Cond: ((nazev)::text ~~ 'P%'::text)                                                                               │
│   Rows Removed by Index Recheck: 134                                                                                        │
│   Heap Blocks: exact=58                                                                                                     │
│   ->  Bitmap Index Scan on obce_nazev_idx1  (cost=0.00..15.26 rows=435 width=0) (actual time=0.287..0.287 rows=584 loops=1) │
│         Index Cond: ((nazev)::text ~~ 'P%'::text)                                                                           │
│ Planning time: 0.359 ms                                                                                                     │
│ Execution time: 1.056 ms                                                                                                    │
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘    
(8 rows)

您知道吗?@城市表的“名称”列上有一个数据库索引。我不熟悉“PostgreSQL解释”,那么你应该查一下。链接在@3чёччччы的评论中,您知道吗?@3чччCities表的“名称”列上有一个数据库索引。我不熟悉“PostgreSQL解释”,那么你应该查一下。链接在@3чччц的评论中