OceanBase 2.1 的ORACLE相容效能力探祕
概述
OceanBase是一款通用的分散式關係型資料庫,目前內部業務使用比較多有兩個版本:1.4和2.1。OceanBase每個版本變化總能帶給人很多驚喜,其中2.1版本實現了ORACLE很多特性的相容。本文主要簡單瀏覽一下這些新功能。
運維
賬戶管理
在ORACLE Mode的租戶裡,建立使用者依然是create user
命令,密碼不用單引號。授權方式還是還是ORACLE和MySQL的結合。檢視許可權方式是MySQL的語法(這點還是MySQL語法很方便)。
在ORACLE Mode的租戶裡,不同使用者就是不同SCHEMA
,預設彼此資料訪問許可權隔離,除非明確授權訪問。
$obclient -h11.***.84.84 -usys@tnt_oracle#obdemo -P2883 SYS -A -pobDBA2019
create user ora_user identified by 123456;
grant all privileges on ora_user.* to ora_user;
grant select on sys.* to ora_user;
show grants for ora_user;
select username, account_status, lock_date, expiry_date, created from dba_users;
例項變數檢視
OceanBase叢集級別的引數是通過檢視和修改parameters
,但是在Oracle租戶裡,租戶級別的設定修改依然是通過類似修改MySQL的變數(variables
)來的。這點跟Oracle原生例項不一樣。
show global variables where variable_name in ('autocommit','ob_compatibility_mode','ob_enable_sql_audit','ob_query_timeout','ob_read_consistency','ob_tcp_invited_nodes','ob_trx_timeout','sql_mode','tx_isolation','system_time_zone','time_zone');
修改方法舉例
set global autocommit=on;
效能分析
OceanBase實現了很多類似Oracle的AWR相關的GV$
檢視。不過大部分在業務租戶下是沒有許可權檢視的。業務租戶下只提供了一個SQL全量日誌檢視。這個也夠用了。詳細用法參見《阿里資料庫效能診斷的利器——SQL全量日誌》
select /*+ read_consistency(weak) query_timeout(1000000000) */ request_time, svr_Ip, trace_id, sid, client_ip, tenant_id,tenant_name,user_name,db_name, query_sql, affected_rows,ret_code, event, state, elapsed_time, execute_time, queue_time, decode_time, get_plan_time, block_cache_hit, bloom_filter_cache_Hit, block_index_cache_hit, disk_reads,retry_cnt,table_scan, memstore_read_row_count, ssstore_read_row_count, round(request_memory_used/1024/1024) req_mem_mbfrom gv$sql_audit where user_name in ('ora_user') and rownum<100order by request_time desc ;
開發
面向開發的功能主要就是DDL
和DML
了。
DDL
建表
OceanBase的表是索引組織表(IOT
),強烈建議設定主鍵,並且主鍵就是資料。
建表時支援的資料型別如下:
-
CHAR
/VARCHAR2
-
DATE
/TIMESTAMP
/TIMESTAMP WITH TIME ZONE
/TIMESTAMP WITH LOCAL TIME ZONE
-
INTERVAL
(部分運算) -
NUMBER
(包括其子型別int
等) -
CLOB
/BLOB
(不超過48M)
建表示例:
$obclient -h11.***.84.84 -uora_user@tnt_oracle#obdemo -P2883 -A -p123456 ORA_USER
create table t01(
id number not null,
c1 char(10),
c2 varchar2(10),
c3 DATE default sysdate,
c4 timestamp default systimestamp,
c5 timestamp with time zone default localtimestamp,
c6 timestamp with local time zone default localtimestamp,
c7 int,
c8 clob,
c9 blob
);
create sequence seq_t01 start with 1000000000 increment by 2 cache 100 ;
insert into t01(id, c1, c2, c7, c8)
values(seq_t01.nextval, 'test', 'test',99999999999,'clobtextclobtextclobtextclobtextclobtextclobtextclobtextclobtextclobtext');
select * from t01\G
支援通過CTAS
方法從老表建立新表。
修改表結構
目前對修改表結構型別還有很多限制,如只能在同類型別中改大值域。如varchar
擴大長度。
函式
目前支援ORACLE常用的函式。如時間函式、數值函式、字串函式、型別轉換函式、條件函式、系統函式、統計分析函式等等。
時間函式
目前支援:
-
interval
-
localtimestamp
-
current_timestamp
-
sysdate
/systimestamp
select sysdate, sysdate + interval'1' minute next_min, sysdate + interval'1' hour next_hour, sysdate + interval'1' day next_day from dual;
數值函式
目前支援:
-
abs
/sign
-
floor
/'ceil` -
trunc
/mod
-
bitand
示例如下:
select abs(-3.1415926), ceil(3.1415926), floor(3.1415926), sign(-3.1415926),round(314 / 7, 0), mod(314,7), trunc(3.1415926) f
rom dual;
比較函式
目前支援:
-
least
/greatest
字串函式
目前支援:
-
pad
/lpad
/rpad
-
instr
/substr
-
concat
/||
-
length
/lengthb
-
lower
/upper
select lpad(3.14159, 10, '*') c1, rpad('Apple', 10, '<') c2, ltrim(' Hello') c3, rtrim('World! ') c4, 'Hello ' || ' World' c5, concat('Hello ',' World!') c6, lower('Hello World!') c7, upper('Hello World!') c8, instr('Hello World','o',6) c9, substr('Hello World',5,3) c10, length('Hello 中國!') c11, lengthb('Hello 中國!') c12 from dual;
型別轉換函式
主要是數值、字串、日期之間的互相轉換
目前支援:
-
to_date
/to_timestamp
-
to_char
-
to_number
示例如下:
select cast('3.1415926' as number), to_date('2019-05-23 14:00:00','yyyy-mm-dd hh24:mi:ss'), to_number('3.1415926'), to_char(3.1415926), to_timestamp(sysdate), to_timestamp_tz(sysdate) from dual;
條件函式
目前支援:
-
case...when...end
-
nvl
-
decode
drop table t02;
create table t02(id number not null , type varchar2(2), gmt_create date not null default sysdate);
insert into t02(id,type) values(1,'R'),(2,'B'),(3,NULL);commit;
select id, NVL(type,'NULL!') type, case type when 'R' then 'Good guy!' when 'B' then 'Bad guy!' else 'Unknown!' end type_desc,
decode(type,'R','Good guy!','B','Bad guy!','Unknown!') type_desc2, gmt_create
from t02;
系統函式
目前支援:
-
uid
/user
select uid, user, userenv('schemaid'),SYS_CONTEXT('userenv','current_user') from dual;
select rownum rn,object_name, object_type from user_objects order by object_name;
分析函式
目前支援的聚合函式包括:
-
COUNT
-
APPROX_COUNT_DISTINCT
-
SUM
,MAX
,MIN
,AVG
-
LISTAGG
,RANK
,DENSE_RANK
,PERCENT_RANK
+ROW_NUMBER
-
NTILE
,CUME_DIST
,FIRST_VALUE
,LAST_VALUE
-
LEAD
,LAG
,NTH_VALUE
示例如下:
統計組內排名
select customernumber, checknumber, paymentdate, amount, dense_rank() over (partition by customernumber order by amount) dense_rank ,
round(PERCENT_RANK() over (partition by customernumber order by amount),2) perentage
from payments where customernumber=141 order by dense_rank, customernumber;
統計數量近似值
select count(distinct customernumber), APPROX_COUNT_DISTINCT(customernumber) from payments;
統計分組彙總
select * from (
select OFFICECODE, listagg(LASTNAME,',') within GROUP (ORDER BY EMPLOYEENUMBER) OVER (partition by OFFICECODE) EMP_LIST,
row_number() over (partition by OFFICECODE order by EMPLOYEENUMBER) as rn
FROM employees)
where rn = 1;
統計分組內第一和最後一個值
select OFFICECODE, listagg(LASTNAME,',') within GROUP (ORDER BY EMPLOYEENUMBER) OVER (partition by OFFICECODE) EMP_LIST,
first_value(LASTNAME) over (partition by OFFICECODE order by EMPLOYEENUMBER ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) first_emp_all,
last_value(LASTNAME) over (partition by OFFICECODE order by EMPLOYEENUMBER ) last_emp,
last_value(LASTNAME) over (partition by OFFICECODE order by EMPLOYEENUMBER ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) last_emp_all
FROM employees
order by OFFICECODE;
同行顯示分組統計的上一個和下一個值
select customernumber, checknumber, paymentdate,amount, round(cume_dist() over (partition by customernumber order by amount) ,2) as cume_dist ,lag(amount) over (partition by customernumber order by amount) last_ck,lead(amount) over (partition by customernumber order by amount) next_ckfrom payments where customernumber in (412,447,452,458) order by customernumber;
後記
更多Oracle的相容性還在陸續開發或內部試用中。尤其是儲存過程、PLSQL
以及Package
。請保持關注。
OceanBase對Oracle的相容只是使用者功能介面上的相容,其底層原理依然是OceanBase特有的原理。
推薦閱讀
-
OceanBase資料庫實踐入門——瞭解總控服務
-
OceanBase資料庫實踐入門——常用操作SQL
-
OceanBase分割槽表有什麼不同?
更多後續分享敬請關注公眾號:obpilot