Tuesday, March 13, 2012

Thursday, January 19, 2012

Analytic functions

Analytic functions are nice:


stackoverflow: Conditional group by (group similar items)

As above, you can group by some rows and others not, in a single shot.

Monday, September 26, 2011

Cum arăţi că iubeşti?

I-a citit Mariei Mary povestirea cu femeia pacatoasa care a plans la picioarele Domnului Isus si care L-a uns cu mir de nard curat.
La sfarsitul povestirii, se cere fedback:
Intrebarea numarul unu: Cum arati tu cuiva ca il iubesti?
Raspunsul Mariei. Păi dacă tati imi cere mie... un prosop, eu îl iau, îl intocmesc frumos, si i-l dau asa frumos... Adica sunt ascultatoare.
Intrebarea numărul doi. Cum arati tu Domnului Isus ca Il iubesti?
Raspunsul Mariei.Pai cand o merg asa prin cer, daca ma intalnesc cu Domnul Isus, si e murdar pe picioare, Il spal pe picioare. Si daca e murdar si pe mâini, Il spăl si pe mâini.

Friday, July 1, 2011

Oracle's minus has a bug?

No, it doesn't.
But look at the following situation:
You have two tables that must be identical(must contain same data).
You shoot a minus between them and get some rows. Few, but they exists.

You choose one row from the result of minus, and run
select * from table1 where col=key1
minus
select * from table2 where col=key1
and you get one row.

you run

select * from table1 where col=key1
union all
select * from table2 where col=key1
and you get two rows.

you run 
select * from table1 where col=key1
union
select * from table2 where col=key1
and you get one row!
so, the rows are not different!

How can that be possible???
You run explain plan for every query from above and it does what you asked in query.
Think a bit, and if you can't find the answer, scroll down to see it.




Wednesday, June 1, 2011

change default table or index tablespace

Is so simple as useful

ALTER TABLE table_name MODIFY DEFAULT ATTRIBUTES TABLESPACE new_tablespace;
ALTER INDEX index_name MODIFY DEFAULT ATTRIBUTES TABLESPACE new_tablespace;

For example, new partitions for local indexes will be created in the new tablespace when you add new partitions on the table.

Thursday, May 19, 2011

Oracle - move partitioned tables to another tablespace

Just few days ago I've got a task to move all data from a tablespace to another - disk replacement.
My thought was: "simple job, excepting partitioned tables."
No, you do not need to recreate the tables(with all corresponding indexes) and make insert into new_table select * from old_table. I have 700 tables, few of them near 10GB, but many around 1GB.

First code I managed looked like this:


declare
   stmt varchar(1000);
begin
   for k in 0..189
   loop
      stmt:='ALTER TABLE table_name MOVE PARTITION P'||to_char(add_months(to_date('201102','yyyymm'),k),'yyyymm')|| ' TABLESPACE NEW_TABLESPACE';
      execute immediate stmt;

      stmt:='ALTER TABLE table_name MODIFY PARTITION P'||to_char(add_months(to_date('201102','yyyymm'),k),'yyyymm') ||' REBUILD UNUSABLE LOCAL INDEXES TABLESPACE NEW_TABLESPACE';
      execute immediate stmt;
   end loop;
end;


for tables partitioned by range in a monthly manner.

Tuesday, May 17, 2011

miroase ca la munte

Maria, până sa implinească 3 ani, iarna asta.
Am luat un şampon diferit de cel cu care era/eram obişnuiţi.
Când îl folosim prima dată, Maria comentează: "Ăsta e ca la munte". Noi nu am înţeles nimic, evident. Nu scria pe el mountain, nu era cu cine ştie ce arome... ce vrea sa zică figurina asta de Maria?

Ne prindem noi după ceva timp cum stă treaba. Vara trecută, când am fost la munte, am luat la repezeală din supermarket un şampon de copii, pe care l-am folosit la munte. Era acelaşi şampon pe care il foloseam acum şi maria l-a recunoscut dupa... miros. Maria a asociat mirosul cu muntele sau... în sfârşit, şi-a adus aminte ceva cu şase luni în urmă. Pe atunci avea până în doi ani şi jumătate, şi intre timp nu am folosit un astfel de şampon.