This tutorial shows you how to cache Oracle database tables using Oracle In-Memory Database Cache (IMDB Cache). IMDB Cache improves application transaction response time, by caching a performance-critical subset of tables from an Oracle database into a TimesTen in-memory database running in the application tier. This tutorial provides a step by step guide to create an In-Memory Database Cache Grid with a number of grid members to distribute the cache data on multiple nodes. The tutorial will show how to specify and create different cache groups and how to access the cache data.
Approximately 50 minutes
In this tutorial, the following software have been pre-installed and configured:
1. Oracle In-Memory Database Cache 11.2.1.8.0
2. Oracle Database 11.2.0.2
3. SQL Developer 3.0
IMDB Cache improves application transaction response time, by caching a performance-critical subset of tables from an Oracle database into a TimesTen in-memory database running in the application tier. This tutorial provides a step by step guide to create an In-Memory Database Cache Grid with a number of grid members to distribute the cache data and workload. The tutorial will show how to specify and create different cache groups and how to access the cache data.
An application typically chooses to cache frequently-accessed data, or performance-sensitive data to improve application response time and increase throughput. IMDB Cache uses the concept of a cache group to define the Oracle tables to be cached. A cache group is a collection of cached tables related to each other by foreign key constraints. A database cache grid is a cluster of TimesTen in-memory databases that collectively manage an application's cache data.
The focus of this tutorial is to illustrate the characteristics of the different types of cache groups deployed inside a cache grid, and to demonstrate the interactions of the cache data between in-memory databases and the Oracle database.
In this tutorial, tables from the Oracle user TTHR are cached. Note: the tables in the TTHR schema are identical to those in the HR sample schema.
The tables are cached in 2 different cache groups.
The data in the above cache groups are pre-loaded from the Oracle database.
Typically, cache databases and cache groups are set up by a database administrator known as the cache manager, and application users are granted access to the cache data. In this tutorial, the in-memory database user TTHR acts as both the cache manager and the application user.
Before you start caching Oracle tables, you need to create a TimesTen in-memory database. This database must include a user with either ADMIN or CACHE_MANAGER privilege. In this tutorial, we have a cache manager with ADMIN privilege; cache manager is responsible for administering the cache grid and cache groups. The Oracle user that owns the tables to be cached must also exist in the cache database, this user is known as the cache table user.
The followings have been pre-created:
- An Oracle database
- Two TimesTen in-memory databases, cachedb1 and cachedb2
- User tthr; who has the credential for the cache manager and cache table user
In this tutorial section, you use SQL Developer to connect to an in-memory database, and run a script to create a cache grid.
1. |
Double click on the desktop icon "SQL Developer 3.0 " to launch SQL Developer.
|
2. |
TimesTen connections work best in SQL Developer if the AutoCommit flag is checked.
|
3. |
Click on + sign next to Database to expand the preferences.
|
4. |
Check the Autocommit checkbox, if not already checked.
|
5. |
In the Connections tab, click on the New Connection icon, a green plus + sign.
|
6. |
Enter oracle in the Connection Name field.
|
7. |
Select TTORCL from the Network Alias drop down list.
|
8. |
The connection oracle has been created. It should appear in the Connections navigator. If not click the Refresh icon.
|
1. |
Now, use SQL Developer to create a connection to the TimesTen database cachedb1.
|
2. |
In the Connections tab, click on the New Connection icon, a green plus + sign. A "New / Select Database Connection" dialog box appears.
|
3. |
Enter cachedb1 in the Connection Name field.
|
4. |
Select cachedb1 from the DSN drop down list.
|
5. |
Check the status of the connection on the status line (above the Help button). It should read Success.
|
6. |
The connection cachedb1 has been created. It should appear in the Connections navigator. If not, expand out the connections by clicking on the + sign next to Connections.
|
1. |
Click on Open to bring up the Open File dialog box.
|
2. |
Navigate to the SQL script directory. If there is a cache_grid_sql icon on the left panel, click on the icon to go directly to the SQL script directory. Otherwise, use Location: to navigate to the script directory:
|
3. |
Select the file 01_create cache_grid.sql and click Open.
|
4. |
The content of this file appears in a SQL Worksheet tab. Review the code.
|
5. |
Click Run Script to execute this script on cachedb1.
|
6. |
The result from running the script is displayed in the Script Output Pane.
|
7. |
Close the file 01_create_cache_grid.sql by clicking on the button X, located on the right of the tab.
|
In this topic you will learn how to create cache groups to cache Oracle tables into the in-memory database, cachedb1. You will create cache groups using both the Create Cache Group dialog in SQL Developer, and also by issuing the CREATE CACHE GROUP statement in SQL*Worksheet. The cache agent process is responsible for managing the interaction and data synchronization between the Oracle database and the cache database.
1. |
Before creating cache groups, the cache agent must be running.
|
2. |
Check the Start agent check box and click Apply. This starts up the cache agent for cachedb1.
|
3. |
Click OK to close the confirmation dialog box.
|
1. |
Go back to the connections navigator. Click on the + sign next to the connection cachedb1 to expand out the connection, if not already expanded.
|
2. |
Right click on Cache Groups and select New Cache Group. This brings up the Create cache group dialog box.
|
3. |
Enter awt into the Name field, and change the cache group Type from Readonly to Asynchronous Writethrough.
|
4. |
The screen defines the tables to be cached. Right click in "Tables cached" and select Add root table. This brings up a dialog box with a listing of the tables belonging to the user TTHR.
|
5. |
Select TTHR.REGIONS and click OK.
|
6. |
Right click on "Tables cached" and select Add child tables.
|
7. |
Select both child tables TTHR.COUNTRIES and TTHR.LOCATIONS and click OK.
|
8. |
Click on the DDL tab to view the corresponding CREATE CACHE GROUP statement for the cache group.
|
9. |
Click Apply to create the cache group. Click OK to close the confirmation dialog box.
|
10. |
Expand Tables to view the tables cached in the in-memory database.
|
1. |
Click on Open to bring up the Open File dialog box.
|
2. |
Click on the cache_grid_sql icon on the left panel to go directly to the SQL script directory. Select file 02_create_global_dynamic.sql and click Open.
|
3. |
Select cachedb1 as the database connection. Click Run Script to create a dynamic AWT global cache group on the TTHR.EMPLOYEES and TTHR.JOB_HISTORY tables.
Close the file 02_create_global_dyanmic.sql by clicking on the button X.
|
4. |
Go back to the connections navigator, select Cache Groups and click Refresh to view the new cache groups.
|
1. |
To enable the propagation of AWT data between the cache tables and the Oracle database, the replication agent must be running. Right-click on the connection cachedb1 in the connections navigator, and select Start / Stop Replication Agent.
|
2. |
Check the Start agent checkbox and click Apply. This starts up the replication agent on cachedb1.
|
3. |
We will now attach cachedb1 to the grid.
|
4. |
Script Output shows cachedb1 is now attached to the cache grid samplegrid. |
Data from the Oracle database can be preloaded into a cache group. Depending on the cache group type, an optional WHERE condition can also be specified to restrict or partition the data to be cached. For dynamic cache groups, data will be loaded on-demand when SELECT, INSERT, UPDATE or DELETE statement is issued targeting the cache tables. In this topic you use SQL Developer to preload the non-dynamic cache groups.
1. |
Check the content of the COUNTRIES cache tables.
|
2. |
The COUNTRIES cache table has been created, but the table is empty. Click OK to close the dialog.
|
3. |
To load data into the cache group AWT, perform the following steps.
|
4. |
Enter the following condition into the Where clause field.
In this example, cachedb1 caches only the countries from Europe (region_id=1). Click OK to close the confirmation dialog box. |
5. |
Repeat the Count Rows operation.
|
6. |
This time, it should return 8 rows. Click OK to continue.
|
After setting up the first cache database cachedb1, you will now repeat some of the previous steps to add a second cache database cachedb2 into the cache grid.
The same Global cache groups must exist in all the grid members. Local cache groups do have this requirement, each grid member can define it's own set of local cache groups.
To create, configure and attach another cache database into the cache grid, perform the following steps:
1. |
Create a database connection to cachedb2. Enter cachedb2 in the Connection Name field, and tthr in the Username and Password fields. Select cachedb2 from the DSN drop down list. Enter tthr in the Oracle Password (for Cache) field. Click Connect. The connection cachedb2 has been created. It should appear in the connections navigator. If not, expand out the connections and refresh.
|
2. |
Run a SQL script to associate cachedb2 with the cache grid samplegrid. Click on Open to bring up the Open File dialog box. Select the script 04_associate_cachedb2_grid.sql from the cache_grid_sql folder. The procedure ttGridNameset associates the cachedb2 to the cache grid samplegrid. Close the file 04_associate_cachedb2_grid.sql by clicking on the button X.
|
3. |
Start the cache agent on cachedb2. Check the Start agent check box and click Apply. This starts up the cache agent for cachedb2. Click OK to close the confirmation dialog box.
|
4. |
Create the same global dynamic AWT cache group based on the employees and job_history tables in cachedb2. The result from running the CREATE CACHE GROUP script is displayed in the Script Output Pane. Close the file 02_create_global_dyanmic.sql by clicking on the button X.
|
5. |
Go back to the Connection Navigator. Click on the + sign to expand cachedb2, if not already expanded.
|
6. |
Right-click on the connection cachedb2 in the connections navigator, and select Start / Stop Replication Agent. Check the Start agent check box and click Apply. This starts up the replication agent for cachedb2. Click OK to close the confirmation dialog box.
|
7. |
Run a SQL script to attach cachedb2 to the cache grid samplegrid. Click on Open to bring up the Open File dialog box. Select the script 05_attach_cachedb2.sql from the cache_grid_sql folder. Change the SQL Worksheet connection to cachedb2 and click Run Script. The Script Output pane shows that both cachedb1 and cachedb2 are attached to the cache grid. Close the file 05_attach_cachedb2.sql by clicking on the button X.
|
In the previous sections, you learned how to create and configure a cache grid with two grid members, and how to create different types of cache groups. In this topic, you will learn about the properties of these cache groups, and verify the data transfer between the cache grid members and the data synchronization between the Oracle database and the Cache Grid.
1. |
Select the cachedb1 table COUNTRIES from the connections navigator.
|
2. |
Click on the Data tab and view records cached in the COUNTRIES table.
|
3. |
Open and run a script to update the TTHR.COUNTRIES table on cachedb1 using SQL Worksheet.
|
4. |
The Script Output pane confirms 4 countries have been inserted, and the country name for UK has also been updated. Close the file 06_insert_countries.sql by clicking on the button X.
|
5. |
Go back to the data tab showing the COUNTRIES table from cachedb1, click Refresh to see the changes in the cache table.
|
6. |
Now go to the oracle connection and check for the the updates in the corresponding COUNTRIES table.
|
7. |
Click on the Data tab to view the records in the Oracle COUNTRIES table. Notice the updates in the cache database have been replicated to the Oracle database.
|
1. |
Load a subset of data into global cache group G_AWT into cachedb1
|
2. |
Enter the following condition into the Where clause field.
In this example, cachedb1 caches only the employees who's manager's id is 100 (manager_id=100). Click OK to close the confirmation dialog box. ![]() |
3. |
In the connections navigator, select the table EMPLOYEES from the cachedb1 connection.
|
4. |
Click on the Data tab to view the content in the cache table.
Note: EMPLOYEES table now has 14 records loaded.
|
5. |
Update Neena's phone number to 650.555.5555 The Data Editor - Log returns the result of the above operation.
|
6. |
Run a SQL script to update Neena's record from the other grid member cachedb2 . Suppose another application connection wants to update the same set of records (Neena's record) from the other grid member, cachedb2. We will now demonstrate the location transparency functionality, where the desired record is automatically transferred from cachedb1 to cachedb2 when needed Click on Open to bring up the Open File dialog box. Select the script 07_update_employees.sql from the cache_grid_sql folder. Change the SQL Worksheet connection to cachedb2 and click Run Script. The Script Output pane returns the result of the SQL operation.
There are 3 SQL operations involved in this script:
|
7. |
View the data in the EMPLOYEES table on cachedb2.
Now we see that EMPLOYEES table on cachedb2 has the record for employee Neena, and her updated phone number and salary.
|
8. |
Go back to the EMPLOYEES table in cachedb1 and check the content. In the connections navigator, select the table EMPLOYEES from the cachedb1 connection.
Note: EMPLOYEES table on cachedb1 now has only 13 records left (because Neena's employee record was shipped over to cachedb2). |
In this section, we will run a SQL script to find out the total number of employees and the highest salary among the employee records loaded into the cache grid (across all grid members).
1. |
Click on Open to bring up the Open File dialog box. Select the script 08_global_query.sql from the cache_grid_sql folder.
|
2. |
Change the SQL Worksheet connection to cachedb1 and click Run Script.
|
3. |
The Script Output pane returns the result of the SQL operation.
There are 5 operations involved in this script:
Note that, these "select count(*)" query and the "select max(salary)" query are executed on database cachedb1, but with "GlobalProcessing" enabled, they return the results reflecting both database cachedb1 and cachedb2.
|
In this topic you will learn how to detach cachedb1 and cachedb2 from the cache grid, and remove all the cache groups that were created in this tutorial.
Note that this section is to clean up the environment you have just created. You may skip this optional section.
1. |
Before global cache groups can be dropped, the database must be detached from the cache grid. Run a SQL script to detach cachedb2 from the cache grid samplegrid. Click on Open to bring up the Open File dialog box. Select the script 09_detach_cachedb.sql from the cache_grid_sql folder. Change the SQL Worksheet connection to cachedb2 and click Run Script. The Script Output pane shows that only cachedb1 is now attached to the cache grid. Close the file 09_detach_cachedb.sql by clicking on the button X.
|
2. |
To disable the propagation of AWT data from the cache tables to the Oracle database, the replication agent must be stopped. Right-click on the connection cachedb2 in the connections navigator, and select Start / Stop Replication Agent.
|
3. |
Check the Stop agent checkbox and click Apply.
|
4. |
Perform the following steps to remove the cache group from cachedb2.
|
5. |
Right click on the cache group G_AWT and select Drop. This brings up the Drop cache group dialog box.
|
6. |
Click Apply to drop the cache group. Click OK to close the confirmation dialog box.
|
7. |
Expand Tables to verify the cache tables EMPLOYEES and JOB_HISTORY have been removed. If necessary, click on the icon Refresh to refresh the connections navigator.
|
8. |
We will now stop the cache agent. Right-click on the connection cachedb2 in the connections navigator, and select Start / Stop Cache Agent.
|
9. |
Check the Stop agent check box and click Apply. This stops the cache agent on cachedb2.
|
1. |
Run a SQL script to detach cachedb1 from the cache grid samplegrid. Click on Open to bring up the Open File dialog box. Select the script 09_detach_cachedb.sql from the sql folder. Change the SQL Worksheet connection to cachedb1 and click Run Script. The Script Output pane shows that no database is now attached to the cache grid. Close the file 09_detach_cachedb.sql by clicking on the button X.
|
2. |
To disable the propagation of AWT data from the cache tables to the Oracle database, the replication agent must be stopped. Right-click on the connection cachedb1 in the connections navigator, and select Start / Stop Replication Agent.
|
3. |
Check the Stop agent checkbox and click Apply.
|
4. |
Perform the following steps to remove cache groups from cachedb1.
|
5. |
Right click on the cache group AWT and select Drop. This brings up the Drop cache group dialog box.
|
6. |
Click Apply to drop the cache group. Click OK to close the confirmation dialog box.
|
7. |
Repeat steps 5 and 6 to drop the remaining cache group G_AWT in cachedb1 .
|
8. |
Expand Tables to verify all the cache tables have been removed. If necessary, click on the icon Refresh to refresh the connections navigator.
|
9. |
We will now stop the cache agent. Right-click on the connection cachedb1 in the connections navigator, and select Start / Stop Cache Agent.
|
10. |
Check the Stop agent check box and click Apply. This stops the cache agent on cachedb1.
|
1. |
Click on Open to bring up the Open File dialog box.
|
2. |
The content of this file appears in a SQL Worksheet tab. Review the code.
|
3. |
The result from running the script is displayed in the Script Output Pane. The cache grid samplegrid has been removed.
|
4. |
Select File -> Exit to close SQL Developer. |
In this lesson, you learned how to:
![]() |
Create SQL Developer connections to TimesTen In-Memory databases | |
![]() |
Create and drop a cache grid using SQL Worksheet | |
![]() |
Create and drop different types of cache groups using SQL Developer | |
![]() |
Preload data to populate cache groups | |
![]() |
Load data on demand into dynamic cache groups | |
![]() |
Start up and shut down the cache and replication agents |
To learn more about In-Memory Database Cache you can refer to:
![]() |
Oracle In-Memory Database Product Center on OTN - http://www.oracle.com/technology/products/timesten/imdb_cache |