Before You Begin
Purpose
This tutorial describes to use SQL Tuning Advisor to tune ad-hoc SQL statements in Visual Studio. It also describes to use Oracle Performance Analyzer to tune applications using Oracle Database.Note:Oracle Performance Analyzer is best used with an Oracle Database that has very little activity from other users. Depending on the configuration of your database as well as other activity from other users you may obtain additional findings or different findings from what is shown here.
Time to Complete
Approximately 90 minutesNote:60 of these minutes are simply waiting for performance analysis to complete. During that wait you can work on another lesson.
Background
SQL Tuning Advisor and Oracle Performance Analyzer are features of Oracle Developer Tools for Visual Studio. SQL Tuning Advisor provides recommendations to improve performance of SQL statements typed into the Oracle Query Window. Oracle Performance Analyzer examines the use of the database over time by a running .NET application and gives recommendations based on the actual workload on the database.SQL Tuning Advisor requires that the user have the ADVISOR privilege as well as an Oracle Database license for the Oracle Tuning Pack and Oracle Diagnostic Pack.
Oracle Performance Analyzer requires that the user have SYSDBA privileges as well as an Oracle Database license for the Oracle Diagnostic Pack.
What Do You Need?
- Microsoft Visual Studio 2015 or later with .NET Framework 4.5 or later.
- Oracle Database 11g Release 2 or later.
- Oracle 12c Oracle Data Access Components (ODAC) with Oracle Developer Tools for Visual Studio version 12.1.0.2.4 or later.
Setting up environment
Perform the following steps to create Oracle database connections.
-
Open the Visual Studio. From the View menu, select Server Explorer.
-
In the Server Explorer, right click on Data Connections and then select Add Connection.
Note: Connect to the existing SYS schema, if you already have it there. -
The Add Connection window opens up. Provide the following values, and then select the Connect as SYSDBA role checkbox, and then click OK.
- Data Source: Oracle Database (ODP.NET, Managed Driver)
- User Name: SYS
- Password: <your sys password>
- Data source name: ORCL
-
The SYS connection is created and displayed.
-
Expand Data Connections , then right click the SYS.ORCL connection and select Privileges.
Note: The HR user must have the ADVISOR privilege to run the SQL Tuning Advisor. -
The Grant Revoke Privileges window opens up. Provide the following values and then click OK.
- Object Type: System Privileges
- User list: HR
- System privileges on HR: ADVISOR Grant
-
Similarly, you also need to create a HR connection. In the Server Explorer, right click on Data Connections and then select Add Connection.
Note: Connect to the existing HR schema, if you already have it there. -
The Add Connection window opens up. Provide the following values and then click OK.
- Data Source: Oracle Database (ODP.NET, Managed Driver)
- User Name: HR
- Password: <your HR password>
- Data source name: ORCL
-
The HR connection is created and displayed.
Using SQL Tuning Advisor to Tune AdHoc SQL
Perform the following steps to tune a SQL statement in the Query Window using the SQL Tuning Advisor.
-
Expand Data Connections. Right-click the HR.ORCL connection and select Query Window.
-
Write the following query in the Query Window and click Execute. This will create a copy of the EMPLOYEES table.
Note: You might getcreate table employees2 as select * from employees;
Warning of a NULL column in an aggregate table
message which can be safely ignored. -
Write the following query in the Query Window and click Execute. This will create a copy of the DEPARTMENTS table.
Note: You might getcreate table departments2 as select * from departments;
Warning of a NULL column in an aggregate table
message which can be safely ignored. -
Expand Data Connections, and then expand HR.ORCL, and then expand Tables, and then expand Relational Tables, to see the new tables listed.
-
Write the following query in the Query Window and click Tune. This will tune some SQL against the EMPLOYEES2 and DEPARTMENTS2 tables.
select employees2.salary from employees2, departments2 where employees2.department_id = departments2.department_id and employees2.salary <3000 union select employees.salary from employees, departments where employees.department_id = departments.department_id and employees.salary = 0
-
You will receive a dialog box asking if you have a license for the Tuning and Diagnostic Pack. Select the 'Don't show this message' again check box and click Yes.
-
In the Tune SQL window, you will get multiple INDEX findings. Select the RESTRUCTURE SQL finding and click View Report.
-
Various commands will be listed. Select only the create index commands.
-
Copy those three commands and paste it in the query window and click Execute.
-
Write the same query that you wrote in step 5 again in the Query Window and click Tune.
select employees2.salary from employees2, departments2 where employees2.department_id = departments2.department_id and employees2.salary <3000 union select employees.salary from employees, departments where employees.department_id = departments.department_id and employees.salary = 0
-
This time you should notice that the Tuning Advisor did not find any recommendations except the RESTRUCTURE SQL, which you can ignore.
Using Performance Analyzer to Analyze Application Performance
Perform the following steps to analyze the application using the Oracle Performance Analyzer and implement the recommendations it provides.
-
From the File menu, select New, and then select Project.
-
The New Project window opens up. In the left side pane, expand Installed, and then expand Templates, and then expand Visual C#, and then expand Windows. In the center pane, select Console Application. Enter oracleperformanceanalyzer as the name of the project, and then click OK.
-
The
Program.cs
file of the project opens up. -
From the Project menu, select Add Reference.
-
The Reference Manager window opens up. In the left side pane, expand Assemblies, and then expand Extensions. In the center pane, scroll down the list of assemblies and select Oracle.ManagedDataAccess and then click OK.
Note: Depending on your configuration, it may be necessary to click Browse and navigate to where Oracle.ManagedDataAccess.dll is located. -
Open the Program.cs file. Enter the following code in Program.cs file and Save.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Oracle.ManagedDataAccess.Client; using Oracle.ManagedDataAccess.Types; namespace OraclePerformanceAnalyzer { class Program { static void Main(string[] args) { string constr = "User Id=hr; Password=hr; Data Source=orcl; Max Pool Size = 50; Connection Lifetime = 1"; //string constr = "User Id=hr; Password=hr; Data Source=orcl; Max Pool Size = 50"; int sal = 3000; while (true) { sal++; GC.Collect(); GC.WaitForPendingFinalizers(); OracleConnection con = new OracleConnection(constr); con.Open(); StringBuilder sbSQL = new StringBuilder(); sbSQL.Append("select employees2.salary from employees2, departments2 where employees2.department_id = departments2.department_id and employees2.salary <3000 union select employees.salary from employees, departments where employees.department_id = departments.department_id and employees.salary = "); sbSQL.Append(sal); //sbSQL.Append(":salary"); OracleCommand cmd = new OracleCommand(); cmd.Connection = con; cmd.CommandText = sbSQL.ToString(); // OracleParameter p_salary = new OracleParameter(); //p_salary.OracleDbType = OracleDbType.Decimal; //p_salary.Value = sal; //cmd.Parameters.Add(p_salary); OracleDataReader dr = cmd.ExecuteReader(); dr.FetchSize = 10; while (dr.Read()) { } dr.Close(); } } } }
-
From the Build menu, select Build Solution.
-
From the Debug menu, select Start Without Debugging.
Note: This runs the application in a tight loop to make it easier to obtain the minimum required 'database time' for a successful analysis ('Database time' is the time that the database spends servicing the application). -
An empty command window appears (the application does not output to the window). If you receive an error, fix the error and run the application again. Leave this window open and switch back to Visual Studio.
-
Run Oracle Performance Analyzer as the SYS user. In the Server Explorer, right-click SYS.ORCL connection and select Oracle Performance Analyzer.
-
In the Diagnostic Pack Required dialog box opens. Click Yes.
Note: If you are using a multitenant container database, you must run Oracle Performance Analyzer only on the container database. If you run it on a pluggable database connection, it will throw an error similar to ORA-65040: operation not allowed from within a pluggable database. -
In order to get some recommendations, you need to run the Performance Analyzer for a significant period of time. Change the minutes to 30 and click Start. The timer starts. (While waiting for the timer to count down, you can work on another lesson).
-
When the time is up the results are displayed. Expand Performance Analysis to view the findings. You may receive these three findings (you may see additional findings depending on your database configuration and workload):
- Hard Parse Due to Literal Usage
- Session Connect and Disconnect
- Soft Parse
-
Finding #1 Hard Parse Due to Literal Usage: Investigate application logic for possible use of bind variables instead of literals. The code highlighted in the screen shot is appending a literal value to the end of the WHERE clause causing Oracle Database to have to do a hard parse for every statement. The best practice is to use a bind variable.
-
This can be corrected by commenting out the following line in the code:
sbSQL.Append(sal);
And by uncommenting the following lines:
sbSQL.Append(":salary"); OracleParameter p_salary = new OracleParameter(); p_salary.OracleDbType = OracleDbType.Decimal; p_salary.Value = sal; cmd.Parameters.Add(p_salary);
-
Finding #2 Session Connect and Disconnect: Investigate application logic for possible reduction of connect and disconnect calls. For example, you might use a connection pool scheme in the middle tier. The code highlighted in the screen shot forces the connection pool to destroy and recreate connections every second, effectively eliminating the benefit of a connection pool.
-
This can be corrected by commenting out the following line in the code:
string constr = "User Id=hr; Password=hr; Data Source=orcl; Max Pool Size = 50; Connection Lifetime = 1";
And by uncommenting the following lines:
string constr = "User Id=hr; Password=hr; Data Source=orcl; Max Pool Size = 50";
-
Close command window that opened in step 9. Rebuild the solution, and run the Oracle Performance Analyzer again for 30 minutes. Repeat steps 7 to 12.
-
When the time is up, expand Performance Analysis to view the findings. The two findings that you corrected should no longer be present.
-
The Performance Analyzer results are stored in the database as ADDM Tasks. From the Server Explorer window, expand SYS.ORCL, and then expand Schemas,and then expand SYS, and then expand ADDM Tasks. Select the ADDM task that was most recently created (at the bottom of the list). You can view the analysis you were just viewing by right-clicking the ADDM Task and select View Analysis.