2014-09-25 50 views
0
=> SELECT * FROM "tags" WHERE ("kind" = 'View') ORDER BY "name"; 
Time: 278.318 ms 

標籤表包含358行。他們都是目前的看法。這個簡單的查詢怎麼會這麼久?

Column |   Type   |    Modifiers 
-------------+--------------------------+------------------------------------- 
id   | uuid      | not null default uuid_generate_v4() 
name  | text      | not null 
slug  | text      | not null 
kind  | text      | not null 
external_id | text      | 
created_at | timestamp with time zone | not null default now() 
updated_at | timestamp with time zone | 
filter  | json      | 
Indexes: 
    "tags_pkey" PRIMARY KEY, btree (id) 
    "tags_kind_index" btree (kind) 
    "tags_name_index" btree (name) 

分析說:

           QUERY PLAN 
---------------------------------------------------------------------------------------------------------- 
Sort (cost=9.29..9.47 rows=358 width=124) (actual time=0.654..0.696 rows=358 loops=1) 
    Sort Key: name 
    Sort Method: quicksort Memory: 75kB 
    -> Seq Scan on tags (cost=0.00..6.25 rows=358 width=124) (actual time=0.006..0.108 rows=358 loops=1) 
     Filter: (kind = 'View'::text) 
Total runtime: 0.756 ms 
(6 rows) 
+3

'解釋分析SELECT * FROM「標籤的輸出WHERE(」kind「='View')ORDER BY」name「;' – 2014-09-25 11:24:31

+0

done ........... – 2014-09-25 11:29:10

+1

'Total runtime :0.756 ms'對你來說太慢了? – joop 2014-09-25 11:30:21

回答

0

你運行analyze tags?它會更新表格的統計信息。

首先,如果所有kind值都是'view',那麼該列上的索引是無用的。索引僅在列的cardinality爲高時使用,否則在桌上進行順序掃描便宜。

其次,只有358行,無論如何進行順序掃描更便宜。