{"id":2739,"date":"2013-06-19T17:55:38","date_gmt":"2013-06-19T08:55:38","guid":{"rendered":"http:\/\/www.techscore.com\/blog\/?p=2739"},"modified":"2018-11-14T16:33:54","modified_gmt":"2018-11-14T07:33:54","slug":"postgresql-index-only-scan-%e5%a5%ae%e9%97%98%e8%a8%98-%e3%81%9d%e3%81%ae4","status":"publish","type":"post","link":"https:\/\/www.techscore.com\/blog\/2013\/06\/19\/postgresql-index-only-scan-%e5%a5%ae%e9%97%98%e8%a8%98-%e3%81%9d%e3%81%ae4\/","title":{"rendered":"PostgreSQL Index Only Scan \u596e\u95d8\u8a18 \u305d\u306e4"},"content":{"rendered":"<p>\u3069\u3046\u3082\u3002\u6751\u4e0a\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u30674\u56de\u76ee\u306ePostgreSQL\u3067\u3059\u3002<\/p>\n<p>\u3084\u3063\u3068\u30bf\u30a4\u30c8\u30eb\u306b\u3042\u308bIndex Only Scan\u306b\u6765\u307e\u3057\u305f\u3002<\/p>\n<p>\u8ffd\u8a18\u578b\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3<br \/>\n\u30d0\u30ad\u30e5\u30fc\u30e0<br \/>\nVisibility Map<br \/>\n\u30d7\u30e9\u30f3\u6f14\u7b97\u5b50 \u2190 \u524d\u56de\u30b3\u30b3\u307e\u3067<br \/>\nIndex Only Scan \u2190 \u4eca\u56de\u30b3\u30b3<\/p>\n<p>\u3067\u306f\u3067\u306f\u3001\u3044\u3088\u3044\u3088Index Only Scan\u3067\u3059\u3002<\/p>\n<h2>Index Only Scan\u3068\u306f\uff1f\uff1f\uff1f<\/h2>\n<p>Index Only Scan\u3068\u306f\u305d\u306e\u540d\u306e\u901a\u308a\u3001Index\u3060\u3051\u3067Scan\u3059\u308b\u30d7\u30e9\u30f3\u6f14\u7b97\u5b50\u3067\u3059\uff01\uff01<\/p>\n<p>\u524d\u56de\u3092\u601d\u3044\u51fa\u3057\u3066\u4e0b\u3055\u3044\u3002<\/p>\n<p>Seq Scan\u306f\u30c6\u30fc\u30d6\u30eb\u3092\u30b9\u30ad\u30e3\u30f3\u3057\u307e\u3059\u3002<br \/>\nIndex Scan\u306fIndex\u3092\u30b9\u30ad\u30e3\u30f3\u5f8c\u306b\u30c6\u30fc\u30d6\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3001\u8a72\u5f53\u884c\u3092\u53d6\u5f97\u3057\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001\u691c\u7d22\u6642\u306f\u5fc5\u305a\u30c6\u30fc\u30d6\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3067\u3082\u3001\u4f8b\u3048\u3070\u3053\u3093\u306a\u30c6\u30fc\u30d6\u30eb<\/p>\n<pre class=\"lang:sql decode:true\">      Column      |            Type             |                     Modifiers\r\n------------------+-----------------------------+----------------------------------------------------\r\n id               | integer                     | not null default nextval('users_id_seq'::regclass)\r\n login            | character varying(255)      | not null\r\n mail_address     | character varying(255)      | not null\r\n family_name      | character varying(255)      | not null\r\n first_name       | character varying(255)      | not null\r\n family_name_kana | character varying(255)      |\r\n first_name_kana  | character varying(255)      |\r\n birthday         | date                        |\r\n gender           | integer                     |\r\n post_number      | character varying(255)      |\r\n prefecture       | character varying(255)      |\r\n city             | character varying(255)      |\r\n address          | character varying(255)      |\r\n building         | character varying(255)      |\r\n telephone        | character varying(255)      |\r\n created_at       | timestamp without time zone |\r\n updated_at       | timestamp without time zone |\r\nIndexes:\r\n    \"users_pkey\" PRIMARY KEY, btree (id)\r\n    \"users_login_idx\" btree (login)<\/pre>\n<p>\u3053\u3093\u306aSQL\u3092\u5b9f\u884c\u3059\u308b\u6642<\/p>\n<pre class=\"lang:sql decode:true\">select login from users;<\/pre>\n<p>\u308f\u3056\u308f\u3056\u30c6\u30fc\u30d6\u30eb\u3092\u53c2\u7167\u3057\u306a\u304f\u3066\u3082\u3088\u304f\u306a\u3044\u3067\u3059\u304b\uff1f<\/p>\n<p>\u300cIndex\u3092\u30b9\u30ad\u30e3\u30f3\u3057\u305f\u6642\u306b\u3001\u53d6\u5f97\u3059\u308b\u9805\u76ee\u3067\u3042\u308b\u300elogin\u300f\u306fIndex\u60c5\u5831\u306b\u3042\u308b\u306e\u3067\u3001<br \/>\n\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u5f15\u3063\u3053\u629c\u3044\u3066\u300elogin\u300f\u306e\u9805\u76ee\u3092\u53d6\u5f97\u305b\u305a\u3068\u3082\u3001Index\u306b\u3042\u308b\u300elogin\u300f\u3092\u53d6\u5f97\u3059\u308c\u3070\u826f\u3044\u306e\u3067\u306f\uff1f\u300d<\/p>\n<p>\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001<\/p>\n<p>\u300cIndex\u304b\u3089\u60c5\u5831\u304c\u53d6\u5f97\u3067\u304d\u308b\u306e\u306a\u3089\u3001\u30c6\u30fc\u30d6\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u305b\u305a\u306b\u3001Index\u3060\u3051\u306eScan\u3067\u30c7\u30fc\u30bf\u3092\u8fd4\u3057\u307e\u3057\u3087\u3046\u3088\u3002\u300d<\/p>\n<p>\u3068\u3044\u3046\u306e\u304c<strong>Index Only Scan<\/strong>\u3067\u3059\u3002<\/p>\n<h2>\u306a\u305c\u4eca\u307e\u3067\u306a\u304b\u3063\u305f\u306e\uff1f\uff1f\uff1f<\/h2>\n<p>Index Only Scan\u306e\u5185\u5bb9\u3092\u805e\u304f\u3068\u5f53\u305f\u308a\u524d\u306e\u3088\u3046\u306b\u805e\u3053\u3048\u308b\u3057\u3001\u4ed6\u306eDBMS\u3067\u306f\u3059\u3067\u306b\u5b9f\u88c5\u3055\u308c\u3066\u3044\u308b\u6a5f\u80fd\u3067\u3059\u3002<br \/>\nPostgreSQL\u306b\u3082\u3042\u3063\u3066\u5f53\u7136\u306e\u3088\u3046\u306b\u611f\u3058\u308b\u3051\u3069\u3001\u306a\u305c\u4eca\u307e\u3067\u306a\u304b\u3063\u305f\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u5b9f\u306fPostgreSQL\u3067\u306f\u3001Index Only Scan\u306e\u5b9f\u88c5\u304c\u96e3\u3057\u3044\u3068\u3055\u308c\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u306f\u306a\u305c\u304b\uff1f<\/p>\n<p>PostgreSQL\u304c\u8ffd\u8a18\u578b\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3\u3092\u63a1\u7528\u3057\u3066\u3044\u305f\u304b\u3089\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001login\u3001mail_address\u306e\u8907\u5408Index\u304cusers\u30c6\u30fc\u30d6\u30eb\u306b\u4f5c\u6210\u3055\u308c\u3066\u3044\u305f\u3068\u3057\u307e\u3059\u3002<br \/>\n\u305d\u306e\u6642\u306b\u3001<\/p>\n<pre class=\"lang:sql decode:true\">select mail_address from users where login = 'XXXX';<\/pre>\n<p>\u3068SQL\u3092\u5b9f\u884c\u3059\u308b\u3068\u3001\u4f5c\u6210\u3057\u3066\u3044\u308b\u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002<br \/>\n\u8907\u5408\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u305f\u3081\u3001mail_address\u3082\u540c\u3058\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u3042\u308b\u306e\u3067\u3001\u3053\u306e\u5024\u3092\u8fd4\u305b\u3070\u3044\u3044\u3088\u3046\u306b\u601d\u3044\u307e\u3059\u304c\u3001\u305d\u3046\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\n\u3082\u3057\u3001SELECT\u5b9f\u884c\u6642\u306bmail_address\u306e\u5024\u304c\u5225\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304b\u3089\u5909\u66f4\u3055\u308c\u308c\u3070\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u5185\u306e\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u306f\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001Index\u306b\u306fMVCC\u3092\u5224\u65ad\u3059\u308b\u60c5\u5831\u3092\u6301\u3063\u3066\u3044\u306a\u3044\u305f\u3081\u3001\u30c6\u30fc\u30d6\u30eb\u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u307f\u306a\u3044\u3068\u6b63\u78ba\u306a\u5024\u304c\u308f\u304b\u3089\u306a\u3044\u306e\u3067\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u30d0\u30fc\u30b8\u30e7\u30f39.2\u304b\u3089\u8ffd\u52a0\u3055\u308c\u305fIndex Only Scan\u306f\u3069\u3046\u3084\u3063\u3066\u5b9f\u73fe\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<h2>Index Only Scan\u306e\u5b9f\u73fe<\/h2>\n<p>MVCC\u3092\u5224\u65ad\u3059\u308b\u60c5\u5831(\u8a72\u5f53\u30ec\u30b3\u30fc\u30c9\u304c\u66f4\u65b0\u3055\u308c\u305f\u306e\u304b\u3001\u524a\u9664\u3055\u308c\u305f\u306e\u304b\u3068\u3044\u3046\u60c5\u5831)\u306f\u30c6\u30fc\u30d6\u30eb\u306b\u3057\u304b\u306a\u3044\u306e\u3067\u3001PostgreSQL\u3067\u306f\u30c6\u30fc\u30d6\u30eb\u306e\u53c2\u7167\u306a\u3057\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u306e\u306f\u96e3\u3057\u3044\u3068\u3044\u3046\u3053\u3068\u3067\u3057\u305f\u3002<\/p>\n<p>\u3053\u3053\u3067\u3001\u601d\u3044\u51fa\u3057\u3066\u4e0b\u3055\u3044\u3002<br \/>\n\u30c6\u30fc\u30d6\u30eb\u4ee5\u5916\u306b\u3082\u30c7\u30fc\u30bf\u304c\u5909\u66f4\u3055\u308c\u305f\u3069\u3046\u304b\u306e\u60c5\u5831\u3092\u6301\u3063\u3066\u3044\u308b\u6a5f\u80fd\u304c\u3042\u3063\u305f\u3053\u3068\u3092\u3002\u3002\u3002\u3002<\/p>\n<p>\u305d\u308c\u306f\u3002\u3002\u3002<\/p>\n<p>\u300cVisibility Map\u300d\u3067\u3059\u3002<\/p>\n<p>Visibility Map\u306f\u300c\u3059\u3079\u3066\u306e\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u304b\u3089\u53ef\u8996\u304b\u3069\u3046\u304b\u30921\u30da\u30fc\u30b81\u30d3\u30c3\u30c8\u3067\u6301\u3064\u300d\u3068\u3044\u3046\u3082\u306e\u3067\u3057\u305f\u3002<\/p>\n<p>Visibility Map\u3092\u53c2\u7167\u3059\u308c\u3070\u3001\u53ef\u8996\u304b\u3069\u3046\u304b\u308f\u304b\u308a\u307e\u3059\u3002<br \/>\n\u3064\u307e\u308a\u3001\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u5b9f\u884c\u4e2d\u304b\u3069\u3046\u304b\u304c\u5224\u65ad\u3067\u304d\u3001\u30c7\u30fc\u30bf\u304c\u5909\u66f4\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>\u3082\u3057\u3001\u5909\u66f4\u3055\u308c\u3066\u3044\u306a\u3044\u306e\u3067\u3042\u308c\u3070\u3001Index\u304b\u3089\u5bfe\u8c61\u306e\u9805\u76ee\u306e\u5024\u3092\u53d6\u5f97\u3057\u3066\u3082\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p><a href=\"https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/06\/postgresql-techscore-blog4-1.png\" rel=\"facebox\" rel=\"attachment wp-att-2852\"><img loading=\"lazy\" class=\"alignnone size-medium wp-image-2852\" alt=\"postgresql-techscore-blog4-1\" src=\"https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/06\/postgresql-techscore-blog4-1.png\" width=\"300\" height=\"161\" srcset=\"https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/06\/postgresql-techscore-blog4-1.png 854w, https:\/\/www.techscore.com\/blog\/wp\/wp-content\/uploads\/2013\/06\/postgresql-techscore-blog4-1-300x161.png 300w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h2>Index Only Scan\u306eExplain<\/h2>\n<p>\u3067\u306f\u3001Index Only Scan\u306eExplain\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre class=\"lang:sql decode:true\">=# explain select login from users;\r\n                          QUERY PLAN\r\n-----------------------------------------------------------------------------------------------\r\n Index Only Scan using users_login_idx on users  (cost=0.00..346914.04 rows=10000140 width=19)<\/pre>\n<p><strong>Good!!!<\/strong><\/p>\n<p>\u3061\u306a\u307f\u306bselect count(*)\u3067\u3082\u52b9\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sql decode:true\">=# explain select count(*) from users;\r\n                          QUERY PLAN\r\n-----------------------------------------------------------------------------------------------\r\n Aggregate  (cost=284701.67..284701.68 rows=1 width=0)\r\n   -&gt;  Index Only Scan using users_pkey on users  (cost=0.00..259701.32 rows=10000140 width=0)<\/pre>\n<p><strong>Nice!!!<\/strong><\/p>\n<p>\u6b21\u306bSeq Scan \u3068\u306e\u30b3\u30b9\u30c8\u5dee\u3092\u898b\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>Seq Scan<\/h3>\n<pre class=\"lang:sql decode:true\">=# explain analyze select login from users;\r\n                          QUERY PLAN\r\n----------------------------------------------------------------------------------------------------------------------\r\n Seq Scan on users  (cost=0.00..385071.40 rows=10000140 width=19) (actual time=6.315..9402.347 rows=10000000 loops=1)\r\n Total runtime: 10287.434 ms<\/pre>\n<h3>Index Only Scan<\/h3>\n<pre class=\"lang:sql decode:true\">=# explain analyze select login from users;\r\n                          QUERY PLAN\r\n----------------------------------------------------------------------------------------------------------------------------------------------------\r\n Index Only Scan using users_login_idx on users  (cost=0.00..346914.04 rows=10000140 width=19) (actual time=16.714..4888.194 rows=10000000 loops=1)\r\n   Heap Fetches: 0\r\n Total runtime: 5720.321 ms<\/pre>\n<p><strong>\u306f\u3084\u3044\uff01\uff01<\/strong><\/p>\n<h2>\u307e\u3068\u3081<\/h2>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001Index Only Scan\u306b\u3064\u3044\u3066\u8aac\u660e\u3057\u3066\u304d\u307e\u3057\u305f\u3002<br \/>\n\u3067\u3082\u3001\u4eca\u56de\u3067\u6700\u7d42\u56de\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u3053\u306e\u9023\u8f09\u8a18\u4e8b\u306e\u30bf\u30a4\u30c8\u30eb\u306b\u300c\u596e\u95d8\u8a18\u300d\u3068\u3044\u3046\u5358\u8a9e\u304c\u4ed8\u3044\u3066\u3044\u307e\u3059\u3002<br \/>\n\u305d\u308c\u306f\u306a\u305c\u304b\uff01\uff1f<\/p>\n<p>Index Only Scan\u306f\u306a\u304b\u306a\u304b\u306a\u6c17\u5206\u5c4b\u306a\u3093\u3067\u3059\u3002<br \/>\n\u52b9\u3044\u305f\u308a\u52b9\u304b\u306a\u304b\u3063\u305f\u308a\u3001\u52b9\u3044\u3066\u3082\u9045\u304b\u3063\u305f\u308a\u3002\u3002\u3002<br \/>\n\u306a\u3093\u3067\u3001\u305d\u3053\u3089\u8fba\u306e\u596e\u95d8\u3057\u305f\u5185\u5bb9\u3092\u6b21\u56de\u306b\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u8ffd\u8a18\u578b\u30a2\u30fc\u30ad\u30c6\u30af\u30c1\u30e3<br \/>\n\u30d0\u30ad\u30e5\u30fc\u30e0<br \/>\nVisibility Map<br \/>\n\u30d7\u30e9\u30f3\u6f14\u7b97\u5b50<br \/>\nIndex Only Scan \u2190 \u4eca\u56de\u30b3\u30b3<br \/>\n\u596e\u95d8\u8a18 \u2190 \u8ffd\u52a0\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3069\u3046\u3082\u3002\u6751\u4e0a\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u30674\u56de\u76ee\u306ePostgreSQL\u3067\u3059\u3002<\/p>\n<p>\u3084\u3063\u3068\u30bf\u30a4\u30c8\u30eb\u306b\u3042\u308bIndex Only Scan\u306b\u6765\u307e\u3057\u305f\u3002<br \/><a href=\"https:\/\/www.techscore.com\/blog\/2013\/06\/19\/postgresql-index-only-scan-%e5%a5%ae%e9%97%98%e8%a8%98-%e3%81%9d%e3%81%ae4\/\">\u7d9a\u304d\u3092\u8aad\u3080...<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[97],"tags":[206],"_links":{"self":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2739"}],"collection":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/comments?post=2739"}],"version-history":[{"count":1,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2739\/revisions"}],"predecessor-version":[{"id":17924,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/posts\/2739\/revisions\/17924"}],"wp:attachment":[{"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/media?parent=2739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/categories?post=2739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techscore.com\/blog\/wp-json\/wp\/v2\/tags?post=2739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}