YMatrix 文档
快速上手
SQL参考
- ABORT
- ALTER_DATABASE
- ALTER_EXTENSION
- ALTER_EXTERNAL_TABLE
- ALTER_FOREIGN_DATA_WRAPPER
- ALTER_FOREIGN_TABLE
- ALTER_FUNCTION
- ALTER_INDEX
- ALTER_RESOURCE_GROUP
- ALTER_RESOURCE_QUEUE
- ALTER_ROLE
- ALTER_RULE
- ALTER_SCHEMA
- ALTER_SEQUENCE
- ALTER_SERVER
- ALTER_TABLE
- ALTER_TABLESPACE
- ALTER_TYPE
- ALTER_USER_MAPPING
- ALTER_VIEW
- ANALYZE
- BEGIN
- CHECKPOINT
- COMMIT
- COPY
- CREATE_DATABASE
- CREATE_EXTENSION
- CREATE_EXTERNAL_TABLE
- CREATE_FOREIGN_DATA_WRAPPER
- CREATE_FOREIGN_TABLE
- CREATE_FUNCTION
- CREATE_INDEX
- CREATE_RESOURCE_GROUP
- CREATE_RESOURCE_QUEUE
- CREATE_ROLE
- CREATE_RULE
- CREATE_SCHEMA
- CREATE_SEQUENCE
- CREATE_SERVER
- CREATE_TABLE
- CREATE_TABLE_AS
- CREATE_TABLESPACE
- CREATE_TYPE
- CREATE_USER_MAPPING
- CREATE_VIEW
- DELETE
- DROP_DATABASE
- DROP_EXTENSION
- DROP_EXTERNAL_TABLE
- DROP_FOREIGN_DATA_WRAPPER
- DROP_FOREIGN_TABLE
- DROP_FUNCTION
- DROP_INDEX
- DROP_RESOURCE_GROUP
- DROP_RESOURCE_QUEUE
- DROP_ROLE
- DROP_RULE
- DROP_SCHEMA
- DROP_SEQUENCE
- DROP_SERVER
- DROP_TABLE
- DROP_TABLESPACE
- DROP_TYPE
- DROP_USER_MAPPING
- DROP_VIEW
- END
- EXPLAIN
- GRANT
- INSERT
- LOAD
- LOCK
- REINDEX
- RELEASE_SAVEPOINT
- RESET
- REVOKE
- ROLLBACK_TO_SAVEPOINT
- ROLLBACK
- SAVEPOINT
- SELECT INTO
- SET ROLE
- SET TRANSACTION
- SET
- SHOW
- START TRANSACTION
- TRUNCATE
- UPDATE
- VACUUM
工具指南
空间数据类型
普通数据库拥有字符串(string)、数值(number)和日期(date)这些数据类型,空间数据库添加了额外的数据类型(空间数据类型)以用于表达地理特征(geographic features)。这些空间数据类型抽象并封装了诸如边界(boundary)和维度(dimension)等空间结构。
MatrixDB使用PostGIS扩展来支持空间数据类型。
注意:该特性仅在企业版中提供。
1. 安装PostGIS
在每个节点上安装rpm包(postgis依赖geos39,所以先安装依赖):
sudo yum install https://ftp.postgresql.org/pub/repos/yum/common/redhat/rhel-7-x86_64/geos39-3.9.1-1.rhel7.x86_64.rpm
sudo yum install mxdb-postgis-2.5-1.el7.x86_64.rpm
连接数据库并创建扩展:
CREATE EXTENSION postgis;
2. PostGIS的使用
2.1 PostGIS常用空间数据类型
PostGIS支持的常用数据类型包括:
- POINT:点
- LINESTRING:线段
- POLYGON:多边形
2.1.1 建表
创建包括空间数据类型的表:
-- 点表
CREATE TABLE global_points (
id INT,
name VARCHAR(64),
location GEOGRAPHY(POINT)
)
Distributed by(id);
-- 面表
CREATE TABLE global_areas (
id INT,
name VARCHAR(64),
area GEOGRAPHY(POLYGON)
)
Distributed by(id);
2.1.2 插入数据
插入点数据:
INSERT INTO global_points VALUES(1, 'point1', 'POINT(-110 29)');
INSERT INTO global_points VALUES(2, 'point2', 'POINT(-111 30)');
插入面数据:
INSERT INTO global_areas VALUES(1, 'area1', 'POLYGON((-100 25, -100 30, -120 30, -120 25, -100 25))');
INSERT INTO global_areas VALUES(2, 'area2', 'POLYGON((-90 25, -90 30, -100 30, -100 25, -90 25))');
2.1.3 常用空间计算函数
计算点坐标距离:
SELECT name, ST_Distance('POINT(-110 30)'::geography, location) FROM global_points;
name | st_distance
--------+------------------
point1 | 110844.074057415
point2 | 96485.974080553
(2 rows)
计算面积:
SELECT id,name,ST_Area(area) FROM global_areas ;
id | name | st_area
----+-------+--------------------
1 | area1 | 1098617743576.8588
2 | area2 | 547807626660.82526
(2 rows)
判断点是否在面内:
SELECT a.name, p.name FROM global_points AS p, global_areas AS a WHERE ST_Intersects (a.area, p.location);
name | name
-------+--------
area1 | point1
area1 | point2
(2 rows)
计算面的交集:
SELECT ST_AsText(ST_Intersection(a1.area, a2.area)) FROM global_areas as a1, global_areas as a2 WHERE a1.name = 'area1' AND a2.name = 'area2';
st_astext
---------------------------------------------------------
LINESTRING(-100 24.9999999999992,-100 29.9999999999995)
(1 row)
2.2 创建空间索引
空间数据需要建立空间索引,才能使空间计算更加高效。PostGIS支持的空间索引包括:
- GIST:通用查询树
- BRIN:块范围索引
- SP-GiST:空间分区通用查询树
为location
字段建立索引:
CREATE INDEX global_points_gix ON global_points USING GIST ( location );
为area
字段建立索引:
CREATE INDEX global_areas_gix ON global_areas USING GIST ( area );