<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WoodyVillage &#187; PostgreSQL</title>
	<atom:link href="http://www.woody-village.com/archives/category/postgresql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.woody-village.com</link>
	<description></description>
	<lastBuildDate>Sat, 14 Aug 2010 04:30:43 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PostgreSQLで位置情報 その2(インデックスを張る)</title>
		<link>http://www.woody-village.com/archives/437</link>
		<comments>http://www.woody-village.com/archives/437#comments</comments>
		<pubDate>Fri, 23 Apr 2010 12:58:09 +0000</pubDate>
		<dc:creator>premier</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.woody-village.com/?p=437</guid>
		<description><![CDATA[とりあえず、こんな感じでSQLを作って、テストデータを準備。 Bitmap Heap Scan on location_data (cost=5.07..267.95 rows=100 width=20) (actual time=8.917..8.920 rows=1 loops=1) Filter: (circle(location, 0::double precision) @ ''::circle) -> Bitmap Index Scan on idxloc (cost=0.00..5.04 rows=100 width=0) (actual time=8.861..8.861 rows=1 loops=1) Index Cond: (circle(location, 0::double precision) @ ''::circle) Total runtime: 9.086 ms (8 rows) 9.008ms。 インデックスを落としてもう一度。 # drop index idxloc; DROP INDEX # explain [...]]]></description>
			<content:encoded><![CDATA[<p>
とりあえず、こんな感じでSQLを作って、テストデータを準備。<br />
<pre>
<？php
for($i=1; $i<=100000; $i++){
        $sql = "INSERT INTO location_data VALUES(" . $i . ", point(" . rand(10000000, 999999999)/1000000 . ", " . rand(10000000, 999999999)/1000000 . "))";
}
？>
</pre>
<br />
<br />
インデックスを張れるのは『box型』『circle型』『polygon型』の3種類で、『point型』には張れないらしい。<br />
ただ、PostgreSQLには『<a href="http://www.postgresql.jp/document/8.4/html/indexes-expressional.html">式に対するインデックス</a>』というものがあって、例えばcircle型の場合、「点は半径ゼロの円」という考え方をして、このようにインデックスを張る。<br />
<pre>
# create index idxloc on location_data using gist(circle(location, 0));
CREATE INDEX
</pre>
<br />
<br />
実際に検索する場合はこのようにする。<br />
<pre>
# select * from location_data
 where circle(location, 0) @ circle(point(35.173161, 136.906845),32.435939)
 order by location <-> point(35.173161, 136.906845);
  id   |        location        
-------+------------------------
 69043 | (35.091225,140.067053)
 95577 | (31.701982,137.488926)
...
 85878 | (12.450013,160.012371)
(343 rows)
</pre>
<br />
<br />
EXPLAINで検索スピードを確認。<br />
<pre>
# explain analyze select * from location_data
 where circle(location, 0) @ circle(point(35.173161, 136.906845),3.243594)
 order by location <-> point(35.173161, 136.906845);
                                                        QUERY PLAN                                                        
--------------------------------------------------------------------------------------------------------------------------
 Sort  (cost=271.28..271.53 rows=100 width=20) (actual time=8.954..8.956 rows=1 loops=1)
   Sort Key: ((location <-> '(35.173161,136.906845)'::point))
   Sort Method:  quicksort  Memory: 17kB
   ->  Bitmap Heap Scan on location_data  (cost=5.07..267.95 rows=100 width=20) (actual time=8.917..8.920 rows=1 loops=1)
         Filter: (circle(location, 0::double precision) @ '<(35.173161,136.906845),3.243594>'::circle)
         ->  Bitmap Index Scan on idxloc  (cost=0.00..5.04 rows=100 width=0) (actual time=8.861..8.861 rows=1 loops=1)
               Index Cond: (circle(location, 0::double precision) @ '<(35.173161,136.906845),3.243594>'::circle)
 Total runtime: 9.086 ms
(8 rows)
</pre>
<br />
<br />
9.008ms。<br />
<br />
インデックスを落としてもう一度。<br />
<pre>
# drop index idxloc;
DROP INDEX

# explain analyze select * from location_data
 where circle(location, 0) @ circle(point(35.173161, 136.906845),3.243594)
 order by location <-> point(35.173161, 136.906845);
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Sort  (cost=2092.57..2092.82 rows=100 width=20) (actual time=57.908..57.910 rows=1 loops=1)
   Sort Key: ((location <-> '(35.173161,136.906845)'::point))
   Sort Method:  quicksort  Memory: 17kB
   ->  Seq Scan on location_data  (cost=0.00..2089.25 rows=100 width=20) (actual time=39.362..57.864 rows=1 loops=1)
         Filter: (circle(location, 0::double precision) @ '<(35.173161,136.906845),3.243594>'::circle)
 Total runtime: 58.133 ms
(6 rows)
</pre>
<br />
<br />
今度は58.133ms。よさそうですね。<br />
<br />
<br />
次回は、ある地点からn（キロ）メートル内の検索を。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.woody-village.com/archives/437/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PostgreSQLで位置情報</title>
		<link>http://www.woody-village.com/archives/434</link>
		<comments>http://www.woody-village.com/archives/434#comments</comments>
		<pubDate>Wed, 21 Apr 2010 14:50:44 +0000</pubDate>
		<dc:creator>premier</dc:creator>
				<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://www.woody-village.com/?p=434</guid>
		<description><![CDATA[PostGISがメジャー（なのか？）だけど、使わないやり方。 PostgreSQLのデータ型に、平面における座標点を扱う『point型』というのがある。 [使用例] テーブル作成 ===== # create table location_data (id integer, location point); ===== データ挿入 x座標が100、y座標が200の場合 ===== # insert into location_data (id, location) values(1, point(100, 200)); ===== 抽出すると ===== # select * from location_data; id &#124; location ----+----------- 1 &#124; (100,200) 2 &#124; (150,100) 3 &#124; (200,200) (3 rows) ===== x座標、y座標を別々に取り出すには ===== # select location[0] [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://postgis.refractions.net/">PostGIS</a>がメジャー（なのか？）だけど、使わないやり方。<br />
<br />
<br />
PostgreSQLのデータ型に、平面における座標点を扱う『point型』というのがある。<br />
<br />
<br />
<br />
<br />
[使用例]<br />
<br />
テーブル作成<br />
<pre>
=====
# create table location_data (id integer, location point);
=====
</pre>
<br />
<br />
データ挿入<br />
x座標が100、y座標が200の場合<br />
<pre>
=====
# insert into location_data (id, location) values(1, point(100, 200));
=====
</pre>
<br />
<br />
抽出すると<br />
<pre>
=====
# select * from location_data;
 id | location  
----+-----------
  1 | (100,200)
  2 | (150,100)
  3 | (200,200)
(3 rows)
=====
</pre>
<br />
<br />
x座標、y座標を別々に取り出すには<br />
<pre>
=====
# select location[0] as x, location[1] as y from location_data;
  x  |  y  
-----+-----
 100 | 200
 150 | 100
 200 | 200
(3 rows)
=====
</pre>
<br />
<br />
絞り込み その1<br />
2つのpointを対角とした長方形に含まれる範囲<br />
<pre>
=====
# select * from location_data \
where location @ box(point(150, 100),point(200,300));
 id | location  
----+-----------
  2 | (150,100)
  3 | (200,200)
(2 rows)
=====
</pre>
<br />
<br />
絞り込み その2<br />
あるpointを中心とする半径rの円に含まれる範囲<br />
<pre>
=====
# select * from location_data where location @ circle(point(200, 200),100);
 id | location  
----+-----------
  1 | (100,200)
  3 | (200,200)
(2 rows)
=====
</pre>
<br />
<br />
ソート<br />
<pre>
=====
# select * from location_data 
where location @ circle(point(200, 200),100) order by location <-> point(200,200);
 id | location  
----+-----------
  3 | (200,200)
  1 | (100,200)
(2 rows)
=====
</pre>
<br />
<br />
<br />
<br />
とりあえず、ざっとこんな感じ。<br />
<br />
<br />
インデックスの張り方は次の機会に。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.woody-village.com/archives/434/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

