此实验申请地址在这里,时间为120分钟。
帮助见这里。
简介
参加本次动手实践研讨会,了解如何使用 True Cache 通过卸载查询和减少对主数据库的请求和连接数量来提高可扩展性。本次研讨会基于连接到配置了 True Cache 的主数据库的计算实例(在线事务处理应用程序)。演示应用程序是一个使用 23ai JDBC 驱动程序的 Java 程序。它模拟了主数据库的大量事务,以及如何将只读查询卸载到 true cache 来帮助提高应用程序性能。
Oracle Database True Cache 是一种内存中、一致且自动管理的 SQL 和键值(对象)缓存。从概念上讲,True Cache 是一种无磁盘 Active Data Guard (ADG)。当数据库成为瓶颈时,许多大型 Web 应用程序都会面临性能问题。参加本次动手实践研讨会,了解如何使用 True Cache 通过卸载查询和减少对主数据库的请求和连接数量来提高可扩展性。
为什么要使用 True Cache?即使没有对数据进行分区,True Cache 也可用于扩展以读取为主的应用程序。当主数据库成为瓶颈时,True Cache 可用于卸载主数据库并扩展工作负载。此外,单个查询中的数据始终是一致且最新的,当跨多行连接(join)时,这一点很重要。
一些问题:
- 对象缓存,对象指的是什么?Segment, Row set?
- 我只访问几行,需要把整个表拿过来吗?
- 内存不够怎么办,内存需要多大?也是LRU算法吗?
本实验目标为了解如何使用 True Cache 通过卸载查询和减少对主数据库的请求和连接数量来提高可扩展性。
完成设置后,下一个实验将涵盖:
- 创建数据并将其加载到基于事务的模式
- 运行基于 Java 的应用程序,使用 jdbc 连接到数据库,并首先针对主数据库运行不同的事务,然后针对 True Cache 实例运行不同的事务,以显示 True Cache 如何提高应用程序性能。
先决条件(可选)
- 最好熟悉 Oracle 数据库,但这不是必需的
- 最好熟悉 Java 和 JDBC,但这不是必需的
- 对云和数据库术语有所了解会有所帮助
- 熟悉 Oracle 云基础设施 (OCI) 会有所帮助
实验 1:初始化环境
任务 1:验证所需进程是否已启动并正在运行。
- 按照以下指示进行操作,以验证您的环境,然后再开始执行后续实验。以下进程应已启动并正在运行:
- Oracle 主数据库容器
- Oracle True Cache 容器
- 客户端应用容器
- 打开终端会话并按照以下指示进行操作以验证服务。
$ sudo podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
df86e4394197 localhost/oracle/database-ext-truecache:23.4.0-ee /bin/sh -c exec $... 2 months ago Up 27 minutes prod
c6ef9520bf62 localhost/oracle/database-ext-truecache:23.4.0-ee /bin/sh -c exec $... 2 months ago Up 26 minutes truedb
078364fd5b61 container-registry.oracle.com/os/oraclelinux:8 /usr/sbin/init 2 months ago Up 27 minutes appclient
如果容器已停止且未处于运行状态,则尝试使用以下 docker 命令重新启动它。
sudo podman stop <container ID/NAME>
sudo podman start <container ID/NAME>
对于多个容器,运行以下命令一次性重新启动所有容器:
sudo podman container stop $(sudo podman container list -qa)
sudo podman container start $(sudo podman container list -qa)
实验 2:将数据加载到数据库
在本实验中,您将创建事务处理模式并将数据加载到其中。
关于 Oracle True Cache
现代应用程序通常需要在连接数和可缓存的数据量方面具有巨大的可扩展性。
一种流行的方法是将缓存放在数据库前面。这些缓存依赖于应用程序通常不需要查看最新数据的事实。例如,当有人浏览航班预订系统时,系统可以显示一秒钟前的航班数据。当有人预订航班时,系统将显示最新数据。
Oracle True Cache 仅使用其缓冲区缓存中的数据来满足查询。与 Oracle Active Data Guard 一样,True Cache 是主数据库的全功能只读复制,只是它基本上是无盘的。
在本实验中,您将:
- 了解如何在新创建的环境中创建模式,创建表。
- 将数据上传到这些表
任务 1:创建用户和表
登入主数据库所在服务器:
$ sudo podman exec -it prod /bin/bash
bash-4.4$
登入数据库,运行脚本step1.sql:
bash-4.4$ sqlplus / as sysdba
SQL*Plus: Release 23.0.0.0.0 - Production on Wed Jul 24 01:12:22 2024
Version 23.4.0.24.05
Copyright (c) 1982, 2024, Oracle. All rights reserved.
Connected to:
Oracle Database 23ai Enterprise Edition Release 23.0.0.0.0 - Production
Version 23.4.0.24.05
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 ORCLPDB1 READ WRITE NO
SQL> alter session set container=orclpdb1;
Session altered.
SQL> @step1
User dropped.
User created.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
step1.sql 创建用户transactions 并赋权:
rem
rem Connect to pdb as sys
rem
DROP USER IF EXISTS transactions CASCADE ;
CREATE USER transactions identified BY WElcome_#12;
GRANT CONNECT, RESOURCE TO transactions;
GRANT SELECT_CATALOG_ROLE TO transactions;
GRANT UNLIMITED TABLESPACE TO transactions;
GRANT ALL PRIVILEGES TO transactions;
GRANT DBA TO transactions;
当前注册到监听的服务如下:
$ lsnrctl status
LSNRCTL for Linux: Version 23.0.0.0.0 - Production on 24-JUL-2024 01:16:50
Copyright (c) 1991, 2024, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=prod)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 23.0.0.0.0 - Production
Start Date 24-JUL-2024 00:33:49
Uptime 0 days 0 hr. 43 min. 1 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /opt/oracle/product/23ai/dbhome_1/network/admin/listener.ora
Listener Log File /opt/oracle/diag/tnslsnr/prod/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=prod)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "19387f2c53b50cf0e063020114ac67bc" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "ORCLCDB" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "ORCLCDBXDB" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "SALES1" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "SALES1_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "SALES2" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "SALES2_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "SALES3" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "SALES3_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "SALES4" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "SALES4_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "TRUEDB" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "orclpdb1" has 2 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
The command completed successfully
以transactions用户登录SALES服务,但每次都是登录到TRUEDB库,而TRUEDB库作为备库是只读的:
$ sqlplus transactions/WElcome_#12@orclpdb1
只好,以sys用户登录,但以transactions用户执行step2和step3:
SQL> show user
USER is "SYS"
SQL> alter session set container=orclpdb1;
Session altered.
SQL> alter session set current_schema=transactions;
Session altered.
SQL> @step2
Function created.
SQL> @step3
Table dropped.
Table dropped.
Sequence dropped.
Sequence created.
Table created.
Table created.
SQL> show user
USER is "SYS"
任务 2:将数据加载到表中
执行step4,加载数据,这一步时间稍长:
SQL> alter session set container=orclpdb1;
Session altered.
SQL> alter session set current_schema=transactions;
Session altered.
SQL> @step4
Table truncated.
Elapsed: 00:00:00.04
2000000 rows created.
Commit complete.
Elapsed: 00:00:08.15
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:08.42
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:13.66
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:13.07
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:18.51
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:12.16
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:12.34
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:12.00
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:13.01
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:12.81
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:13.23
1 INSERT INTO transactions.accounts
2 SELECT transactions.country_cd_from_account_id(n)
3 ,n
4 ,n
5 ,10000
6 ,SYS_EXTRACT_UTC(SYSTIMESTAMP)
7 FROM (
8 SELECT &&initial_value + LEVEL - 1 AS n
9 FROM dual
10 CONNECT BY LEVEL <= &&final_value - &&initial_value + 1
11* )
2000000 rows created.
Commit complete.
Elapsed: 00:00:14.61
PL/SQL procedure successfully completed.
Commit complete.
Elapsed: 00:00:06.62
任务 3:验证 True Cache
$ sudo podman exec -it truedb /bin/bash
bash-4.4$
$ lsnrctl status
LSNRCTL for Linux: Version 23.0.0.0.0 - Production on 24-JUL-2024 02:27:51
Copyright (c) 1991, 2024, Oracle. All rights reserved.
Connecting to (ADDRESS=(PROTOCOL=TCP)(HOST=truedb)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 23.0.0.0.0 - Production
Start Date 24-JUL-2024 00:34:28
Uptime 0 days 1 hr. 53 min. 23 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /opt/oracle/product/23ai/dbhome_1/network/admin/listener.ora
Listener Log File /opt/oracle/diag/tnslsnr/truedb/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=truedb)(PORT=1521)))
Services Summary...
Service "SALES1_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "SALES2_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "SALES3_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "SALES4_TC" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "TRUEDB" has 2 instance(s).
Instance "TRUEDB", status UNKNOWN, has 1 handler(s) for this service...
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
Service "orclpdb1" has 1 instance(s).
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
The command completed successfully
$ sqlplus transactions/WElcome_#12@orclpdb1
SQL*Plus: Release 23.0.0.0.0 - Production on Wed Jul 24 02:29:21 2024
Version 23.4.0.24.05
Copyright (c) 1982, 2024, Oracle. All rights reserved.
Connected to:
Oracle Database 23ai Enterprise Edition Release 23.0.0.0.0 - Production
Version 23.4.0.24.05
SQL> select database_role from v$database;
DATABASE_ROLE
----------------
TRUE CACHE
实验 3:通过 jdbc 使用 True Cache
在此实验中,您将通过 jdbc 测试与 true cache 的连接,然后首先针对主数据库运行事务处理应用程序,然后针对 true cache 实例运行事务处理应用程序,并观察性能的改进。
应用程序使用主数据库的单个数据库应用程序服务名称维护一个逻辑连接,而 JDBC Thin 驱动程序(Oracle Database 23ai 及更高版本)维护两个物理连接。应用程序通过特殊调用将逻辑连接标记为只读或读写,从而控制 True Cache 和主数据库实例之间的读/写拆分。此模式仅适用于基于 JDBC 的应用程序。
此处使用的应用程序是一个事务处理应用程序,它对数据库执行各种事务操作。其中一些操作包括获取客户余额、获取客户详细信息、更新余额。每个线程模拟执行不同操作的用户。
在本实验中,您将:
- 在连接到主数据库时运行应用程序
- 在连接到 True Cache 时运行应用程序并观察性能差异
任务 1:运行应用程序
登录客户端:
$ sudo podman exec -it appclient /bin/bash
[root@appclient /]#
查看示例程序:
# cd /stage/clientapp
# ls -l
total 8220
-rwxr-xr-x. 1 1000 1000 638 May 24 19:40 TransactionApp.sh
-rw-r--r--. 1 1000 1000 9868 Aug 6 2023 Transactions_TrueCache.class
-rw-r--r--. 1 1000 1000 12246 Aug 6 2023 Transactions_TrueCache.java
-rw-r--r--. 1 1000 1000 6917992 Aug 3 2023 ojdbc8.jar
-rw-r--r--. 1 1000 1000 1469874 Aug 3 2023 ucp.jar
查看运行脚本,这个脚本比较了两次运行的性能,这两次运行的差异只在最后那个参数上有区别,即true和false:
# cat TransactionApp.sh
#!/bin/sh
ARGS="$@"
JAVA_HOME=/stage/jdk-17.0.6
PATH=$JAVA_HOME/bin:$PATH
CURRENT_DIR="$PWD"
export JAVA_HOME
export PATH
echo "CURRENT DIRECTORY : $CURRENT_DIR"
echo "Using JAVA_HOME :$JAVA_HOME"
echo "Now running Transactions against Primary database for 2 minutes with 50 users"
java -cp $CURRENT_DIR/ojdbc8.jar:$CURRENT_DIR/ucp.jar:. Transactions_TrueCache \ "prod:1521/sales1" transactions WElcome_#12 50 120 false
echo "Now running Transactions against True Cache for 2 minutes with 50 users"
java -cp $CURRENT_DIR/ojdbc8.jar:$CURRENT_DIR/ucp.jar:. Transactions_TrueCache \ "prod:1521/sales1" transactions WElcome_#12 50 120 true
脚本的成功结果确保 jdbc 能够通过 oracle.jdbc.useTrue CacheDriverConnection=true 参数连接到 True Cache 实例。
# ./TransactionApp.sh
CURRENT DIRECTORY : /stage/clientapp
Using JAVA_HOME :/stage/jdk-17.0.6
Now running Transactions against Primary database for 2 minutes with 50 users
Beginning of the transaction test!!!
Result Summary
Number of transactions completed: 163650
Transactions per second: 1363
Read Only Transactions per second: 909
Number of selects completed: 218200
Number of updates completed: 54550
Number of inserts completed: 54550
Number of commits completed: 54550
Now running Transactions against True Cache for 2 minutes with 50 users
Beginning of the transaction test!!!
Using True cache for select statements now
Result Summary
Number of transactions completed: 188550
Transactions per second: 1571
Read Only Transactions per second: 1047
Number of selects completed: 251400
Number of updates completed: 0
Number of inserts completed: 0
Number of commits completed: 0
通过运行 TransactionApp.sh 来运行应用程序,这将首先针对主服务器运行该应用程序 2 分钟,然后使用 50 个线程针对 True Cache 实例运行该应用程序 2 分钟。
遗留问题
如果一个服务对应2个实例,那如何登录到指定的实例呢?本例为TRUEDB:
Service "orclpdb1" has 2 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Instance "TRUEDB", status READY, has 1 handler(s) for this service...
参考
- Oracle True Cache Technical Architecture
- Oracle True Cache User’s Guide
- https://github.com/oracle-livelabs/database/tree/main/truecache
- https://stackoverflow.com/questions/22049212/copying-files-from-docker-container-to-host