2016-08-21 3 views
1

내 응용 프로그램에는 특정 알파벳으로 시작하는 모든 도시가 표시된 페이지가 있습니다. 예를 들어ILIKE 성능을 향상시키는 방법은 무엇입니까?

:

State: Alabama, Page A 
--> All cities in Alabama starting with alphabet 'A' 

이 내 쿼리

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

이 쿼리는 ~ 110 걸립니다 - 140 밀리. 질문 시간을 < 10ms로 줄일 수있는 방법이 있습니까? 사전 :)에서

감사

+3

당신은 https://www.postgresql.org/ (['Database_index'] (https://en.wikipedia.org/wiki/Database_index)와 ['PostgreSQL을 기능 explain']에 대해 알고 계십니까 docs/9.4/static/using-explain.html)? –

+0

@ Cities 테이블에 'name'열에 데이터베이스 인덱스가 있습니다. 나는 'PostgreSQL 설명'에 익숙하지 않다. – Mahendhar

+0

그런 다음 그것을 찾아야한다. 링크는 @ Зелёный의 댓글에 있습니다. – e4c5

답변

3

PostgreSQL을 당신은 varchar_pattern_ops 키워드

postgres=# create index on obce(nazev varchar_pattern_ops); 
CREATE INDEX 
Time: 124.709 ms 
postgres=# explain analyze select * from obce where nazev like 'P%'; 
┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ 
│               QUERY PLAN               │ 
╞═════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╡ 
│ Bitmap Heap Scan on obce (cost=12.39..76.39 rows=435 width=41) (actual time=0.291..0.714 rows=450 loops=1)     │ 
│ Filter: ((nazev)::text ~~ 'P%'::text)                      │ 
│ Heap Blocks: exact=58                          │ 
│ -> Bitmap Index Scan on obce_nazev_idx1 (cost=0.00..12.28 rows=400 width=0) (actual time=0.253..0.253 rows=450 loops=1) │ 
│   Index Cond: (((nazev)::text ~>=~ 'P'::text) AND ((nazev)::text ~<~ 'Q'::text))          │ 
│ Planning time: 0.953 ms                          │ 
│ Execution time: 0.831 ms                         │ 
└─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘ 
(7 rows) 

과 특수 구문을 사용한다 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) 

을 보통의 인덱스를 사용하지 않습니다하지만이 '아무튼 ILIKE에 대한 작업 - 해결 방법은 기능 색인 일 수 있습니다.

create index on obce(upper(nazev) varchar_pattern_ops); 
select * from obce where upper(nazev) like upper('P%'); 

참고 : "Nazev는"

또 다른 가능성은 pg_trgm 확장을 사용하고 괘 인덱스를 사용 체코 어의 이름입니다. LIKE, 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) 
관련 문제