天天看點

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

postgresql , postgis , 地理位置 , knn , 近鄰檢索 , 網格檢索 , polygon中心點 , 半徑搜尋

偉大的馬老師說:

“純電商時代很快會結束,未來的十年、二十年,沒有電子商務這一說,隻有新零售這一說,也就是說線上線下和物流必須結合在一起,才能誕生真正的新零售”

線上是指雲平台,線下是指銷售門店或生産商,新物流消滅庫存,減少囤貨量。

電子商務平台消失是指,現有的電商平台分散,每個人都有自己的電商平台,不再入駐天貓、京東、亞馬遜大型電子商務平台。舉例:每個人在電商平台都有自己的店鋪,集中在平台下進行銷售,隻能在一塊水池裡生活,這是很局限性的。

要打通線上線下、消滅庫存,需要發揮資料的價值,比如通過資料預測銷量。同時線上線下對接,也對資料營運産生了新的挑戰,比如基于地理位置的網格化營運由來而生。

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

1、支援基于地理位置(gis)的快速資料檢索。

2、支援海量銷售資料的分析、挖掘。

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

1、海量的銷量資料通過oss并行進入到阿裡雲hybriddb for postgresql資料庫。

2、rds postgresql負責線上事務處理,網格化營運的任意多邊形圈選商戶。

3、etl程式,負責資料排程。

4、bi應用對接hdb for pg和pg,驅動和文法與postgresql相容。

5、hybriddb for postgresql提供高可用、備份的基本功能,同時提供了一鍵擴容的功能。使用者不需要擔心未來資料增長的性能壓力。

6、hdb pg和rds pg可以通過oss_ext外部表插件,透明通路(讀寫)oss的資料。oss提供海量共享存儲,rds pg和hdb pg之間通過oss可共享資料,同時oss還可以作為外部海量資料來源并行導入到hdb pg的高速通道。oss還可以作為rds pg和hdb pg的冷資料存儲。

用到内置的polygon, box, circle, point類型,gist空間索引,<->近鄰排序操作符,@>操作符。

1、構造1億商戶地理位置資料

2、建立空間索引

3、建立查詢優化函數

輸入任意多邊形,傳回落在多邊形中的商戶。

如果需要帶其他條件的空間查詢,可以使用空間複合分區索引(partial index),例如

詳見

<a href="https://github.com/digoal/blog/blob/master/201707/20170721_01.md">《分區索引的應用和實踐 - 阿裡雲rds postgresql最佳實踐》</a>

4、空間索引性能驗證,一億資料網格查詢約 0.8 毫秒。

實際生産上存儲的是經緯度,用得更多的是postgis空間資料庫。前面使用内置幾何類型是為了測試友善。

我們需要用到postgis的 商戶網格搜尋 的函數有兩個

<a href="http://postgis.net/docs/manual-2.3/st_within.html">http://postgis.net/docs/manual-2.3/st_within.html</a>

1、st_within

st_within — returns true if the geometry a is completely inside geometry b

boolean st_within(geometry a, geometry b);

returns true if geometry a is completely inside geometry b. for this function to make sense, the source geometries must both be of the same coordinate projection, having the same srid. it is a given that if st_within(a,b) is true and st_within(b,a) is true, then the two geometries are considered spatially equal.

this function call will automatically include a bounding box comparison that will make use of any indexes that are available on the geometries. to avoid index use, use the function _st_within.

2、st_contains

st_contains — returns true if and only if no points of b lie in the exterior of a, and at least one point of the interior of b lies in the interior of a.

boolean st_contains(geometry geoma, geometry geomb);

returns true if geometry b is completely inside geometry a. for this function to make sense, the source geometries must both be of the same coordinate projection, having the same srid. st_contains is the inverse of st_within. so st_contains(a,b) implies st_within(b,a) except in the case of invalid geometries where the result is always false regardless or not defined.

this function call will automatically include a bounding box comparison that will make use of any indexes that are available on the geometries. to avoid index use, use the function _st_contains.

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐
(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

同時還需要用到gist空間索引,可能用到&lt;-&gt;knn排序操作符,外切圓,圓心等函數,資料構造函數st_pointfromtext等。詳見postgis手冊

<a href="http://postgis.net/docs/manual-2.3/reference.html">http://postgis.net/docs/manual-2.3/reference.html</a>

1、建表、建立空間索引

2、構造1億測試資料

3、商戶網格搜尋 查詢

執行計劃,使用了空間索引,同時包含了部分過濾(做法應該和我後面提到的類似,外切圓,按距離輸出,過濾不在polygon内的點)。

4、查詢分解

4.1 求包含polygon的最小圓(實際上是個多段polygon)

4.2 求包含polygon的最小圓的圓心

4.3 求包含polygon的最小圓的圓心、半徑(postgis 2.3引入的功能)

2.3以前的版本可以這樣來求半徑,分解步驟如下

4.4 距離排序,截止半徑大小,同時過濾不在polygon内的點

gpdb暫時不支援gist索引的knn sort,以及knn merge sort。

是以我們看到多了一個外排的節點。

不過沒關系隻要輸出的結果不多,排序不是瓶頸。因為st_within還是能用上空間索引的。

資料分析能力如何呢?

這裡有一組單機1tb的tpc-h測試資料,hybriddb for postgresql是mpp分布式資料庫,可以通過增加節點線性提升性能。

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

另外還有一些測試資料可以參考如下:

<a href="https://github.com/digoal/blog/blob/master/201707/20170714_01.md">《tpc-h測試 - postgresql 10 vs deepgreen(greenplum)》</a>

<a href="https://github.com/digoal/blog/blob/master/201707/20170703_01.md">《100tb級, 日增量1tb(100億)的oltp olap混合場景資料庫設計方向》</a>

1、空間索引,gist索引是postgresql獨有的空間索引,支援精準的距離索引搜尋,同時支援按舉例遠近排序傳回結果。性能杠杠的,也是很多科研機構、空間業務的首選。

2、knn查詢,按距離由近到遠輸出記錄。

3、oss外部表,阿裡雲rds pg和hdb pg增加的功能,與雲端海量對象存儲oss打通,在資料庫中以外部表的形式透明的讀寫oss中的檔案。可以達到每個線程約30mb/s的讀寫帶寬,增加并發即可提高整體的吞吐。

4、etl,雲端或使用者的etl程式,隻要支援oss對象連接配接、pg的連接配接協定即可。

5、madlib,是一個開源的機器學習庫,支援大多數的學習庫,通過rds pg,hdb pg的sql接口實作機器學習。

madlib支援classification, regression, clustering, topic modeling, association rule mining, descriptive statistics, validation等衆多挖掘模型。

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

<a href="http://madlib.incubator.apache.org/product.html">http://madlib.incubator.apache.org/product.html</a>

<a href="http://madlib.incubator.apache.org/docs/latest/index.html">madlib手冊</a>

6、幾何知識

多邊形的内切圓,circle(polygon)

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

多邊形box和外圓,circle(box(polygon))

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

pg的幾何函數如下

<a href="https://www.postgresql.org/docs/9.6/static/functions-geometry.html">https://www.postgresql.org/docs/9.6/static/functions-geometry.html</a>

postgis的幾何函數如下

7、以上性能測試涉及到的多邊形搜尋是pg 10的測試,如果你發現老版本存在空間索引的性能問題,可以用以下這個方法進行優化。

首先将多邊形轉換為box,再求box的外圓,通過knn索引順序傳回記錄,同時過濾多邊形包含的資料。

<a href="https://www.aliyun.com/product/rds/postgresql">阿裡雲 rds postgresql</a>

<a href="https://www.aliyun.com/product/gpdb">阿裡雲 hybriddb for postgresql</a>

<a href="https://www.aliyun.com/product/oss">阿裡雲 oss</a>

<a href="https://github.com/digoal/blog/blob/master/201701/20170113_01.md">《(ar虛拟現實)紅包 技術思考 - gis與圖像識别的完美結合》</a>

<a href="https://github.com/digoal/blog/blob/master/201612/20161231_01.md">《從難纏的模糊查詢聊開 - postgresql獨門絕招之一 gin , gist , sp-gist , rum 索引原理與技術背景》</a>

<a href="https://github.com/digoal/blog/blob/master/201707/20170722_01.md">《時間、空間、對象多元屬性 海量資料任意多元 高效檢索 - 阿裡雲rds postgresql最佳實踐》</a>

<a href="https://github.com/digoal/blog/blob/master/201706/20170620_01.md">《空間複合索引加速空間搜尋》</a>

<a href="https://github.com/digoal/blog/blob/master/201704/20170413_02.md">《奔跑吧,大屏 - 時間+空間 實時四維資料透視》</a>

<a href="https://github.com/digoal/blog/blob/master/201703/20170328_04.md">《視覺挖掘與postgis空間資料庫的完美邂逅 - 廣告營銷\圈人》</a>

<a href="https://github.com/digoal/blog/blob/master/201706/20170629_01.md">《postgresql\gpdb 毫秒級海量時空資料透視 典型案例分享》</a>

新零售行業,通過打通線上線下、消滅庫存,需要發揮資料的價值,比如通過資料預測銷量。同時線上線下對接,也對資料營運産生了新的挑戰,比如基于地理位置的網格化營運由來而生。

要求資料庫具備:

1、支援基于地理位置(gis)的快速資料檢索的能力。

2、支援海量銷售資料的分析、挖掘的能力。

(新零售)商戶網格化營運 - 阿裡雲RDS PostgreSQL最佳實踐

通過阿裡雲的rds postgresql、hybriddb for postgresql、oss,實作了億級地理位置資料一毫秒内響應,同時支援分析、挖掘需求的全鍊路需求。

<a href="https://github.com/digoal/blog/blob/master/201308/20130806_01.md">《gis附近查找性能優化 - postgis long lat geometry distance search tuning using gist knn function》</a>