Tuning .NET Applications in Visual Studio with SQL Tuning Advisor and Oracle Performance Analyzer


Options



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 minutes

Note: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.

  1. Open the Visual Studio. From the View menu, select Server Explorer.

    Tuning DotNet Applications
    Description of this image
  2. In the Server Explorer, right click on Data Connections and then select Add Connection.

    Tuning DotNet Applications
    Description of this image
    Note: Connect to the existing SYS schema, if you already have it there.
  3. 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
    Tuning DotNet Applications
    Description of this image
  4. The SYS connection is created and displayed.

    Tuning DotNet Applications
    Description of this image
  5. Expand Data Connections , then right click the SYS.ORCL connection and select Privileges.

    Tuning DotNet Applications
    Description of this image
    Note: The HR user must have the ADVISOR privilege to run the SQL Tuning Advisor.
  6. 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
    Tuning DotNet Applications
    Description of this image
  7. Similarly, you also need to create a HR connection. In the Server Explorer, right click on Data Connections and then select Add Connection.

    Tuning DotNet Applications
    Description of this image
    Note: Connect to the existing HR schema, if you already have it there.
  8. 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
    Tuning DotNet Applications
    Description of this image
  9. The HR connection is created and displayed.

    Tuning DotNet Applications
    Description of this image

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.

  1. Expand Data Connections. Right-click the HR.ORCL connection and select Query Window.

    Tuning DotNet Applications
    Description of this image
  2. Write the following query in the Query Window and click Execute. This will create a copy of the EMPLOYEES table.

    create table employees2 as select * from employees;
    Tuning DotNet Applications
    Description of this image
    Note: You might get Warning of a NULL column in an aggregate table message which can be safely ignored.
  3. Write the following query in the Query Window and click Execute. This will create a copy of the DEPARTMENTS table.

    create table departments2 as select * from departments;
    Tuning DotNet Applications
    Description of this image
    Note: You might get Warning of a NULL column in an aggregate table message which can be safely ignored.
  4. Expand Data Connections, and then expand HR.ORCL, and then expand Tables, and then expand Relational Tables, to see the new tables listed.

    Tuning DotNet Applications
    Description of this image
  5. 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


    Tuning DotNet Applications
    Description of this image
  6. 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.

    Tuning DotNet Applications
    Description of this image
  7. In the Tune SQL window, you will get multiple INDEX findings. Select the RESTRUCTURE SQL finding and click View Report.

    Tuning DotNet Applications
    Description of this image
  8. Various commands will be listed. Select only the create index commands.

    Tuning DotNet Applications
    Description of this image
  9. Copy those three commands and paste it in the query window and click Execute.

    Tuning DotNet Applications
    Description of this image
  10. 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


    Tuning DotNet Applications
    Description of this image
  11. This time you should notice that the Tuning Advisor did not find any recommendations except the RESTRUCTURE SQL, which you can ignore.

    Tuning DotNet Applications
    Description of this image

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.

  1. From the File menu, select New, and then select Project.

    Tuning DotNet Applications
    Description of this image
  2. 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.

    Tuning DotNet Applications
    Description of this image
  3. The Program.cs file of the project opens up.

    Tuning DotNet Applications
    Description of this image
  4. From the Project menu, select Add Reference.

    Tuning DotNet Applications
    Description of this image
  5. 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.

    Tuning DotNet Applications
    Description of this image
    Note: Depending on your configuration, it may be necessary to click Browse and navigate to where Oracle.ManagedDataAccess.dll is located.
  6. 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();
                }
            }
        }
    }


    Tuning DotNet Applications
    Description of this image
  7. From the Build menu, select Build Solution.

    Tuning DotNet Applications
    Description of this image
  8. From the Debug menu, select Start Without Debugging.

    Tuning DotNet Applications
    Description of this image
    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).
  9. 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.

    Tuning DotNet Applications
    Description of this image
  10. Run Oracle Performance Analyzer as the SYS user. In the Server Explorer, right-click SYS.ORCL connection and select Oracle Performance Analyzer.

    Tuning DotNet Applications
    Description of this image
  11. In the Diagnostic Pack Required dialog box opens. Click Yes.

    Tuning DotNet Applications
    Description of this image
    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.
  12. 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).

    Tuning DotNet Applications
    Description of this image
  13. 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):

    1. Hard Parse Due to Literal Usage
    2. Session Connect and Disconnect
    3. Soft Parse
    Tuning DotNet Applications
    Description of this image
  14. 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.

    Tuning DotNet Applications
    Description of this image
  15. 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);


    Tuning DotNet Applications
    Description of this image
  16. 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.

    Tuning DotNet Applications
    Description of this image
  17. 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";
    Tuning DotNet Applications
    Description of this image
  18. 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.

  19. When the time is up, expand Performance Analysis to view the findings. The two findings that you corrected should no longer be present.

    Tuning DotNet Applications
    Description of this image
  20. 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.

    Tuning DotNet Applications
    Description of this image

Want to Learn More?