BaserCMSでPostgreSQLを使う場合に貼っておいたほうがいいINDEX一覧(ページ編)|幡司

この記事は
BaserCMSでPostgreSQLを使う場合に貼っておいたほうがいいINDEX一覧(ブログ編)
の続きとなっています。まずはそちらをご覧ください。
さて、今回はページ編です。
BaserCMSのページには、ブログと違い、「タグ」はないようです。
なので問題になりそうなのは「カテゴリー」周り。
一応DB覗いてみましょう。
public | bc_page_categories | table | postgres
public | bc_pages | table | postgres
今回はもうちょっと詳しく紹介、テーブル構成見てみます。
Table "public.bc_pages"
Column | Type | Modifiers
------------------+-----------------------------+-------------------------------------------------------
id | integer | not null default nextval('bc_pages_id_seq'::regclass)
sort | integer |
name | character varying(50) | default NULL::character varying
title | character varying(255) | default NULL::character varying
description | character varying(255) | default NULL::character varying
contents | text |
page_category_id | integer |
status | boolean |
url | text |
draft | text |
author_id | integer |
publish_begin | timestamp without time zone |
publish_end | timestamp without time zone |
modified | timestamp without time zone |
created | timestamp without time zone |
exclude_search | boolean |
Indexes:
"bc_pages_pkey" PRIMARY KEY, btree (id)
デフォルトだとこんな感じです。
次に登録されたデータを見てみましょう。
baser-1-6-14=# SELECT id , sort , page_category_id , url ,author_id from bc_pages;
id | sort | page_category_id | url | author_id
----+------+------------------+---------------------+-----------
9 | 9 | 2 | /smartphone/index |
5 | 5 | 1 | /mobile/index | 1
2 | 2 | | /about | 1
6 | 6 | 1 | /mobile/service | 1
7 | 7 | 1 | /mobile/about | 1
8 | 8 | 2 | /smartphone/recruit |
1 | 1 | | /index | 1
4 | 4 | | /sitemap | 1
11 | 11 | 2 | /smartphone/service |
10 | 10 | 2 | /smartphone/sitemap |
12 | 12 | 2 | /smartphone/about |
3 | 3 | | /service | 1
17 | 13 | 2 | /smartPhone | 1
18 | 14 | 1 | /testhataji | 2
(14 rows)
page_category_id は名前の通りカテゴリーの外部キーですね。
author_idはログインユーザーの外部キーのようです。
どちらも登録数が少なければ、貼らなくてもいいんですけど、まぁ今回は「どこに貼るか」ってネタ記事なので貼っときましょう。
今後増えるときにネックになりそうなところは、貼っておいていいんじゃないかなーってのが僕の考えです。
CREATE INDEX bc_pages_sort_idx ON bc_pages(sort) ;
CREATE INDEX bc_pages_page_category_id_idx ON bc_pages(page_category_id) ;
CREATE INDEX bc_pages_author_id_idx ON bc_pages(author_id) ;
以上です。簡単ですね。
次にbc_page_categoriesです。
適当に色々登録して見ました。
BaserCMSのカテゴリは、階層が保存できるので大変便利

で、階層を幾つか指定して、保存してみたデータがこれ。
baser-1-6-14=# SELECT * from bc_page_categories ORDER BY id ;
id | parent_id | lft | rght | name | title | sort | contents_navi | modified | created | owner_id
----+-----------+-----+------+------------------------+------------------------+------+---------------+---------------------+---------------------+----------
1 | | 1 | 6 | mobile | モバイル | 1 | f | | 2011-06-13 16:53:32 |
2 | | 7 | 12 | smartphone | スマートフォン | | f | 2011-10-12 15:03:50 | 2011-10-12 15:03:50 |
6 | | 13 | 22 | testCategory | testCategory | | f | 2011-11-05 12:57:55 | 2011-11-05 12:57:55 |
7 | 1 | 2 | 5 | test2 | test2 | | t | 2011-11-05 12:58:14 | 2011-11-05 12:58:14 |
8 | 6 | 14 | 21 | aaaa | eeeee | | t | 2011-11-05 12:58:26 | 2011-11-05 12:58:26 |
9 | 8 | 15 | 20 | test22 | テストカテゴリー22 | | t | 2011-11-05 12:58:53 | 2011-11-05 12:58:53 |
10 | 7 | 3 | 4 | モバイルテスト | モバイルテスト | | t | 2011-11-05 12:59:21 | 2011-11-05 12:59:21 |
11 | 2 | 8 | 11 | スマホテスト | スマホテスト | | f | 2011-11-05 12:59:33 | 2011-11-05 12:59:33 |
12 | 11 | 9 | 10 | スマホテスト2 | スマホテスト2 | | f | 2011-11-05 12:59:46 | 2011-11-05 12:59:46 |
13 | 9 | 16 | 17 | カテゴリテスト3階層目 | カテゴリテスト3階層目 | | t | 2011-11-05 13:01:17 | 2011-11-05 13:01:16 |
14 | 9 | 18 | 19 | テスト3階層目の2 | テスト3階層目の2 | | f | 2011-11-05 13:02:32 | 2011-11-05 13:02:32 |
(11 rows)
sort と owner_id は使ってないんですね。
parent_id は親カテゴリのIDみたい。
で、lft と rgh ていうカラム。これはどうもソートに使ってるみたい。
このへんも貼っときましょう。確信は無いですが。
そのうちエロイ人がもっと詳しく調べてくれるはず。w
CREATE INDEX bc_page_categories_parent_id_idx ON bc_page_categories(parent_id) ;
CREATE INDEX bc_page_categories_lft_idx ON bc_page_categories(lft) ;
CREATE INDEX bc_page_categories_rght_idx ON bc_page_categories(rght) ;
これで以上です。
これこれでも遅い〜ってひとは統計取りながらの話になります。
あと、DB以外で遅いかもしれませんからね。
スロークエリを見つける方法は、前回も紹介したスロークエリの分析で見つける方法と、
あとは「デバッグモード」も使えると思います。
管理画面から「システム設定」で「デバッグモード2」を選ぶと実際に走っているSQLが表示されます。

その一覧を見ながらネックになりそうなSQLを探してEXPLAIN ANALYZE で調べてみるとかですね。
そのへんの地味な作業になってくると思います。
とりあえず今日はここまで。
NEWS一覧へ
