Indeksy na kolumnach z wartościami nullowymi cz. 1

A tak dokładnie aby zadziałał, gdy chcemy zrobić zapytanie:

select * from tablica where kolumna is null;

Zakładamy wtedy indeks funkcyjny:

create index kolumna_idx on tablica (NVL(kolumna,TO_DATE(' 4000-01-01', 'syyyy-mm-dd'))) 

I wtedy musimy tak konstruować zapytanie, aby mogło wykorzystać powyższy indeks:

select * from tablica   where NVL(kolumna,TO_DATE('4000-01-01', 'syyyy-mm-dd')) = TO_DATE('4000-01-01', 'syyyy-mm-dd') 

Dla porównania koszt tradycyjnego zapytania:

select count(*) from dh_monitor.events where valid_to is null

to 8122. Dla porównania, zapytanie które wykorzystuje indeks ma koszt 3!!!

select count(*) from  events where NVL(VALID_TO,TO_DATE('4000-01-01 ', 'syyyy-mm-dd')) = TO_DATE('4000-01-01', 'syyyy-mm-dd')

Dla tabeli, gdzie stosunek ilości nullowych wierszy do wszystkich w tabeli wynosi: 6.568/5.999.617

You May Also Like

Confitura 2013 afterthoughts

Confitura, the biggest free-of-charge Java conference in Europe, took place on the 6th of July in Warsaw. TouK's presence was heavy, with 5 separate talks, all chosen in call for papers, no sponsored bullshit. We were sponsoring deck chairs during the...Confitura, the biggest free-of-charge Java conference in Europe, took place on the 6th of July in Warsaw. TouK's presence was heavy, with 5 separate talks, all chosen in call for papers, no sponsored bullshit. We were sponsoring deck chairs during the...

Hibernate hbm2ddl won’t create schema before creating tables

Situation I have a local H2 in memory database for integration tests and an Oracle db for production. I do not control the Oracle DB model. The in memory H2 database is created automatically by adding <prop key="hibernate.hbm2ddl.auto">update&l...Situation I have a local H2 in memory database for integration tests and an Oracle db for production. I do not control the Oracle DB model. The in memory H2 database is created automatically by adding <prop key="hibernate.hbm2ddl.auto">update&l...