If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > is null very slow on DB2

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-04-11, 12:48
Breako Breako is offline
Registered User
 
Join Date: Jan 2006
Posts: 119
is null very slow on DB2

hi,
I have a table where two columns are a compound index. When I use "is null" a table scan is performed.

SELECT * FROM MYTABLE WHERE COL_A is null AND COL_B is null;

whereas

SELECT * FROM MYTABLE WHERE COL_A = 0 AND COL_B =0;

performs no table scan.

In my table COL_A and COL_B together form an index.

Any ideas?

Thanks
Reply With Quote
  #2 (permalink)  
Old 08-04-11, 12:57
Marcus_A Marcus_A is offline
Registered User
 
Join Date: May 2003
Location: USA
Posts: 5,196
If there are a lot of rows where the values are null, DB2 may find it faster to do a table scan of the entire table. I assume you have done a runstats on the table.
__________________
M. A. Feldman
IBM Certified DBA on DB2 for Linux, UNIX, and Windows
IBM Certified DBA on DB2 for z/OS and OS/390
Reply With Quote
  #3 (permalink)  
Old 08-04-11, 14:12
Peter.Vanroose Peter.Vanroose is offline
Registered User
 
Join Date: Sep 2004
Location: Belgium
Posts: 1,079
Without runstats, the two queries should have identical access paths.
__________________
--_Peter Vanroose,
__IBM Certified Database Administrator, DB2 9 for z/OS
__IBM Certified Application Developer
__ABIS Training and Consulting
__http://www.abis.be/
Reply With Quote
  #4 (permalink)  
Old 08-04-11, 19:38
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
How about this?
Code:
SELECT *
 FROM  MYTABLE
 WHERE COL_A is null AND COL_B is null
   OR  COL_A = 0 AND COL_A <> 0
   AND COL_B = 0 AND COL_B <> 0
Reply With Quote
  #5 (permalink)  
Old 08-04-11, 20:54
tonkuma tonkuma is offline
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 2,193
I did a small test.

I couldn't recreate the issue(tablespace scan) after runstats.

Sorry, I misunderstood the result.
"col_name is null for Not null column"
was optimized using not null constraints.
DB2 generates the answer without reading the actual table.


Summary of results: Executed after runstats.
Code:
                    +--------------------------------------------+
                    |       column attributes                    |
+-------------------+----------+-----------------+---------------+
| predicates        | Not null | (1 / 1000) null | All rows null |
+-------------------+----------+-----------------+---------------+
| col_name is null  | <genrow> | <ixscan>        | <ixscan>      |
|                   | Ex. 1    | Ex. 3           | Ex. 4         |
+-------------------+----------+-----------------+---------------+
| col_name = 1      | <ixscan> | -               | -             |
|                   | Ex. 2    |                 |               |
+-------------------+----------+-----------------+---------------+
Test table and data:
Code:
------------------------------ Commands Entered ------------------------------
CREATE TABLE
 test_index_nullvalue
( id       INTEGER NOT NULL
, col_int  INTEGER
, col_null INTEGER
)
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

/*
 about 30 secs.
*/
------------------------------ Commands Entered ------------------------------
INSERT INTO
test_index_nullvalue
WITH
 recurse(id , col_a , col_null) AS (
VALUES (1 , 1 , NULLIF(0 , 0) )
UNION ALL
SELECT id + 1
     , NULLIF( MOD(id + 1 , 1000) , 500 )
     , col_null
 FROM  recurse
 WHERE id < 1000000
)
SELECT * FROM recurse
;
------------------------------------------------------------------------------
DB20000I  The SQL command completed successfully.

------------------------------ Commands Entered ------------------------------
SELECT COUNT(*)        count_rows
     , COUNT(id)       count_id
     , COUNT(col_int)  cnt_col_int
     , COUNT(DISTINCT
             col_int)  distinct_int
     , COUNT(col_null) cnt_col_null
 FROM  test_index_nullvalue
;
------------------------------------------------------------------------------

COUNT_ROWS  COUNT_ID    CNT_COL_INT DISTINCT_INT CNT_COL_NULL
----------- ----------- ----------- ------------ ------------
    1000000     1000000      999000          999            0

  1 record(s) selected.
Add Indexes and Runstats:
CREATE INDEX test_index_nullvalue@_id ON test_index_nullvalue (id);
CREATE INDEX test_index_nullvalue@_col_int ON test_index_nullvalue (col_int);
CREATE INDEX test_index_nullvalue@_col_null ON test_index_nullvalue (col_null);
RUNSTATS ON TABLE db2admin.test_index_nullvalue FOR INDEXES ALL;

Result of runstats:
Code:
INDEX_NAME                     NLEAF       NLEVELS FIRSTKEYCARD FULLKEYCARD SEQUENTIAL_PAGES DENSITY     INDCARD     CLUSTERFACTOR AVGNLEAFKEYSIZE AVGLEAFKEYSIZE TABLE_NAME                    
------------------------------ ----------- ------- ------------ ----------- ---------------- ----------- ----------- ------------- --------------- -------------- ------------------------------
TEST_INDEX_NULLVALUE@_ID              2062       3      1000000     1000000             2061          98     1000000         -1.00               4              4 DB2ADMIN.TEST_INDEX_NULLVALUE 
TEST_INDEX_NULLVALUE@_COL_INT         1102       3         1000        1000             1101          96     1000000         -1.00               4              4 DB2ADMIN.TEST_INDEX_NULLVALUE 
TEST_INDEX_NULLVALUE@_COL_NULL        1101       3            1           1             1100          99     1000000         -1.00               4              4 DB2ADMIN.TEST_INDEX_NULLVALUE
Example 1: Not null column.
<genrow>
Code:
SELECT id
 FROM  test_index_nullvalue
 WHERE id IS NULL
;
Example 2: Not null column.
<ixscan>
Code:
SELECT id
 FROM  test_index_nullvalue
 WHERE id = 1
;
Example 3: (1 / 1000) rows are null.
<ixscan>
Code:
SELECT COUNT(*)
 FROM  test_index_nullvalue
 WHERE col_int IS NULL
;
Example 4: All rows are null.
<ixscan>
Code:
SELECT COUNT(*)
 FROM  test_index_nullvalue
 WHERE col_null IS NULL
;

Last edited by tonkuma; 08-04-11 at 21:23. Reason: Removed Example 5 to 7. Corrected my misunderstandings for the result.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On