From 24a40fa5507fec26d8101babdd5a97f74aa1baac Mon Sep 17 00:00:00 2001 From: Vonng Date: Fri, 9 Feb 2018 20:09:21 +0800 Subject: [PATCH] add gis intro --- README.md | 11 +- ddia/part-i.md | 4 +- gis/README.md | 45 ++++++++ gis/img/gis-type.png | Bin 0 -> 2333 bytes gis/intro.md | 241 +++++++++++++++++++++++++++++++++++++++++++ storage/ssh-intro.md | 160 ++++++++++++++++++++++++++++ 6 files changed, 458 insertions(+), 3 deletions(-) create mode 100644 gis/README.md create mode 100644 gis/img/gis-type.png create mode 100644 gis/intro.md create mode 100644 storage/ssh-intro.md diff --git a/README.md b/README.md index cbc40a7..3fd6b12 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,12 @@ +## Storage + +* SSD Internal + + + ## [PostgreSQL](pg/) PostgreSQL是世界上最先进的开源关系型数据库! @@ -44,7 +50,10 @@ PostgreSQL是世界上最先进的开源关系型数据库! ### GIS -* PostGIS教程 +* [PostGIS入门](gis/intro.md) 【EMPTY】 + + +* [PostGIS教程-翻译计划](gis/README.md) ### Adminstration diff --git a/ddia/part-i.md b/ddia/part-i.md index dc19128..2c0504d 100644 --- a/ddia/part-i.md +++ b/ddia/part-i.md @@ -5,9 +5,9 @@ 1. [第一章](ch1.md)将介绍本书使用的术语和方法。**可靠性,可扩展性和可维护性** ,这些词汇到底意味着什么?以及如何实现这些目标。 2. [第二章](ch2.md)将对几种不同的**数据模型和查询语言**进行比较。从程序员的角度看,这是数据库之间最明显的区别。不同的数据模型适用于不同的应用场景。 3. [第三章](ch3.md)将深**存储引擎**内部,并研究数据库如何在磁盘上摆放数据。不同的存储引擎针对不同的负载进行优化,选择合适的存储引擎对系统性能有巨大影响。 -4. [第四章](ch4)将对几种不同的**数据编码(序列化)**进行比较。特别讨论了在应用需求经常变化、模式需要随时间调整的环境中这些格式的适用情况。 +4. [第四章](ch4)将对几种不同的 **数据编码(序列化)**进行比较。特别讨论了在应用需求经常变化、模式需要随时间调整的环境中这些格式的适用情况。 -第二部分将专门讨论在**分布式数据系统**中才有的特殊问题。 +第二部分将专门讨论在**分布式数据系统**中才有的问题。 diff --git a/gis/README.md b/gis/README.md new file mode 100644 index 0000000..a013398 --- /dev/null +++ b/gis/README.md @@ -0,0 +1,45 @@ +# PostGIS教程 + +翻译完DDIA再来看看有没有空搞这个吧。 + + + +## Workshop Modules + +- [1. Welcome](http://workshops.boundlessgeo.com/postgis-intro/welcome.html) +- [2. Introduction](http://workshops.boundlessgeo.com/postgis-intro/introduction.html) +- [3. Installation](http://workshops.boundlessgeo.com/postgis-intro/installation.html) +- [4. Creating a Spatial Database](http://workshops.boundlessgeo.com/postgis-intro/creating_db.html) +- [5. Loading spatial data](http://workshops.boundlessgeo.com/postgis-intro/loading_data.html) +- [6. About our data](http://workshops.boundlessgeo.com/postgis-intro/about_data.html) +- [7. Simple SQL](http://workshops.boundlessgeo.com/postgis-intro/simple_sql.html) +- [8. Simple SQL Exercises](http://workshops.boundlessgeo.com/postgis-intro/simple_sql_exercises.html) +- [9. Geometries](http://workshops.boundlessgeo.com/postgis-intro/geometries.html) +- [10. Geometry Exercises](http://workshops.boundlessgeo.com/postgis-intro/geometries_exercises.html) +- [11. Spatial Relationships](http://workshops.boundlessgeo.com/postgis-intro/spatial_relationships.html) +- [12. Spatial Relationships Exercises](http://workshops.boundlessgeo.com/postgis-intro/spatial_relationships_exercises.html) +- [13. Spatial Joins](http://workshops.boundlessgeo.com/postgis-intro/joins.html) +- [14. Spatial Joins Exercises](http://workshops.boundlessgeo.com/postgis-intro/joins_exercises.html) +- [15. Spatial Indexing](http://workshops.boundlessgeo.com/postgis-intro/indexing.html) +- [16. Projecting Data](http://workshops.boundlessgeo.com/postgis-intro/projection.html) +- [17. Projection Exercises](http://workshops.boundlessgeo.com/postgis-intro/projection_exercises.html) +- [18. Geography](http://workshops.boundlessgeo.com/postgis-intro/geography.html) +- [19. Geometry Constructing Functions](http://workshops.boundlessgeo.com/postgis-intro/geometry_returning.html) +- [20. More Spatial Joins](http://workshops.boundlessgeo.com/postgis-intro/joins_advanced.html) +- [21. Validity](http://workshops.boundlessgeo.com/postgis-intro/validity.html) +- [22. Equality](http://workshops.boundlessgeo.com/postgis-intro/equality.html) +- [23. Linear Referencing](http://workshops.boundlessgeo.com/postgis-intro/linear_referencing.html) +- [24. Dimensionally Extended 9-Intersection Model](http://workshops.boundlessgeo.com/postgis-intro/de9im.html) +- [25. Clustering on Indices](http://workshops.boundlessgeo.com/postgis-intro/clusterindex.html) +- [26. 3-D](http://workshops.boundlessgeo.com/postgis-intro/3d.html) +- [27. Nearest-Neighbour Searching](http://workshops.boundlessgeo.com/postgis-intro/knn.html) +- [28. Tracking Edit History using Triggers](http://workshops.boundlessgeo.com/postgis-intro/history_tracking.html) +- [29. Advanced Geometry Constructions](http://workshops.boundlessgeo.com/postgis-intro/advanced_geometry_construction.html) +- [30. Tuning PostgreSQL for Spatial](http://workshops.boundlessgeo.com/postgis-intro/tuning.html) +- [31. PostgreSQL Security](http://workshops.boundlessgeo.com/postgis-intro/security.html) +- [32. PostgreSQL Schemas](http://workshops.boundlessgeo.com/postgis-intro/schemas.html) +- [33. PostgreSQL Backup and Restore](http://workshops.boundlessgeo.com/postgis-intro/backup.html) +- [34. Software Upgrades](http://workshops.boundlessgeo.com/postgis-intro/upgrades.html) +- [35. Appendix A: PostGIS Functions](http://workshops.boundlessgeo.com/postgis-intro/postgis-functions.html) +- [36. Appendix B: Glossary](http://workshops.boundlessgeo.com/postgis-intro/glossary.html) +- [37. Appendix C: License](http://workshops.boundlessgeo.com/postgis-intro/license.html) \ No newline at end of file diff --git a/gis/img/gis-type.png b/gis/img/gis-type.png new file mode 100644 index 0000000000000000000000000000000000000000..c5868c05dd13d0289c6199716e191141e8cffc3f GIT binary patch literal 2333 zcmc(hdo7GR!cI%+QP&hH{xsB=ahXRD^k{kmD9J z6Ncg7xTL&|86y@Wmo#}@LWmSan)9o5);Vjv=lpm6IDhQ@eD~hp^*n3uwLfd`bZ^g7 zN(%cG002-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I*?Nj;0I?Qq4Iysa|o!F3X=h`l?H7ra5W$3-1SHuBwFo{NgPsZqM@Gx;B%AL zC5Z`p{$2WKO*hAi{X|tgi30Nfys(Uz5k1@VwHEj5%x~hXi!u5Jx@(3qJqAvaWF~Pc~ZG>S+BfHCE9O68Ws6>}%g7xp(-A?{Rx&W~KH$ zVdJ))teQ_cdVIx-?@NMkqNiX}!h5)-W6FGFz@>`C3Qfw}h;BVBX$gOjK_v2(5LlN9 z69La;!h|yA4r5g9n;m*QMD?Jmc^ovg?$dndaf4c2m*2=adj(eM7^eCJTJfmE7VjH}4}h8567ndA7Bw57;jX!?j6lzn z_H)cQ7CrAW#S258$g^I7r)t&Z6|^?DyfBOP^B~2)ZZCS5G2(M9I|Z^hu+25x*yYFg(AYg z8evm4WeOv`8}NIZk~2J0>X^_COoS*%ZcTfd#@sb#$wtkvp#kg}gM99SN+M&99g4L3 z8*{Z|Xc-wkmOWP5X8qkgRzwaO7=xXt>Vk2;1{hJ~P|dfX$>o?NNI1CW2r>1c>${>c^qpimf)>L}F+)mA1 zVjUBqPG60V?`?m5zm?OJQRELFJ-LqQNpihdJ5*A_%6ZF+OSoH~@*1)7D2ZcjnXGEZ zA@&gOv0J3SKW>(!qyKEoPzb=3*D|35Y+e_;ng+gS{Ax)jcJ()&4?L}17Olu~Hd=9r XlpEHPgF~jkzYKtI@pOLTL`eAu&dW5q literal 0 HcmV?d00001 diff --git a/gis/intro.md b/gis/intro.md new file mode 100644 index 0000000..3cfd24e --- /dev/null +++ b/gis/intro.md @@ -0,0 +1,241 @@ +# PostGIS简明教程 + +PostGIS是PostgreSQL强大扩展能力的最佳示例,它已经成为GIS行业的事实标准,值得用几本书去专门讲。但这里不妨先管中窥豹一下。 + +## 1. 安装与配置 + +安装与配置并不是PostGIS的学习重点,然而它确实是许多新人入门的最大拦路虎。 + +建议直接使用现成的二进制包,发行版来安装PostGIS,而不是手工编译,这会轻松很多。 + +在Mac上可以通过homebrew一键安装PostGIS + +```bash +/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" +``` + +在CentOS上可以通过yum安装,在Ubuntu可以通过apt-get安装,不再赘述 + +连接PostgreSQL并执行以下查询,确认PostGIS扩展已经正确地安装,可以被数据库识别: + +```bash +vonng=# SELECT name,default_version FROM pg_available_extensions WHERE name ~ 'gis'; + name | default_version +------------------------+----------------- + postgis | 2.4.3 + postgis_tiger_geocoder | 2.4.3 + postgis_topology | 2.4.3 + btree_gist | 1.5 + postgis_sfcgal | 2.4.3 +``` + + + +## 2. 创建GIS数据库 + +PostGIS是PostgreSQL的一个扩展,连接并执行以下命令,可在当前数据库中加载PostGIS插件。 + +```sql +CREATE EXTENSION postgis; +CREATE EXTENSION postgis_topology; +CREATE EXTENSION postgis_sfcgal; +CREATE EXTENSION fuzzystrmatch; +CREATE EXTENSION address_standardizer; +CREATE EXTENSION address_standardizer_data_us; +CREATE EXTENSION postgis_tiger_geocoder; +``` + +执行完毕后,执行`postgis_full_version`查看当前PostGIS版本。 + +```sql +gis=# SELECT postgis_full_version(); + +POSTGIS="2.4.3 r16312" PGSQL="100" GEOS="3.6.2-CAPI-1.10.2 4d2925d6" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 1.11.5, released 2016/07/01" LIBXML="2.9.7" LIBJSON="0.12.1" RASTER +``` + +现在GIS数据库已经准备好了。让我们进入主题吧。 + +## 3. 几何对象 + +PostGIS支持很多几何类型:点,线,多边形,复合几何体等,并提供了大量实用的相关函数。 + +注意,虽然PostGIS中的几何类型与PostgreSQL内建的几何类型非常像,但它们并不是一回事。所有PostGIS中的对象命名通常都以`ST`开头,是空间类型(Spatial Type)的缩写。 + +对于PostGIS而言,所有几何对象都有一个公共父类`Geometry`,这种面向对象的组织形式允许在数据库中进行一些灵活的操作:例如在数据表中的同一列中存储不同的几何对象。 + +每种几何对象实际上都是PostGIS底层C++几何库geos中对象包装,这些几何类型按照面向对象的继承关系组成了一颗树: + +![](img/gis-type.png) + + + +### 几何对象的创建 + +几何对象可以通过PostGIS内建的函数进行创建,例如: + +```sql +>>> SELECT ST_Point(1.0, 2.0); +0101000000000000000000F03F0000000000000040 +``` + +注意,当查询原始集合类型时,PostgreSQL会返回几何对象的二进制数据的十六进制表示。这允许各类ETL工具以同样的方式高效处理PostGIS类型,但二进制表示对人类很不友好,可以通过`ST_AsText`获取人类可读的格式。 + +```sql +>>> SELECT ST_AsText(ST_Point(1.0, 2.0)); +POINT(1 2) +``` + +当然,如同PostgreSQL内建的类型一样,PostGIS类型也可以使用字面值的方式创建。 + +```SQL +CREATE TABLE geom ( + geom GEOMETRY +); + +INSERT INTO geom VALUES +('Point(1 2)'), +('LineString(0 0,1 1,2 1,2 3)'), +('Polygon((0 0, 1 0, 1 1,0 1,0 0))'), +('MultiPoint(1 2,3 4)'); +``` + +通常在使用PostGIS中,几何类型使用统一的`Geometry`类型。如果需要判断具体的几何类型,则可以使用`ST_GeometryType`。 + +```sql +geo=# SELECT ST_GeometryType(geom), ST_AsText(geom) FROM geom; + + st_geometrytype | st_astext +-----------------+-------------------------------- + ST_Point | POINT(1 2) + ST_LineString | LINESTRING(0 0,1 1,2 1,2 3) + ST_Polygon | POLYGON((0 0,1 0,1 1,0 1,0 0)) + ST_MultiPoint | MULTIPOINT(1 2,3 4) +``` + +### 点 + +让我们从最简单的**点(Point)**开始。PostGIS的点默认是二维空间中的点,具有两个`double`类型的分量`x,y`。使用`ST_X, ST_Y`可以从点中取出对应的坐标分量 + +```sql +geo=# SELECT ST_X(geom), ST_Y(geom) FROM geom WHERE ST_GeometryType(geom) = 'ST_Point'; + st_x | st_y +------+------ + 1 | 2 +``` + +在介绍更多几何类型前, + + + +## 4. Play with Point + +单纯使用PostGIS的Point,就已经可以实现许多有趣的功能了。 + +### 计算两点距离 + +```sql +geo=# SELECT ST_Point(1,1) <-> ST_Point(2,2); +1.4142135623730951 +``` + +运算符`<->`可以计算左右两侧两点之间的距离。 + +### 应用:查找最近的餐馆 + +现在我们有一张包含全国所有餐馆的表,有五千万条记录: + +```sql +CREATE TABLE poi( + id BIGSERIAL, + name TEXT, + position GEOMETRY +) +``` + +如果我现在在国贸`(116.458855, 39.909863)`,想要找到距离这里最近的10家餐厅。应该如何查询呢? + +```sql +SELECT name FROM poi +ORDER BY position <-> ST_Point(116.458855, 39.909863) LIMIT 10; +``` + +```sql + QUERY PLAN +--------------------------------------------------------------------------------------------- + Limit (cost=4610514.44..4610514.47 rows=10 width=31) + -> Sort (cost=4610514.44..4767389.77 rows=62750132 width=31) + Sort Key: (("position" <-> '0101000000CAA65CE15D1D5D40946B0A6476F44340'::geometry)) + -> Seq Scan on poi (cost=0.00..3254506.65 rows=62750132 width=31) +``` + +执行需要一次扫表,需要几分钟的时间。对于只有几千行、每天查询几十次来说,这也没什么大不了的。但对于几千万的数据量,几万的查询QPS,就需要索引了。 + +在`position`列上创建GIST索引: + +```sql +CREATE INDEX CONCURRENTLY idx_poi_position_gist ON poi USING gist(position); +``` + +然后再执行同样的查询,变为了索引扫描。 + +```sql + QUERY PLAN +------------------------------------------------------------------------------------------------------ + Limit (cost=0.42..9.73 rows=10 width=31) + -> Index Scan using idx_poi_position_gist on poi (cost=0.42..58440964.86 rows=62750132 width=31) + Order By: ("position" <-> '0101000000CAA65CE15D1D5D40946B0A6476F44340'::geometry) +``` + +结果,在0.1毫秒内就返回了结果! + +```sql +geo=# SELECT name +FROM poi +ORDER BY position <-> ST_Point(116.458855, 39.909863) +LIMIT 10; + name +------------------------------ + 苹果智元咨询北京有限公司 + 住友商社 + 国贸 + 路易·费罗(国贸店) + addidas(国贸店) + 博艺府家 + 北京尚正明远信息技术研究中心 + 北京竹露桐花商贸有限公司 + 文心雕龙 + +(10 rows) + +Time: 0.993 ms +``` + +也许需要成百上千行应用代码实现的功能,现在一行SQL就可以搞定,而且性能相当瞩目。 + + + +## 线段 + +### 表示道路 + +* 找出城市里最长的道路 + + +* 计算城市道路里程 +* 计算全国道路里程 + + + + +## 多边形 + +### 带洞的多边形 + +表示复杂的地理对象,例如:工人体育馆 + +### 地理围栏 + +例如你有用户的位置轨迹数据,现在希望研究用户经过了哪些商圈。 + + + diff --git a/storage/ssh-intro.md b/storage/ssh-intro.md new file mode 100644 index 0000000..e45261c --- /dev/null +++ b/storage/ssh-intro.md @@ -0,0 +1,160 @@ +# SSD 原理 + + + +《大话存储》 + + + +### 颗粒 + +东芝的颗粒,海力士的颗粒 + + + +### Feature + +Atomic Write: 打开开关,10%性能提升。1.2x写放大 + +Prioritize Write : 阿里定制功能 + + + +### 温度 + +正常负载:60度。 + +Warning级别告警: 70度 + +温度79度超过10秒钟,开始降速。 + + + +### 统计 + +Power Cycle可以用于判断重启次数。 + +Dynamic Bad Blocks 统计坏块数目。 + +半年时间将动态坏块,转为静态坏块。如果超过50,通知厂家。 + +一个Page 32K = 32768B,Page是最小的写入单位。 + + 一个Block 256 Page = 8M,Block是最小的擦除单位。 + +坏块最大导致30% 性能损失 + + + +### SEU问题 + +Media Status : Healthy|Bad + +SEU Flag: Normal|Correctable|Uncorrectable + + + +## 磨损与寿命 + +WL Bandwidth 磨损均衡。 + +每十五天翻新一遍旧数据。每十分钟检查一遍最旧的是否需要翻新。 + +总写入量 = 主机写入量+GC带宽 + +Estimated Life Left: 99.840% -> 15% 适合开始迁移 + + + + + +## 写放大问题 + +写放大= 主机写入量 / 总写入量 + +写放大因子,> 2 完全随机写 + +经验值2以内。 + +监控动态写放大 + +Write Amplifier: 1.100 + +统计时间窗口:1s + + + +### Lun & Channel + +最多8个并发。 + + + +Lun ,一个数据结构。 + +Channel: 并发通道 + +CRC:存储Block错误 + +ECC:控制器内存错误 + + + +32k + 1024 bit校验和。 + +每1024字节需要40字节校正。 + +掉电保护 ,只需要刷入32K 数据。 + +使用主板余电的功能。从12V掉到5V,5V可以再供100ms,5ms可以刷入磁盘 + + + +### 基本测试 + +使用fio测试 + +测试模型: + +顺序带宽:1个并发,队列深度128 + +IOPS:随机4 到 8 个并发,每个并发32~64队列深度 + +异步IO:最大化压测底层处理能力 + +DirectIO: + +全盘写两三遍,做满盘预处理。 + + + +## 高级IO开发 + +FTL:Flash Translation Layer 将传统硬盘的API翻译成闪存的API。 + +255个逻辑controller + + + +## SSD基础知识 + +LBA -> PBA + +PCI倍速 + +网卡 + + + +730 四个插槽,每个16速。、 + +一块万兆网卡x8。一块四口千兆网卡。 + +一块存储卡x8 + + + +15000 * + +块大小越大,造成的IO越高。 +