Sitemap

Oracle SQL Tuning & Indexes

5 min readMar 13, 2025

--

Press enter or click to view image in full size

🎯SQL Tuning: Objective & Steps

  • Introduction to SQL Tuning:
  • SQL tuning is the attempt to diagnose & repair SQL statements that fail to meet a performance standard.
  • SQL tuning is the process of improving the performance of SQL statements in an Oracle database. The goal of SQL tuning is to make SQL queries run faster, which can help reduce the response time for users & the resources used.
  • SQL tuning is essentially the process of optimizing a database for better performance.
  • SQL tuning is the iterative process of improving SQL statement performance to meet specific, measurable, and achievable goals. SQL tuning implies fixing problems in deployed applications.

Objective of SQL Tuning:

A SQL statement becomes a problem when it fails to perform according to a measurable standard.

After you have identified the problem, a typical tuning session has one of the following goals:

Reduce user response time: decreasing the time between when a user issues a statement and receives a response

Improve throughput: using the least amount of resources necessary to process all rows accessed by a statement

Steps involved in SQL Tuning:

  • Identify High-Load SQL: Use the ADDM or AWR to identify SQL statements that are poorly performing.
  • Verify Execution Plans: Check that the query optimizer’s execution plans for high-load SQLs are reasonable.
  • Implement Corrective Actions: Generate better execution plans for poorly performing SQL statements.
  • Some ways to improve SQL Performance include: Using indexes to reduce the amount of resources used, Optimizing how SQL queries function, Reducing, Balancing, and Parallelizing the workload.

🎯SQL Profile

🟦SQL Tuning Tasks:

  • A tuning session depend on many factors, including whether you tune proactively or reactively.
  • Proactive SQL tuning, you use SQL Tuning Advisor to determine whether to make SQL statements perform better.
  • Reactive SQL tuning, you correct a SQL-related problem that a user has experienced.
  • A typical SQL tuning session involves following tasks:
  • Identifying high-load SQL statements.
  • Gathering performance-related data.
  • Determining the causes of the problem.
  • Defining the scope of the problem.
  • Implementingc corrective actions for suboptimally performing SQL statements.
  • Preventing SQL performance regressions.

🎯SQL Profile:

🟦1. Creating Tuning Task

  • DECLARE
  • l_sql_tune_task_id VARCHAR2(100);
  • BEGIN
  • l_sql_tune_task_id := DBMS_SQLTUNE.create_tuning_task (
  • sql_id => ‘d342t9u6r4jub’,
  • scope => DBMS_SQLTUNE.scope_comprehensive,
  • task_name => ‘d342t9u6r4jub_tuning_task’,
  • description => ‘Tuning task for statement d342t9u6r4jub..’);
  • DBMS_OUTPUT.put_line(‘l_sql_tune_task_id: ‘ || l_sql_tune_task_id);
  • END;

🟦2. Execute tuning task

  • EXEC DBMS_SQLTUNE.execute_tuning_task(task_name => ‘d342t9u6r4jub_tuning_task’);
  • SELECT task_name, status FROM dba_advisor_log WHERE owner = ‘SYS’
  • select owner,status,TASK_NAME from dba_advisor_log where TASK_NAME like ‘%tuning_task%’ and TASK_NAME like ‘d342t9u6r4jub_tuning_task’

Get Cloud Evolving Data’s stories in your inbox

Join Medium for free to get updates from this writer.

🟦3. Diplay Recommendations

  • SET LONG 10000000;
  • SET PAGESIZE 1000000
  • SET LINESIZE 20000
  • SELECT DBMS_SQLTUNE.report_tuning_task(‘d342t9u6r4jub_tuning_task’) AS recommendations FROM dual;
  • execute dbms_sqltune.accept_sql_profile(task_name => ‘56s1yjh2s3dw6_tuning_task’, task_owner => ‘SYS’, replace => TRUE);

🎯How to tune Index??

What is Index? An index in Oracle is a structure that provides a shortcut to a table’s rows, allowing for faster access to data.

  • B-tree Indexes: (default)
  • Primary Key Index
  • Unique Index
  • Index foreign Key columns
  • Concatenated Index
  • Function Based Index
  • Indexing a virtual coloumn
  • Creating a Bitmap Index
  • Creating an Index-Organized Table

🟦Index cardinality:

  • Consider the number of distinct values in a column when creating an index, as a high cardinality can lead to better performance.

🟦Avoiding over-indexing:

  • Creating too many indexes can negatively impact insert/update performance due to additional overhead, so carefully evaluate the need for each index.

🎯How to tune indexes in Oracle??

  • Analyze query plans:
  • Use the EXPLAIN PLAN command to analyze query execution plans and identify potential bottlenecks where indexes could improve performance.
  • Gather Statistics:
  • Regularly gather statistics on tables to ensure the optimizer has accurate information about data distribution for optimal index usage.
  • Monitor Performance:
  • Use tools like Oracle’s Automatic Workload Repository (AWR) to identify poorly performing queries and analyze index usage.
  • Re-evaluate Indexes:
  • Regularly review indexes and drop or modify them as needed based on changing query patterns.
  • Important considerations:
  • Index Maintenance:
  • Periodically rebuild or reorganize indexes to maintain optimal performance, especially after large data insertions or deletions.
  • Index-related parameters:
  • Adjust relevant database parameters like PCTFREE and PCTUSED to manage index space allocation effectively.

🎯Taxonomy of Index:

  • Index types:
  • Choose the appropriate index type based on query patterns, including:
  • B-Tree Index: The B-Tree index would be placed on columns that were frequently used in the predicate of a query and expect some small fraction of the data from the table to be returned. It is effoicient for range queries and equality searches. High cardinality.
  • Bitmap Indexes: Bitmap indexes that store pointers to many rows with a single index key entry. There will be a very small number of index entries, each of which points to many rows. Suitable for large datasets with distinct values, efficient for filtering on multiple columns. Low cardinality.
  • Function-Based Index: A function-based index calculates the result of a function that involves one or more columns and stores that result in the index.
  • Unique Index is a database constraint that ensures that no two rows in a table have the same value for a given column or columns. Ensures no duplicate values in a column
  • Primary Key is a column or group of columns in a relational database table that uniquely identifies each record in the table:
  • Foreign Key is a column or set of columns in a table that references the primary key of another table, which links the two tables together.
  • Concatenated Index, also known as a multi-column, composite, or combined index, is an index that spans multiple columns in a database table:
  • Index Organized Tables (IOT) have their primary key data and non-key column data stored within the same B*Tree structure. Effectively, the data is stored within the primary key index.
  • Virtual Index is a “fake” index whose definition exists in the data dictionary but has no associated index segment.

--

--