Skip to Main Content

Oracle Database Discussions

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

better cardinality for predicate having is null

spur230Nov 2 2015 — edited Nov 5 2015

I  am using Oracle 11.2.0.3.   I  have a query similar to the one given below. It's estimated cardinality is  3 times off from actual.  I tried to create extended statistics but it is not helping. 

Can't extended statistics be used  on columns  handling is null?

Is there any way to improve cardinality for this cases.

I have created random data in tmp.

col1 can have values  1 and 2.

col 2 can have values 1 and 2.

col3 is date and it is null mostly when  col1=1 and col2=1

I want to get good estimate for query (select * from tmp where col1=1 and col2 =1 and col3 is null)

drop table tmp;

create table tmp ( col1 number, col2 number, col3 date);

insert  into tmp

select 1 ,1 ,sysdate from dual

union all

select 1, 2, sysdate  from dual

union all

select 1 ,1 ,NUll  from dual

union all

select 1, 1, NULL  from dual

union all

select 1, 1, sysdate  from dual

union all

select 2, 2, sysdate  from dual

union all

select 1, 1, NULL  from dual

exec DBMS_STATS.GATHER_TABLE_STATS( user, 'TMP' , method_opt => 'FOR ALL COLUMNS ');

select  count(*) from tmp where col1=1 and col2 =1 and col3 is null ;

-- gives 3 estimate is only 1

Plan hash value: 3231217655

----------------------------------------------------------------------------

| Id  | Operation          | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time   |

----------------------------------------------------------------------------

|   0 | SELECT STATEMENT   |      |        |       |     4 (100)|          |

|   1 |  SORT AGGREGATE    |      |      1 |    11 |            |          |

|*  2 |   TABLE ACCESS FULL| TMP  |      1 |    11 |     4   (0)| 00:00:01 |

----------------------------------------------------------------------------

select dbms_stats.CREATE_EXTENDED_STATS ( user, 'TMP','(col1,col2,col3)') from dual;

exec DBMS_STATS.GATHER_TABLE_STATS(user, 'TMP', method_opt => 'for columns (col1,col2,col3) ' , degree=> 16 , estimate_percent => null);

select  count(*) from tmp where col1=1 and col2 =1 and col3 is null;

-- gives 3 estimate is only 1

This post has been answered by Jonathan Lewis on Nov 5 2015
Jump to Answer

Comments

cormaco

I think Quick SQL is the wrong place to execute your script:

About Quick SQL

Quick SQL provides a quick way to generate the SQL required to create a relational data model from an indented text document. This tool is designed to reduce the time and effort required to create SQL tables, triggers, and index structures. This tool is not designed to be a replacement for data modeling, it is simply a quick way to develop a script for simple tables and views. Once the SQL is generated it can be tweaked and expanded upon.

Use SQL Scripts instead.

PS:

Please change you display name into something readable:

How can I change my Display Name?

NicolasG

Thanks, I did it on sql script and it generated my model well and I was able to create an application, however, when I try to add data to my movie table for example, I get the following error:

ORA-01400: impossible d'insérer NULL dans ("NGUILMINIR2022"."ACTORS"."ACTORS_ID")

I imagine that my problem is at the ID level, indeed when I want to create a movie, I do not have enough to enter the Id and I imagine that I have no default values and auto increment so I tried to add a constraint and trigger in the script to fix the problem but it had no effect, maybe I did it wrong?

If you know how to help me I would be very grateful

cormaco

A typical approach is to create a sequence (in APEX in the Object Browser)

and then select this sequence in the Page Designer as default value for this item.

Repeat this step for each primary key column in your model.

Dave Schleis

Hello Nicolas

You can also have SQL Developer Data Modeler create the sequences and the triggers for you.

If you double-click on a table in your relational model it will open the Table Properties dialog

Screen Shot 2020-05-14 at 7.34.09 AM.png

Select the "Columns" item in the left-hand Panel, and then double-click on the primary key column:

Screen Shot 2020-05-14 at 7.36.08 AM.png

This opens the column dialog.

Screen Shot 2020-05-14 at 7.40.08 AM.png

Make sure that "Auto Increment", "Identity Column" and "Engineer" boxes are checked, and then select OK.

Click OK to exit the Table Properties dialog.

By right-clicking on the table in your model and selecting DDL preview, you should see the DDL for the sequence and the trigger.

Screen Shot 2020-05-14 at 7.44.21 AM.png

You will need to check the "Auto Increment", "Identity Column" and "Engineer" boxes for all of the tables and then regenerate the table DDL.

Hope that helps

--dave

PS I noticed that the primary key of your movie table is named "Attribute_1" Are you sure that is what you want?

NicolasG

I manage to create sequence with the object browser but now, my problem is that I do not manage to select the sequence in page designer.

Among my pages generated automatically (interactive state or pannel) during the creation of my application, some allow me to modify an id element and to apply a default values of type sequence with the name of my sequence

detail of my sequence:

  • minvalue: 1
  • maxvalue: 9999
  • increment by 1
  • cycle N
  • order N
  • cache 0
  • lastnumber 1

However, once the page run and the application reloads the application and I try to create a new data in my table from this one the following error appears

ORA-06550: Line 1, column 44 : PLS-00201: identifier 'seq_genre.NEXTVAL' must be declared

so my question are:

  • why some page allow me to modify the default value and sequence when others don't
  • does my sequence are well made for primary_key
  • how to fix my identifier issue

cormaco

This is no longer an Data Modeler issue.

Open a new thread in the APEX forum.

1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Dec 3 2015
Added on Nov 2 2015
9 comments
3,732 views