{"id":459,"date":"2013-09-15T02:50:09","date_gmt":"2013-09-14T17:50:09","guid":{"rendered":"http:\/\/1bed.allright.life\/?p=459"},"modified":"2020-04-19T00:00:48","modified_gmt":"2020-04-18T15:00:48","slug":"post-459","status":"publish","type":"post","link":"https:\/\/1bed.allright.life\/?p=459","title":{"rendered":"group by \u3068 distinct \u901f\u5ea6\u8a55\u4fa1"},"content":{"rendered":"<p>\u5f8c\u8f29\u304c\u8907\u6570\u4ef6\u3042\u308b\u30c7\u30fc\u30bf\u304b\u3089\u30e6\u30cb\u30fc\u30af\u306a\u30c7\u30fc\u30bf\u3092\u4f5c\u308b\u6642\u306b distinct<br \/>\n\u3092\u4f7f\u3063\u3066\u3044\u305f\u3002 \u4ffa\u306f group by<br \/>\n\u306e\u65b9\u304c\u9ad8\u901f\u3060\u3088\u3068\u4f55\u304b\u8aad\u3093\u3060\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3061\u3087\u3063\u3068\u9a5a\u3044\u305f\u3002<\/p>\n<p>\u3068\u308a\u3042\u3048\u305a\u6307\u6458\u306f\u3057\u305f\u3093\u3060\u3051\u3069\u3001\u3069\u3046\u306b\u3082\u8151\u306b\u843d\u3061\u306a\u304b\u3063\u305f\u306e\u3067\u3001\u5b9f\u969b\u306b\u3069\u3061\u3089\u304c\u826f\u3044\u306e\u304b\u81ea\u5206\u306a\u308a\u306b\u8abf\u3079\u3066\u307f\u305f\u3002<\/p>\n<h2>\u5171\u901a\u6761\u4ef6<\/h2>\n<ul>\n<li>\u67d0\u30af\u30e9\u30a6\u30c9\u3001\u30e1\u30e2\u30ea 2GB<\/li>\n<li>\u30ec\u30b3\u30fc\u30c9\u4ef6\u6570 10\u4e07\u4ef6<\/li>\n<li>name \u306b\u306f 47\u90fd\u9053\u5e9c\u770c\u540d\u304c\u30e9\u30f3\u30c0\u30e0\u306b\u5165\u3063\u3066\u3044\u308b \u25cb\u4f7f\u7528\u30c6\u30fc\u30d6\u30eb<\/li>\n<\/ul>\n<pre><code class=\"language-sql\">CREATE TABLE pgtest\n(\n    id         serial      PRIMARY KEY,\n    name       text,\n    data       text,\n    created_at timestamp(0) DEFAULT now(),\n    updated_at timestamp(0) DEFAULT now()\n);<\/code><\/pre>\n<h3>\u6761\u4ef61\uff1aPostgreSQL 7.4 \u306e\u6642<\/h3>\n<ul>\n<li>\n<p>EXPLAIN ANALYZE select distinct on (name) name FROM pgtest;<br \/>\nUnique ->Sort ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 3223.304 ms<\/p>\n<\/li>\n<li>\n<p>EXPLAIN ANALYZE select distinct name FROM pgtest;<br \/>\nUnique ->Sort ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 3206.429 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select name from pgtest group by name;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 272.049 ms<\/p>\n<\/li>\n<\/ul>\n<h3>\u6761\u4ef62\uff1aPostgreSQL 7.4 \u3067 name \u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u3063\u305f\u6642<\/h3>\n<ul>\n<li>\n<p>EXPLAIN ANALYZE select distinct on (name) name FROM pgtest;<br \/>\nUnique ->Sort ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 3219.604 ms<\/p>\n<\/li>\n<li>\n<p>EXPLAIN ANALYZE select distinct name FROM pgtest;<br \/>\nUnique ->Sort ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 3217.941 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select name from pgtest group by name;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 267.396 ms<\/p>\n<\/li>\n<\/ul>\n<h3>\u6761\u4ef63\uff1aPostgreSQL 8.4 \u306e\u6642<\/h3>\n<ul>\n<li>\n<p>EXPLAIN ANALYZE select distinct on (name) name FROM pgtest;<br \/>\nUnique ->Sort ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 2785.410 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select distinct name FROM pgtest;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 233.662 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select name from pgtest group by name;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 230.086 ms<\/p>\n<\/li>\n<\/ul>\n<h3>\u6761\u4ef64\uff1aPostgreSQL 8.4 \u3067 name \u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u3063\u305f\u6642<\/h3>\n<ul>\n<li>\n<p>EXPLAIN ANALYZE select distinct on (name) name FROM pgtest;<br \/>\nUnique ->Index Scan using pgtest_idx1 on pgtest<\/p>\n<p>Total runtime: 267.068 ms<\/p>\n<\/li>\n<li>\n<p>EXPLAIN ANALYZE select distinct name FROM pgtest; Unique ->Index<br \/>\nScan using pgtest_idx1 on pgtest<\/p>\n<p>Total runtime: 267.044 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select name from pgtest group by name;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime:231.749 ms<\/p>\n<\/li>\n<\/ul>\n<h3>\u6761\u4ef65\uff1aPostgreSQL 9.3 \u306e\u6642<\/h3>\n<ul>\n<li>\n<p>EXPLAIN ANALYZE select distinct on (name) name FROM pgtest;<br \/>\nUnique ->Sort ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 2902.322 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select distinct name FROM pgtest;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime:239.562 ms<\/p>\n<\/li>\n<li>\n<p><strong>EXPLAIN ANALYZE select name from pgtest group by name;<\/strong><br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime:235.504 ms<\/p>\n<\/li>\n<\/ul>\n<h3>\u6761\u4ef66\uff1aPostgreSQL 9.3 \u3067 name \u306b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u306f\u3063\u305f\u6642<\/h3>\n<ul>\n<li>\n<p><strong>EXPLAIN ANALYZE select distinct on (name) name FROM pgtest;<\/strong><br \/>\nUnique ->Index Only Scan using pgtest_idx1 on pgtest<\/p>\n<p>Total runtime: 222.463 ms<\/p>\n<\/li>\n<li>\n<p>EXPLAIN ANALYZE select distinct name FROM pgtest;<br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 235.667 ms<\/p>\n<\/li>\n<li>\n<p>EXPLAIN ANALYZE select name from pgtest group by name;<br \/>\nHashAggregate ->Seq Scan on pgtest<\/p>\n<p>Total runtime: 232.319 ms<\/p>\n<\/li>\n<\/ul>\n<p>\u4ffa\u306a\u308a\u306e\u7d50\u8ad6\u3068\u3057\u3066\u306f\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u3084\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5de6\u53f3\u3055\u308c\u3066\u3044\u306a\u3044 group by \u306e\u65b9\u304c\u826f\u3044\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f8c\u8f29\u304c\u8907\u6570\u4ef6\u3042\u308b\u30c7\u30fc\u30bf\u304b\u3089\u30e6\u30cb\u30fc\u30af\u306a\u30c7\u30fc\u30bf\u3092\u4f5c\u308b\u6642\u306b distinct \u3092\u4f7f\u3063\u3066\u3044\u305f\u3002 \u4ffa\u306f group by \u306e\u65b9\u304c\u9ad8\u901f\u3060\u3088\u3068\u4f55\u304b\u8aad\u3093\u3060\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3061\u3087\u3063\u3068\u9a5a\u3044\u305f\u3002 \u3068\u308a\u3042\u3048\u305a\u6307\u6458\u306f\u3057\u305f\u3093\u3060\u3051\u3069\u3001\u3069\u3046\u306b\u3082\u8151\u306b\u843d\u3061\u306a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[25],"class_list":["post-459","post","type-post","status-publish","format-standard","hentry","category-database","tag-postgresql"],"_links":{"self":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts\/459","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=459"}],"version-history":[{"count":37,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions"}],"predecessor-version":[{"id":3246,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=\/wp\/v2\/posts\/459\/revisions\/3246"}],"wp:attachment":[{"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=459"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=459"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1bed.allright.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=459"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}