目录
前言
将oracle数据库的lps&tps查询导入vertica数据库
- 将oracle数据库的lps&tps查询保存为csv文件
- 将所有csv文件上传到vertica服务器的/home/dbadmin目录下
- 用vsql客户端命令连接到vertica数据库
- 在vertica数据库中创建相关的lps&tps表
- 使用copy命令将csv文件导入刚创建的表中
将oracle数据库的lps&tps查询保存为csv文件
这里需要说明一下,lps和tps分别是oracle度量的两个指标,我们使用一个sql查询即可完成,但是我们需要从三个维度才能更详细地阐述lps&tps。这三个维度分别是:最近1小时(按每分钟间隔),最近24小时(按每小时间隔)和最近31天(按每小时间隔)。和上一篇博客文章类似的操作方法,我们在oracle sql developer中以脚本方式查询出lps&tps,然后将其保存为csv文件。由于文件中的数据行过长,所以不便在这里贴出,现已上传到我的,您可以从这里查看,它们分别是:,和。三个维度的sql代码分别如下所示:
-- logons per sec & user transaction per sec in last 1 hour (interval by each minute).
set feedback off;
set sqlformat csv;
set linesize 200
set pagesize 200
column metric_name format a12
column snap_date_time format a20
column psn format 999,999.99
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
select to_char(end_time, 'yyyy-mm-dd hh24:mi:ss') snap_date_time -- the group column
, decode(metric_name, 'user transaction per sec', 'transactions', 'logons per sec', 'logons') metric_name -- the series column
, round(value, 2) psn -- the value column
from v$sysmetric_history
where metric_name in ('user transaction per sec', 'logons per sec')
and group_id = 2 -- just retrieve the name with "system metrics long duration" in v$metricgroup
-- order by snap_date_time
-- , metric_name
order by metric_name
, snap_date_time
;
-- logons per sec & user transaction per sec in last 24 hours (interval by each hour).
set feedback off;
set sqlformat csv;
set linesize 200
set pagesize 200
column metric_name format a12
column snap_date_time format a20
column psn format 999,999.99
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
select to_char(end_time, 'yyyy-mm-dd hh24:mi:ss') snap_date_time -- the group column
, decode(metric_name, 'user transaction per sec', 'transactions', 'logons per sec', 'logons') metric_name -- the series column
, round(average, 2) psn -- the value column
from dba_hist_sysmetric_summary
where metric_name in ('user transaction per sec', 'logons per sec')
and end_time >= sysdate - 1
-- order by snap_date_time
-- , metric_name
order by metric_name
, snap_date_time
;
-- logons per sec & user transaction per sec in last 31 days (interval by each hour).
set feedback off;
set sqlformat csv;
set linesize 200
set pagesize 200
column metric_name format a12
column snap_date_time format a20
column psn format 999,999.99
alter session set nls_date_format = 'yyyy-mm-dd hh24:mi:ss';
select to_char(end_time, 'yyyy-mm-dd hh24:mi:ss') snap_date_time -- the group column
, decode(metric_name, 'user transaction per sec', 'transactions', 'logons per sec', 'logons') metric_name -- the series column
, round(average, 2) psn -- the value column
from dba_hist_sysmetric_summary
where metric_name in ('user transaction per sec', 'logons per sec')
and end_time >= sysdate - 30
-- order by snap_date_time
-- , metric_name
order by metric_name
, snap_date_time
;
将所有csv文件上传到vertica服务器的/home/dbadmin目录下
这里,省略了上传的过程和一些授权相关的步骤,最后的上传结果详见“<<==”所指向的三个csv文件。
[dbadmin@test ~]$ ls -lrht
total 112k
drwxr-xr-x 5 dbadmin verticadba 134 dec 15 14:06 vdb_oracle_perf
-rw-r--r-- 1 dbadmin verticadba 27k dec 16 18:37 oracle_dbtime.csv
-rw-r--r-- 1 dbadmin verticadba 1.9k dec 16 18:37 oracle_dbtime_2.csv
-rw-rw-r-- 1 dbadmin verticadba 390 dec 16 18:44 imp.log
-rw-rw-r-- 1 dbadmin verticadba 393 dec 16 18:45 imp_2.log
-rw-r--r-- 1 dbadmin verticadba 4.7k dec 20 09:42 oracle_lps_tps_in_last_1_hour.csv <<==
-rw-r--r-- 1 dbadmin verticadba 2.0k dec 20 09:45 oracle_lps_tps_in_last_24_hours.csv <<==
-rw-r--r-- 1 dbadmin verticadba 57k dec 20 09:47 oracle_lps_tps_in_last_31_days.csv <<==
用vsql客户端命令连接到vertica数据库
这次我们不用去连接vertica数据库了,哈哈!因为在vertica中自带了一个客户端,(类似于oracle的sql*plus),而且使用它在命令行接口上更加方便和快捷。连接过程详见下面的操作步骤:
[root@test ~]# su - dbadmin
[dbadmin@test ~]$ /opt/vertica/bin/vsql -h 127.0.0.1 vdb_oracle_perf dbadmin
password:
welcome to vsql, the vertica analytic database interactive terminal.
type: \h or \? for help with vsql commands
\g or terminate with semicolon to execute query
\q to quit
vdb_oracle_perf=>
在vertica数据库中创建相关的lps&tps表
在vsql下分别创建三个表,表名依次为:oracle_lps_tps_in_last_1_hour,oracle_lps_tps_in_last_24_hours和oracle_lps_tps_in_last_31_days。其操作步骤分别如下所示:
vdb_oracle_perf=> create table public.oracle_lps_tps_in_last_1_hour
vdb_oracle_perf=> (snap_date_time timestamp,
vdb_oracle_perf=> metric_name varchar2(20),
vdb_oracle_perf=> psn number(8,2)
vdb_oracle_perf=> );
create table
vdb_oracle_perf=>
vdb_oracle_perf=> create table public.oracle_lps_tps_in_last_24_hours
vdb_oracle_perf=> (snap_date_time timestamp,
vdb_oracle_perf=> metric_name varchar2(20),
vdb_oracle_perf=> psn number(8,2)
vdb_oracle_perf=> );
create table
vdb_oracle_perf=>
vdb_oracle_perf=> create table public.oracle_lps_tps_in_last_31_days
vdb_oracle_perf=> (snap_date_time timestamp,
vdb_oracle_perf=> metric_name varchar2(20),
vdb_oracle_perf=> psn number(8,2)
vdb_oracle_perf=> );
create table
vdb_oracle_perf=>
使用copy命令将csv文件导入刚创建的表中
导入过程请看下面的具体步骤:
vdb_oracle_perf=>
vdb_oracle_perf=> copy public.oracle_lps_tps_in_last_1_hour from '/home/dbadmin/oracle_lps_tps_in_last_1_hour.csv' exceptions '/home/dbadmin/imp_lps_tps_1.log' delimiter as ',';
notice 7850: in a multi-threaded load, rejected record data may be written to additional files
hint: exceptions may be written to files [/home/dbadmin/imp_lps_tps_1.log], [/home/dbadmin/imp_lps_tps_1.log.1], etc
rows loaded
-------------
122
(1 row)
vdb_oracle_perf=> copy public.oracle_lps_tps_in_last_24_hours from '/home/dbadmin/oracle_lps_tps_in_last_24_hours.csv' exceptions '/home/dbadmin/imp_lps_tps_2.log' delimiter as ',';
notice 7850: in a multi-threaded load, rejected record data may be written to additional files
hint: exceptions may be written to files [/home/dbadmin/imp_lps_tps_2.log], [/home/dbadmin/imp_lps_tps_2.log.1], etc
rows loaded
-------------
48
(1 row)
vdb_oracle_perf=> copy public.oracle_lps_tps_in_last_31_days from '/home/dbadmin/oracle_lps_tps_in_last_31_days.csv' exceptions '/home/dbadmin/imp_lps_tps_3.log' delimiter as ',';
notice 7850: in a multi-threaded load, rejected record data may be written to additional files
hint: exceptions may be written to files [/home/dbadmin/imp_lps_tps_3.log], [/home/dbadmin/imp_lps_tps_3.log.1], etc
rows loaded
-------------
1440
(1 row)
vdb_oracle_perf=>
用tableau可视化vertica数据库的表
在tableau中连接vertica数据库并选择schema为public,将表oracle_lps_tps_in_last_1_hour用鼠标拖动到指定位置,然后转到工作表,准备下一步的可视化操作。对应的两个屏幕截图分别是:
在工作表区域上,我们把度量名称snap date time用鼠标拖动到标签名为“列”的标签框里(默认是年,将其展开到秒),把度量值psn拖动到标签名为“行”的标签框中,再将度量名称metric name拖动到标签名为“页面”的标签里。与此同时,我们会看到在整个页面的右边区域上新增了一个metric name的标签。不幸的是,这两个度量“logons”和“transactions”不能同时出现在一个“折线图”中。我们需要在标签metric name上选择logons或transactions才能出现与其对应的折线图。详见如下两图的红框标明位置:
回头想想,造成这个问题的原因是表oracle_lps_tps_in_last_1_hour的列metric name的值logons和transactions在同一列,也就是其对应的度量值psn均在同一列,所以这两个度量各称无法出现在同一个折线图上。因此我们需要把metric name的两个值所在的行转变为两个列,在这两个列中分别保存各自的度量值psn,这个需求我们称之为行转列操作。接着在tableau中的vertica数据库表oracle_lps_tps_in_last_1_hour才会有两个不同的度量。到这里,我们就暂先放弃了对表oracle_lps_tps_in_last_24_hours和oracle_lps_tps_in_last_31_days的可视化操作。
对原始sql进行行转列改造,再重复执行前两步
1. 三个维度的行转列部分sql代码如下所示(由于代码过多,这里只贴出行转列的关键部分,其余内容和前面展示的sql代码完全一致):
-- converting rows to columns based on logons per sec & user transaction per sec in last 1 hour (interval by each minute).
......
with lps_tps_in_last_1_hour as
(
......
)
select *
from lps_tps_in_last_1_hour
pivot ( max(psn)
for metric_name in
( 'logons' as "logons"
, 'transactions' as "transactions"
)
)
order by snap_date_time
;
-- converting rows to columns based on logons per sec & user transaction per sec in last 24 hours (interval by each hour).
......
with lps_tps_in_last_24_hours as
(
......
)
select *
from lps_tps_in_last_24_hours
pivot ( max(psn)
for metric_name in
( 'logons' as "logons"
, 'transactions' as "transactions"
)
)
order by snap_date_time
;
-- converting rows to columsn based on logons per sec & user transaction per sec in last 31 days (interval by each hour).
......
with lps_tps_in_last_31_days as
(
......
)
select *
from lps_tps_in_last_31_days
pivot ( max(psn)
for metric_name in
( 'logons' as "logons"
, 'transactions' as "transactions"
)
)
order by snap_date_time
;
2. 同样,将上面的sql代码分别在oracle sql developer里以脚本方式运行并保存为csv文件,您可以从这里查看,它们分别是:,和。
3. 将三个csv文件上传到vertica数据库服务器的/home/dbadmin/目录下,见“<<==”标明的位置。
[dbadmin@test ~]$ ls -lrht
total 156k
drwxr-xr-x 5 dbadmin verticadba 134 dec 15 14:06 vdb_oracle_perf
-rw-r--r-- 1 dbadmin verticadba 27k dec 16 18:37 oracle_dbtime.csv
-rw-r--r-- 1 dbadmin verticadba 1.9k dec 16 18:37 oracle_dbtime_2.csv
-rw-rw-r-- 1 dbadmin verticadba 390 dec 16 18:44 imp.log
-rw-rw-r-- 1 dbadmin verticadba 393 dec 16 18:45 imp_2.log
-rw-r--r-- 1 dbadmin verticadba 4.7k dec 20 09:42 oracle_lps_tps_in_last_1_hour.csv
-rw-r--r-- 1 dbadmin verticadba 2.0k dec 20 09:45 oracle_lps_tps_in_last_24_hours.csv
-rw-r--r-- 1 dbadmin verticadba 57k dec 20 09:47 oracle_lps_tps_in_last_31_days.csv
-rw-rw-r-- 1 dbadmin verticadba 425 dec 20 10:06 imp_lps_tps_1.log
-rw-rw-r-- 1 dbadmin verticadba 428 dec 20 10:06 imp_lps_tps_2.log
-rw-rw-r-- 1 dbadmin verticadba 428 dec 20 10:07 imp_lps_tps_3.log
-rw-r--r-- 1 dbadmin verticadba 1.9k dec 20 14:51 crbc_oracle_lps_tps_in_last_1_hour.csv <<==
-rw-r--r-- 1 dbadmin verticadba 831 dec 20 14:52 crbc_oracle_lps_tps_in_last_24_hours.csv <<==
-rw-r--r-- 1 dbadmin verticadba 24k dec 20 14:53 crbc_oracle_lps_tps_in_last_31_days.csv <<==
4. 用vsql连接到vertica数据库创建新表(表名在原表的基础上增加crbc前缀)并用copy命令导入csv文件中的数据。
vdb_oracle_perf=>
vdb_oracle_perf=> create table public.crbc_oracle_lps_tps_in_last_1_hour
vdb_oracle_perf=> (snap_date_time timestamp,
vdb_oracle_perf=> logons number(8,2),
vdb_oracle_perf=> transactions number(8,2)
vdb_oracle_perf=> );
create table
vdb_oracle_perf=>
vdb_oracle_perf=> create table public.crbc_oracle_lps_tps_in_last_24_hours
vdb_oracle_perf=> (snap_date_time timestamp,
vdb_oracle_perf=> logons number(8,2),
vdb_oracle_perf=> transactions number(8,2)
vdb_oracle_perf=> );
create table
vdb_oracle_perf=>
vdb_oracle_perf=> create table public.crbc_oracle_lps_tps_in_last_31_days
vdb_oracle_perf=> (snap_date_time timestamp,
vdb_oracle_perf=> logons number(8,2),
vdb_oracle_perf=> transactions number(8,2)
vdb_oracle_perf=> );
create table
vdb_oracle_perf=>
vdb_oracle_perf=> copy public.crbc_oracle_lps_tps_in_last_1_hour from '/home/dbadmin/crbc_oracle_lps_tps_in_last_1_hour.csv' exceptions '/home/dbadmin/imp_crbc_lps_tps_1.log' delimiter as ',';
notice 7850: in a multi-threaded load, rejected record data may be written to additional files
hint: exceptions may be written to files [/home/dbadmin/imp_crbc_lps_tps_1.log], [/home/dbadmin/imp_crbc_lps_tps_1.log.1], etc
rows loaded
-------------
61
(1 row)
vdb_oracle_perf=>
vdb_oracle_perf=> copy public.crbc_oracle_lps_tps_in_last_24_hours from '/home/dbadmin/crbc_oracle_lps_tps_in_last_24_hours.csv' exceptions '/home/dbadmin/imp_crbc_lps_tps_2.log' delimiter as ',';
notice 7850: in a multi-threaded load, rejected record data may be written to additional files
hint: exceptions may be written to files [/home/dbadmin/imp_crbc_lps_tps_2.log], [/home/dbadmin/imp_crbc_lps_tps_2.log.1], etc
rows loaded
-------------
24
(1 row)
vdb_oracle_perf=>
vdb_oracle_perf=> copy public.crbc_oracle_lps_tps_in_last_31_days from '/home/dbadmin/crbc_oracle_lps_tps_in_last_31_days.csv' exceptions '/home/dbadmin/imp_crbc_lps_tps_3.log' delimiter as ',';
notice 7850: in a multi-threaded load, rejected record data may be written to additional files
hint: exceptions may be written to files [/home/dbadmin/imp_crbc_lps_tps_3.log], [/home/dbadmin/imp_crbc_lps_tps_3.log.1], etc
rows loaded
-------------
720
(1 row)
vdb_oracle_perf=>
5. 在tableau中连接vertica数据库并可视化新表
这次,我们终于可以将两个度量logons和transactions在同一个折线图中展示出来了,其中对新表crbc_oracle_lps_tps_in_last_1_hour的可视化操作依次见如下五个屏幕截图:
表crbc_oracle_lps_tps_in_last_24_hours和crbc_oracle_lps_tps_in_last_31_days的可视化结果分别如下所示:
以上就是用tableau可视化vertica数据库之“lps&tps”篇的所有内容。另外,您也可以在的第32-64行、第66-98行、第100-132行、第217-238行、第344-365行和第367-388行找到我前面提到的所有sql源代码。欢迎业界各位朋友在文章底部的评论区提出您的反馈意见。