| re: -802 error for count(*) query for a DB2 table
C:\Users\vijay>db2 describe indexes for table db2admin.columns
Index Index Unique Number of
schema name rule columns
-------------- ------------------- -------------- --------------
0 record(s) selected.
explain plan without any indexes :
C:\Program Files\IBM\SQLLIB\MISC>db2expln -d paulb001 -i -t -g -q "select count(*) from db2admin.columns" -noenv
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
******************** DYNAMIC ***************************************
==================== STATEMENT ==========================================
Isolation Level = Cursor Stability
Blocking = Block Unambiguous Cursors
Query Optimization Class = 5
Partition Parallel = No
Intra-Partition Parallel = No
SQL Path = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
"VIJAY"
Statement:
select count(*)
from db2admin.columns
Section Code Page = 1252
Estimated Cost = 71.966728
Estimated Cardinality = 1.000000
( 3) Access Table Name = DB2ADMIN.COLUMNS ID = 2,4
| #Columns = 0
| Relation Scan
| | Prefetch: Eligible
| Lock Intents
| | Table: Intent Share
| | Row : Next Key Share
| Sargable Predicate(s)
( 3) | | Predicate Aggregation
| | | Column Function(s)
( 2) Aggregation Completion
| Column Function(s)
( 1) Return Data to Application
| #Columns = 1
End of section
Optimizer Plan:
RETURN
( 1)
|
GRPBY
( 2)
|
TBSCAN
( 3)
|
Table:
DB2ADMIN
COLUMNS
well as expected its a table scan, should be. :)
C:\vijay>db2 create index idx01 on db2admin.columns(length)
DB20000I The SQL command completed successfully.
C:\vijay>db2 runstats on table db2admin.columns with distribution and detailed indexes all
DB20000I The RUNSTATS command completed successfully.
Explain plan after creating the index :
C:\vijay>db2expln -d paulb001 -i -t -g -q "select count(*) from db2admin.columns" -noenv
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
******************** DYNAMIC ***************************************
==================== STATEMENT ==========================================
Isolation Level = Cursor Stability
Blocking = Block Unambiguous Cursors
Query Optimization Class = 5
Partition Parallel = No
Intra-Partition Parallel = No
SQL Path = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
"VIJAY"
Statement:
select count(*)
from db2admin.columns
Section Code Page = 1252
Estimated Cost = 23.113621
Estimated Cardinality = 1.000000
( 3) Access Table Name = DB2ADMIN.COLUMNS ID = 2,4
| Index Scan: Name = VIJAY.IDX01 ID = 1
| | Regular Index (Not Clustered)
| | Index Columns:
| | | 1: LENGTH (Ascending)
| #Columns = 0
| #Key Columns = 0
| | Start Key: Beginning of Index
| | Stop Key: End of Index
| Index-Only Access
| Index Prefetch: None
| Lock Intents
| | Table: Intent Share
| | Row : Next Key Share
| Sargable Index Predicate(s)
( 3) | | Predicate Aggregation
| | | Column Function(s)
( 2) Aggregation Completion
| Column Function(s)
( 1) Return Data to Application
| #Columns = 1
End of section
Optimizer Plan:
RETURN
( 1)
|
GRPBY
( 2)
|
IXSCAN
( 3)
/ \
Index: Table:
VIJAY DB2ADMIN
IDX01 COLUMNS
You can see udb optimizer is smart enough to pick up the desired index. To explain further :
C:\vijay>db2expln -d paulb001 -i -t -g -q "select count(length) from db2admin.columns" -noenv
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
******************** DYNAMIC ***************************************
==================== STATEMENT ==========================================
Isolation Level = Cursor Stability
Blocking = Block Unambiguous Cursors
Query Optimization Class = 5
Partition Parallel = No
Intra-Partition Parallel = No
SQL Path = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
"VIJAY"
Statement:
select count(length)
from db2admin.columns
Section Code Page = 1252
Estimated Cost = 23.113621
Estimated Cardinality = 1.000000
( 3) Access Table Name = DB2ADMIN.COLUMNS ID = 2,4
| Index Scan: Name = VIJAY.IDX01 ID = 1
| | Regular Index (Not Clustered)
| | Index Columns:
| | | 1: LENGTH (Ascending)
| #Columns = 0
| #Key Columns = 0
| | Start Key: Beginning of Index
| | Stop Key: End of Index
| Index-Only Access
| Index Prefetch: None
| Lock Intents
| | Table: Intent Share
| | Row : Next Key Share
| Sargable Index Predicate(s)
( 3) | | Predicate Aggregation
| | | Column Function(s)
( 2) Aggregation Completion
| Column Function(s)
( 1) Return Data to Application
| #Columns = 1
End of section
Optimizer Plan:
RETURN
( 1)
|
GRPBY
( 2)
|
IXSCAN
( 3)
/ \
Index: Table:
VIJAY DB2ADMIN
IDX01 COLUMNS
C:\vijay>db2expln -d paulb001 -i -t -g -q "select count(scale) from db2admin.columns" -noenv
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
DB2 Universal Database Version 9.1, 5622-044 (c) Copyright IBM Corp. 1991, 2006
Licensed Material - Program Property of IBM
IBM DB2 Universal Database SQL and XQUERY Explain Tool
******************** DYNAMIC ***************************************
==================== STATEMENT ==========================================
Isolation Level = Cursor Stability
Blocking = Block Unambiguous Cursors
Query Optimization Class = 5
Partition Parallel = No
Intra-Partition Parallel = No
SQL Path = "SYSIBM", "SYSFUN", "SYSPROC", "SYSIBMADM",
"VIJAY"
Statement:
select count(scale)
from db2admin.columns
Section Code Page = 1252
Estimated Cost = 23.113621
Estimated Cardinality = 1.000000
( 3) Access Table Name = DB2ADMIN.COLUMNS ID = 2,4
| Index Scan: Name = VIJAY.IDX01 ID = 1
| | Regular Index (Not Clustered)
| | Index Columns:
| | | 1: LENGTH (Ascending)
| #Columns = 0
| #Key Columns = 0
| | Start Key: Beginning of Index
| | Stop Key: End of Index
| Index-Only Access
| Index Prefetch: None
| Lock Intents
| | Table: Intent Share
| | Row : Next Key Share
| Sargable Index Predicate(s)
( 3) | | Predicate Aggregation
| | | Column Function(s)
( 2) Aggregation Completion
| Column Function(s)
( 1) Return Data to Application
| #Columns = 1
End of section
Optimizer Plan:
RETURN
( 1)
|
GRPBY
( 2)
|
IXSCAN
( 3)
/ \
Index: Table:
VIJAY DB2ADMIN
IDX01 COLUMNS
In all case it will use the index that best suits the query.
Cheers, Vijay
|