SQL Tuning Guide - pt6
SQL Tuning Guide - pt6
The following query of the DBA_TAB_COL_STATISTICS table shows information about statistics
that have been gathered on the columns cust_state_province and country_id from the
sh.customers table:
SELECT COUNT(*)
FROM sh.customers
WHERE cust_state_province = 'CA';
COUNT(*)
----------
3341
Consider an explain plan for a query of customers in the state CA and in the country with ID
52790 (USA):
Explained.
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------
Plan hash value: 1683234692
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 128 | 24192 | 442 (7)| 00:00:06 |
|* 1 | TABLE ACCESS FULL| CUSTOMERS | 128 | 24192 | 442 (7)| 00:00:06 |
--------------------------------------------------------------------------
14-3
Chapter 14
Managing Column Group Statistics
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
---
13 rows selected.
See Also:
When you want to manage column group statistics manually, then use DBMS_STATS as
follows:
• Detect column groups
• Create previously detected column groups
• Create column groups manually and gather column group statistics
14-4
Chapter 14
Managing Column Group Statistics
See Also:
14-5
Chapter 14
Managing Column Group Statistics
See Also:
Assumptions
This tutorial assumes the following:
• Cardinality estimates have been incorrect for queries of the sh.customers_test
table (created from the customers table) that use predicates referencing the
columns country_id and cust_state_province.
• You want the database to monitor your workload for 5 minutes (300 seconds).
• You want the database to determine which column groups are needed
automatically.
BEGIN
DBMS_STATS.SEED_COL_USAGE(null,null,300);
END;
/
4. As user sh, run explain plans for two queries in the workload.
The following examples show the explain plans for two queries on the
customers_test table:
14-6
Chapter 14
Managing Column Group Statistics
FROM customers_test
WHERE cust_city = 'Los Angeles'
AND cust_state_province = 'CA'
AND country_id = 52790;
SELECT PLAN_TABLE_OUTPUT
FROM TABLE(DBMS_XPLAN.DISPLAY('plan_table', null,'basic rows'));
SELECT PLAN_TABLE_OUTPUT
FROM TABLE(DBMS_XPLAN.DISPLAY('plan_table', null,'basic rows'));
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
Plan hash value: 4115398853
----------------------------------------------------
| Id | Operation | Name | Rows |
----------------------------------------------------
| 0 | SELECT STATEMENT | | 1 |
| 1 | TABLE ACCESS FULL| CUSTOMERS_TEST | 1 |
----------------------------------------------------
8 rows selected.
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------
Plan hash value: 3050654408
-----------------------------------------------------
| Id | Operation | Name | Rows |
-----------------------------------------------------
| 0 | SELECT STATEMENT | | 1949 |
| 1 | HASH GROUP BY | | 1949 |
| 2 | TABLE ACCESS FULL| CUSTOMERS_TEST | 55500 |
-----------------------------------------------------
9 rows selected.
The first plan shows a cardinality of 1 row for a query that returns 932 rows. The second
plan shows a cardinality of 1949 rows for a query that returns 145 rows.
5. Optionally, review the column usage information recorded for the table.
Call the DBMS_STATS.REPORT_COL_USAGE function to generate a report:
14-7
Chapter 14
Managing Column Group Statistics
LEGEND:
.......
####################################################################
####
1. COUNTRY_ID : EQ
2. CUST_CITY : EQ
3. CUST_STATE_PROVINCE : EQ
4. (CUST_CITY, CUST_STATE_PROVINCE,
COUNTRY_ID) : FILTER
5. (CUST_STATE_PROVINCE, COUNTRY_ID) : GROUP_BY
####################################################################
####
In the preceding report, the first three columns were used in equality predicates in
the first monitored query:
...
WHERE cust_city = 'Los Angeles'
AND cust_state_province = 'CA'
AND country_id = 52790;
All three columns appeared in the same WHERE clause, so the report shows them
as a group filter. In the second query, two columns appeared in the GROUP BY
clause, so the report labels them as GROUP_BY. The sets of columns in the FILTER
and GROUP_BY report are candidates for column groups.
14-8