In this tutorial, you analyze and execute C programs in several stages to learn about the features and functionality of OCI.
You learn how to create and use reports (ADDM reports - Automatic Database Diagnostic Monitor and AWR reports - Automatic Workload Repository) to analyze the performance and bottleneck of a sample application. As the reports are run, you analyze and refine the sample in several stages. The end result is an efficient application.
Approximately 60 minutes.
Before you perform this tutorial, you should:
1. | Install Oracle Database 11g Release 2 (if not already present) |
|
2. | Install the ODBC driver. |
|
3. | Download and unzip the ocilab.zip file into your working directory (i.e./home/oci/ocilab ) |
In this tutorial you use a step by step approach to run and tune the application in several stages. Starting with non-optimized code, each stage adds up to this non-optimized stage showing the performance improvement by using features like statement caching, client side result set caching, etc. A few stages show specific features like LOB handling, and fetching through a ref-cursor.
The AWR and ADDM Reports
Automatic Workload Repository (AWR) is a built-in repository in every Oracle Database. At regular intervals, the Oracle Database makes a snapshot of all its important statistics and workload information and stores in AWR.
The Automatic Database Diagnostic Monitor (ADDM) enables the Oracle Database to diagnose its own performance and determine how identified problems could be resolved. It runs automatically after each AWR statistics capture, making the performance diagnostic data readily available.
To run the reports, you can use the reports.sql script provided with this tutorial. For each of the stages that you want to examine, the executable generated from corresponding C file is provided to the script (reports.sql) with a few additional parameters.
OCI Overview
For OCI analysis, the C source code files are name as stage1.c stage2.c stage3.c stage4.c stage5.c, stage6.c, and stage7.c with a common library file named helper.c that is used for all of the stages shown. These files are stored in the /home/oci/ocilab location.
In this section, you examine 7 stages of an OCI application. Stages 2, 3, 4, 5 illustrate performance related OCI features, while stage6 and stage7 illustrate PLSQL and (secure) Lob support in OCI. By generating the ADDM and AWR reports on the sample applications, you can analyze performance and bottlenecks. These reports are done for stages 1,2,3,4, and 5.
Each stage builds on the code changes done in previous stage. To see the code changes, you use the tkdiff utility to view a stage compared with the previous stage.
The Sample Application
The sample application uses the ocihol/welcome schema. It generates random numbers to simulate web inputs to update and query the MYEMP table.
The following objects are defined within the ocihol schema:
The MYEMP table is defined as:
CREATE TABLE myemp ( empno number(4) primary key, ename varchar2(10), job varchar2(9), mgr number(4), hiredate date, sal number(7,2), comm number(7,2), deptno number(2)) ;To demonstrate client result caching, besides above table, following objects are used:
CREATE TABLE regions ( region_id NUMBER CONSTRAINT region_id_nn NOT NULL, region_name varchar2(25)) ; CREATE OR REPLACE VIEW empview as select * from myemp ;
To demonstrate the LOB features, the following table is defined:
CREATE TABLE lob_table ( lobid number primary key, lobcol clob) LOB(lobcol) STORE AS SECUREFILE (TABLESPACE SYSAUX COMPRESS low CACHE) ;
The following three procedures are used in the OCI application:
The main C functions are:
To compile and link all of the stages of the source code, you can use the make utility.
In this section, no optimizations are performed. The main thread spawns the requested number of threads with a thread function (thread_function()) as an initial routine that each thread should be started with. The 'main' thread, spawns new threads.
Each thread does the following:
. |
Open a terminal window. Change to your /home/oci/ocilab directory and start SQL*Plus. Logon as system. Use the password "manager". Examine the contents for the stage1.c file. You can use a text editor to view the contents. Note: If you are working in a Linux environment, you may need to change the permission to execute the stagen files. To do so, execute the following at the command prompt: chmod 777 stage* exit |
---|---|
. |
Use the gmake utility to build the executable C programs and library for the source code for stage1. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility cd $HOME/oci/ocilab gmake stage1
|
. |
The reports.sql script is stored in the $HOME/oci/ocilab/sql directory. In a new terminal session, change to the $HOME/oci/ocilab/sql directory. cd $HOME/oci/ocilab/sql Start SQL*Plus. In your SQL*Plus session, run the reports.sql script. Pass to it the stage1 file. Add the parameter to iterate 10 times to increase the workload and generate findings. (Note: we have purposefully set the number of iterations to a low value of 10 to save time in this exercise. For the pre-canned reports generated for this stage, the number of iterations was set to 500.) @reports.sql "../stage1 -i 10" As the report runs, you are shown the percentage completed.
|
. |
When prompted for the report name, enter:
stage1_oci
This generates two reports which are saved in the current directory:
Generated sample results are provided to you for each stage. These files are located in the /home/oci/ocilab/doc sub-folder.. The names of the sample results files are:
Note: Your results may differ from the sample results provided due to environment differences. For consistency in this tutorial, the generated sample results are examined.
|
. |
Examine the contents in the stage1_results.txt report. (You can either open the file in a text editor, or you can scroll up in your SQL*Plus session.) Note the summary of findings.
|
. |
Examine the contents in the stage1_results_awr.htm report. Find the file stage1_results_awr.htm. Double-click the file name to open the file in a browser (or right-click and select the Open with "Web-Browser" option). Review the report . Scroll down to the Main Report. Click the SQL Statistics link. Note the Elapsed time statistics and number of SQL executions. As you go through each of the stages and apply different tuning methods, these numbers will change (and improve).
|
In this section, the C code is optimized for array fetch and array DML. The multi-row fetch is performed using an array, which can hold an ARRAY_SIZE number of rows for each fetch.
Note: The example uses regular forward scrolling cursor. OCI also supports readonly scrollable cursor that allows fetches in any direction (backward, forward) with random access. The result set is based on snapshot at query execution time. You have to set a special mode in OCIStmtExecute() to mark the query result-set as scrollable. Then use OCIStmtFetch2() to scroll the results. See OCI documentation for details.
. |
Examine the contents for the stage2.c file and compare it to stage1.c . You can use the tkdiff utility to view the differences. Execute the following command to view the stage2.c and stage1.c files. tkdiff stage1.c stage2.c Note the differences in the files. In stage2.c array DML and array fetch is used. I
|
---|---|
. |
Use the gmake utility to build the executable C programs and library for the source code for stage2. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility. cd $HOME/oci/ocilab gmake stage2
|
. |
In your SQL*Plus session, run the reports.sql script. Pass to it the stage2 file. Add the parameter to iterate 500 times to increase the workload and generate findings. @reports.sql "../stage2 -i 500" As the report runs, you are shown the percentage completed.
|
. |
When prompted for the report name, enter:
stage2_oci
This generates two reports which are saved in the current directory:
Generated sample results are provided to you for each stage. These files are located in the /home/oci/ocilab/doc sub-folder.. The names of the sample results files are:
Note: Your results may differ from the sample results provided due to environment differences. For consistency in this tutorial, the generated sample results are examined.
|
. |
Examine the contents in the stage2_results.txt report. (You can either open the file in a text editor, or you can scroll up in your SQL*Plus session.) Note the summary of findings. Note that Finding 2 recommends to use mid-tier connection pooling. This optimization is done in stage 3.
|
. |
Examine the contents in the stage2_results_awr.htm report. Find the file stage2_results_awr.htm. Double-click the file name to open the file in a browser (or right-click and select the Open with "Web-Browser" option). Review the report. Scroll down to the Main Report. Click the SQL Statistics link. Note the Elapsed time statistics and number of SQL executions. As you go through each of the stages and apply different tuning methods, these numbers will change (and improve).
|
This stage is same as stage2, but with OCI session pooling enabled. This is achieved by using the following configurations options set dynamically using the OCISessionPooling APIs in new method create_session_pool().
In the thread_function(), when each thread get a session per iteration, it comes from the pool instead of creating and destroying sessions (and hence connections) per iteration per thread.
Notes:
. |
Examine the contents for the stage3.c file and compare it to stage2.c. You can use the tkdiff utility to view the differences. Execute the following command to view the stage3.c and stage2.c files. tkdiff stage2.c stage3.c Note the differences in the files. In stage3.c OCI session pooling is used. Note: If you are working in a Linux environment, you may need to change the permission to execute the stagen files. To do so, execute the following at the command prompt: chmod 777 stage* exit |
---|---|
. |
Use the gmake utility to build the executable C programs and library for the source code for stage3. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility cd $HOME/oci/ocilab gmake stage3
|
. |
In your SQL*Plus session, run the reports.sql script. Pass to it the stage3 file. Add the parameter to iterate 1000 times to increase the workload and generate findings. @reports.sql "../stage3 -i 1000 As the report runs, you are shown the percentage completed.
|
. |
When prompted for the report name, enter:
stage3_oci
This generates two reports which are saved in the current directory:
Generated sample results are provided to you for each stage. These files are located in the /home/oci/ocilab/doc sub-folder.. The names of the sample results files are:
Note: Your results may differ from the sample results provided due to environment differences. For consistency in this tutorial, the generated sample results are examined.
|
. |
Examine the contents in the stage3_results.txt report. (You can either open the file in a text editor, or you can scroll up in your SQL*Plus session.) Note the summary of findings. In the stage 2, Finding 2 was "Session Connect and Disconnect" - a recommendation to reduce the connects and disconnects. In stage 3, you do not see the recommendation to reduce connects and disconnects (i.e. connection pooling) now.
|
. |
Examine the contents in the stage3_results_awr.htm report. Find the file stage3_results_awr.htm. Double-click the file name to open the file in a browser (or right-click and select the Open with "Web-Browser" option). Review the report . Scroll down to the Main Report. Click the SQL Statistics link. Note the Elapsed time statistics. As you go through each of the stages and apply different tuning methods, these numbers will change (and improve).
|
This stage is same as stage3, but with client-side statement caching enabled. This is achieved by setting statement cache size on the OCI Session Pool and indicating using this feature in the OCISessionPoolCreate() call.
When the call to OCIStmtPrepare2() is done, it checks to see if that SQL text exists in the client cache. There is one cache per client session. If no match is found, it goes to server to make a (soft) parse call and caches this statement. The next call to OCIStmtPrepare2() on this same session (by the same thread in our application) will re-use this already parsed statement. This saves soft parses at the server.
. |
Examine the contents for the stage4.c file and compare it to stage3.c. You can use the tkdiff utility to view the differences. Execute the following command to view the stage4.c and stage3.c files. tkdiff stage3.c stage4.c Note the differences in the files. In stage4.c OCI statement caching is enabled. Note: If you are working in a Linux environment, you may need to change the permission to execute the stagen files. To do so, execute the following at the command prompt: chmod 777 stage* exit |
---|---|
. |
Use the gmake utility to build the executable C programs and library for the source code for stage4. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility cd $HOME/oci/ocilab gmake stage4
|
. |
In your SQL*Plus session, run the reports.sql script. Pass to it the stage4 file. Add the parameter to iterate 1000 times to increase the workload and generate findings. @reports.sql "../stage4 -i 1000" As the report runs, you are shown the percentage completed.
|
. |
When prompted for the report name, enter:
stage4_oci
This generates two reports which are saved in the current directory:
Generated sample results are provided to you for each stage. These files are located in the /home/oci/ocilab/doc sub-folder.. The names of the sample results files are:
Note: Your results may differ from the sample results provided due to environment differences. For consistency in this tutorial, the generated sample results are examined.
|
. |
Examine the contents in the stage4_results.txt report. (You can either open the file in a text editor, or you can scroll up in your SQL*Plus session.) Note the summary of findings.
|
. |
Examine the contents in the stage4_results_awr.htm report. Compare the parse calls in AWR with previous stage and see that it has decreased a lot. This is due to client-side statement caching. Find the file stage4_results_awr.htm. Double-click the file name to open the file in a browser (or right-click and select the Open with "Web-Browser" option). Review the report . Scroll down to the Main Report. Click the SQL Statistics link. Note the Elapsed time statistics. As you go through each of the stages and apply different tuning methods, these numbers will change (and improve).
|
This stage is same as stage4, but with client result set caching enabled. This is achieved by:
Notes:
. |
You need to set up the regions table for result cache caching using the alter table DDL statement. The stage5.sql script contains the following statements: -- STAGE 5 In a SQL*Plus session, connect as the ocihol/welcome user and run the stage5.sql script: CONNECT ocihol/welcome @stage5.sql
|
---|---|
. |
Examine the contents for the stage5.c file and compare it to stage4.c. You can use the tkdiff utility to view the differences. Execute the following command to view the stage5.c and stage4.c files. tkdiff stage4.c stage5.c Note the differences in the files. In the stage5.c file, OCI client result-set caching is enabled. Note: If you are working in a Linux environment, you may need to change the permission to execute the stagen files. To do so, execute the following at the command prompt: chmod 777 stage* exit |
. |
Use the gmake utility to build the executable C programs and library for the source code for stage5. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility cd $HOME/oci/ocilab gmake stage5
|
. |
In your SQL*Plus session, run the reports.sql script. Pass to it the stage5 file. Add the parameter to iterate 1000 times to increase the workload and generate findings. @reports.sql "../stage5 -i 1000" As the report runs, you are shown the percentage completed.
|
. |
When prompted for the report name, enter:
stage5_oci
This generates two reports which are saved in the current directory:
Generated sample results are provided to you for each stage. These files are located in the /home/oci/ocilab/doc sub-folder.. The names of the sample results files are:
Note: Your results may differ from the sample results provided due to environment differences. For consistency in this tutorial, the generated sample results are examined.
|
. |
Examine the contents in the stage5_results.txt report. (You can either open the file in a text editor, or you can scroll up in your SQL*Plus session.) Note the summary of findings.
|
. |
Examine the contents in the stage5_results_awr.htm report. Find the file stage5_results_awr.htm. Double-click the file name to open the file in a browser (or right-click and select the Open with "Web-Browser" option). Review the report . Scroll down to the Main Report. Click the SQL Statistics link. Note in the AWR section 'SQL Ordered by Executions' , the less number of executions to server for
and ii. Query using result cache table annotation:
Instead of 20 threads * 1000 iterations = 20,000 executions to server, both the above queries show much less executions to server. This will help SQL net traffic and CPU consumption.
|
In this stage, we illustrate support for PLSQL calls within OCI. The stored procedures are defined in the createtables.sql script and used by below new C methods added in this stage.
Note that this ref cursor returned back to the OCI client is like an executed OCI statement handle. You can define and start fetching rows. It calls multirow_fetch_from_emp() to get the rows which is same method used by previous stages to fetch rows using SQL queries.
The OCI prefetching is also available when fetching from a PLSQL ref cursor.
. |
Examine the contents for the stage6.c file and compare it to stage5.c. You can use the tkdiff utility to view the differences. Execute the following command to view the stage6.c and stage5.c files. tkdiff stage5.c stage6.c Note the differences in the files. In the stage6.c file, a PL/SQL REF cursor is used with prefetching. Note: If you are working in a Linux environment, you may need to change the permission to execute the stagen files. To do so, execute the following at the command prompt: chmod 777 stage* exit |
---|---|
. |
Use the gmake utility to build the executable C programs and library for the source code for stage6. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility cd $HOME/oci/ocilab gmake stage6
|
. |
In a terminal window, execute the stage6 file. ./stage6 -i 10 Note: No AWR and ADDM reports are required for the stage6 example.
|
In this stage we demonstrate SecureFile LOBs (the next generation LOBs) with these scenarios:
The lob_table has these properties:
-- STAGE 7 -- creating a table to show LOB usage
DROP TABLE lob_table; create table lob_table (lobid number primary key, lobcol clob)
LOB(lobcol) STORE AS SECUREFILE (TABLESPACE SYSAUX
COMPRESS low
CACHE );
. |
Examine the contents for the stage7.c file and compare it to stage6.c. You can use the tkdiff utility to view the differences. Execute the following command to view the stage7.c and stage6.c files. tkdiff stage6.c stage7.c Note the differences in the files. In the stage7.c file, OCI SecureFile LOB access is demonstrated. Note: If you are working in a Linux environment, you may need to change the permission to execute the stagen files. To do so, execute the following at the command prompt: chmod 777 stage* exit |
---|---|
. |
Use the gmake utility to build the executable C programs and library for the source code for stage6. Start a terminal window, navigate (by using the 'cd' command) to the location where the stage files are (/home/oci/ocilab) and execute the gmake utility cd $HOME/oci/ocilab gmake stage7
|
. |
In a terminal window, execute the stage7 file. ./stage7 -i 10 Note: No AWR and ADDM reports are required for the stage7 example.
|
In this tutorial, you learned how to:
About Oracle |Oracle and Sun | | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Your Privacy Rights | |