Sunday, November 23, 2008

Advanced Oracle Troubleshooting by Tanel Poder..

.. is over. His seminar in Riga was real fun for two days. It was different from the big guys' seminars I've attended before (for example, Thomas Kyte and Jonathan Lewis). Not in the way that it was or wasn't somehow better or not, but in the way that Tanel spoke more about:

  • internal memory structures (x$ tables), process stacks, Oracle kernel functions and their mapping to SQL statement execution plan steps;
  • understanding structure of library cache, process state objects, buffer cache;
  • dumping cursors, trace buffers, process stack;
  • systematic troubleshooting a session and finding the root cause of slowness and what it is doing now;
  • and many more subjects.

Lots of live demos, nothing like an Oracle documentation rephrase, narrative explanation of quite advanced technical things - these were the main characteristics of this seminar.

Some previous understanding of Operating systems basic functions, commands and Oracle architecture however is a must, otherwise you simply will be overwhelmed by new information and won't be able to follow the main idea. On the other hand I'm absolutely not a sysadmin and/or true DBA, but I understood let's say 95% of the material provided ;)

So the conclusion is - I definitely recommend it for the people interested in these topics.

Thursday, November 20, 2008

Data modeling

Time after time it is worth to remember (or learn) basic theory. One of them, which is very important when creating new systems, is data modeling. I'm quite sure most of the DBMS people have seen one or another data model with tables, columns, primary and foreign keys. However the theory of data modeling says that this is only one level out of three. Unfortunately I've seen quite many cases when people have forgotten two another levels. So let's remember what they are.

Conceptual data model

Conceptual data model contains entities, relationships and attributes. One can draw them using different notations for example ER modeling or UML, but the idea remains the same - this model doesn't know anything about the underlying DBMS. This model is the same for Oracle, SQL Server, DB2, MySQL, whatever else. So why is it important?

  • It speaks in business language and uses terms only from business users, not any programmer's or IT person's hacks.
  • It is normalized, every piece of information is stored only once.
  • It displays common structures and tries to aggregate them, it is easier to find them in such model.
  • It is readable (after a short introduction probably) by every sensible customer.
  • It doesn't contain any reference to any particular DBMS, so one can easily reuse it for ultimately any DBMS.

So the question - have you used it? If not - why? Are you aware of possible data waste dangers?

Logical data model

This is the model people usually use. It contains tables, columns, primary keys, foreign keys, unique keys. Some characteristics of it are:

  • It is created on the basis of conceptual data model, but it might not correspond 1:1 to it. For example some entities might be split, some unioned.
  • It may contain some programmer's hacks for example different flag columns for easier data selection.
  • It is aware of used DBMS. For example data types for columns are DBMS specific.
  • It may have denormalized columns (i.e. the same facts stored in more than one column) for performance reasons.
  • Table and column names might not exactly mimic business terms i.e. they might be abbreviated, changed to conform some naming rules and/or DBMS restrictions, although for easier readability it is worth to retain some dependency.
  • It is based on conceptual data model but created keeping performance and most common possible SQL statements in mind.

I'm quite sure you have used it, so the only question is - have you created it keeping performance and business critical SQL in mind?

Physical data model

What's that? Is there anything remaining to model at all? Yes it is. Heap table (i.e. table type which is used by default) is not the only available table type in Oracle. Partitioning is not the only option, which can be used in Oracle. So what is modeled here?

  • Table type - heap, index organized, clustered, sorted hash cluster, single table hash cluster there are many possible options.
  • Partitioning, compression, encryption - do you need it, can you afford it?
  • Indexes - will you use only b-tree or something more?
  • Will you use object tables?
  • What tablespaces will be created and on what devices/files?
  • Here comes the real strength (or weakness) of your chosen DBMS - if it offers all these features - why not at least consider them?

If you are DBA you have definitely used some of the features mentioned above, but have you thought about non-default ones? Have you systematically gone through at least the most business critical tables and considered what kind of table type to use, what kind of indexes create?

Not that I'm suggesting to enforce anything just for the sake of completeness ;) but I'm sure I've read something like that somewhere "If you have only heap tables and b-tree indexes, most probably your schema is not optimal".

Conclusion

Sometimes conceptual modeling is called logical and logical physical and then of course question remains how should be called the real "physical modeling", but it is not so important. The important thing is - whatever DBMS you choose for a new application it is worth to go through all the data model steps. Go through the conceptual modeling to better understand your requirements, to better confirm them with your customer, to better find out common patterns. Of course logical modeling cannot be avoided if we'd like to get at least one table, however quite many people forget about referential integrity constraints and rely only on application enforcing it. Sighhhh... And at last don't forget about physical modeling because when your table will contain (hundreds) millions of rows in production it would be much harder to change it's type, move it somewhere else etc.

Tuesday, October 14, 2008

Mortgage calculator using SQL Model clause


Recently we had local Latvian Oracle day conference. This year it was quite big event with 5 parallel sessions and more than 400 participants. I also was one of the presenters telling about analytic functions and SQL Model clause. During the process of understanding deeper and better Oracle SQL Model clause I've tried make also some a bit complex examples myself. Nowadays the actual problem is mortgage and of course the possibility to pay monthly payments (fortunately not for me :) ) on the background of world wide financial crisis.
So here is fully functional mortgage calculator using SQL Model clause. There are two variants:I've used this site to get formula for fixed monthly payment required to fully amortize a loan over a term of fixed months at a fixed monthly interest rate.
OK and now the scripts.

set ver off
set lines 120
undefine rem_loan year_int_rate term
SELECT m+1 month,
to_char(rem_loan, '99999999.00') rem_loan,
to_char(loan_paid_tot, '99999999.00') loan_paid_tot,
to_char(mon_int, '999999.00') mon_int,
to_char(tot_int, '99999999.00') tot_int,
to_char(mon_paym, '99999999.00') mon_paym,
to_char(mon_paym_tot, '99999999.00') mon_paym_tot,
to_char(grand_tot, '99999999.00') grand_tot
FROM dual
MODEL
DIMENSION BY (-1 m)
MEASURES (&&rem_loan rem_loan,
round(&&rem_loan*&&year_int_rate/100/12,2) mon_int,
ceil(&&rem_loan/&&term*100)/100 mon_paym,
(&&rem_loan/&&term*100)/100 loan_paid_tot,
round(&&rem_loan*&&year_int_rate/12/100,2) tot_int,
ceil(&&rem_loan/&&term*100)/100 + round(&&rem_loan*&&year_int_rate/100/12,2) mon_paym_tot,
ceil(&&rem_loan/&&term*100)/100 + round(&&rem_loan*&&year_int_rate/100/12,2) grand_tot
)
RULES ITERATE (&&term) UNTIL (round(loan_paid_tot[iteration_number], 2) = &&rem_loan) (
rem_loan[iteration_number] = rem_loan[iteration_number -1] - mon_paym[iteration_number - 1],
mon_int[iteration_number] = round(rem_loan[iteration_number]*&&year_int_rate/100/12,2),
mon_paym[iteration_number] = least(ceil(&&rem_loan/&&term*100)/100, rem_loan[iteration_number]),
loan_paid_tot[iteration_number] = loan_paid_tot[iteration_number - 1] + mon_paym[iteration_number],
tot_int[iteration_number] = tot_int[iteration_number - 1] + mon_int[iteration_number],
mon_paym_tot[iteration_number] = mon_paym[iteration_number] + mon_int[iteration_number],
grand_tot[iteration_number] = grand_tot[iteration_number - 1] + mon_paym_tot[iteration_number]);

Result for loan of 1000 money units (lats, euros, dollars, pounds whatever) for 10 % year rate with term of 7 months looks as follows:

Enter value for rem_loan: 1000
Enter value for year_int_rate: 10
Enter value for term: 7

MONTH REM_LOAN LOAN_PAID_TO MON_INT TOT_INT MON_PAYM MON_PAYM_TOT GRAND_TOT
---------- ------------ ------------ ---------- ------------ ------------ ------------ ------------
0 1000.00 142.86 8.33 8.33 142.86 151.19 151.19
1 857.14 285.72 7.14 15.47 142.86 150.00 301.19
2 714.28 428.58 5.95 21.42 142.86 148.81 450.00
3 571.42 571.44 4.76 26.18 142.86 147.62 597.62
4 428.56 714.30 3.57 29.75 142.86 146.43 744.05
5 285.70 857.16 2.38 32.13 142.86 145.24 889.29
6 142.84 1000.00 1.19 33.32 142.84 144.03 1033.32
For fixed payment each month the script is as follows:

SELECT m+1 month,
to_char(rem_loan, '99999999.00') rem_loan,
to_char(loan_paid_tot, '99999999.00') loan_paid_tot,
to_char(mon_int, '999999.00') mon_int,
to_char(tot_int, '99999999.00') tot_int,
to_char(mon_paym, '99999999.00') mon_paym,
to_char(mon_paym_tot, '99999999.00') mon_paym_tot,
to_char(grand_tot, '99999999.00') grand_tot
FROM dual
MODEL
DIMENSION BY (-1 m)
MEASURES (&&rem_loan rem_loan,
round(&&rem_loan*&&year_int_rate/100/12,2) mon_int,
ceil(&&rem_loan*(&&year_int_rate/100/12*POWER((1+&&year_int_rate/100/12),&&term))/
(POWER((1+&&year_int_rate/100/12),&&term)-1)*100)/100 -
round(&&rem_loan*&&year_int_rate/100/12,2) mon_paym,
ceil(&&rem_loan*(&&year_int_rate/100/12*POWER((1+&&year_int_rate/100/12),&&term))/
(POWER((1+&&year_int_rate/100/12),&&term)-1)*100)/100 -
round(&&rem_loan*&&year_int_rate/100/12,2) loan_paid_tot,
round(&&rem_loan*&&year_int_rate/12/100,2) tot_int,
ceil(&&rem_loan*(&&year_int_rate/100/12*POWER((1+&&year_int_rate/100/12),&&term))/
(POWER((1+&&year_int_rate/100/12),&&term)-1)*100)/100 mon_paym_tot,
ceil(&&rem_loan*(&&year_int_rate/100/12*POWER((1+&&year_int_rate/100/12),&&term))/
(POWER((1+&&year_int_rate/100/12),&&term)-1)*100)/100 grand_tot
)
RULES ITERATE (&&term) UNTIL (round(loan_paid_tot[iteration_number], 2) = &&rem_loan) (
rem_loan[iteration_number] = rem_loan[iteration_number -1] - mon_paym[iteration_number - 1],
mon_int[iteration_number] = round(rem_loan[iteration_number]*&&year_int_rate/100/12,2),
mon_paym_tot[iteration_number] = least(
ceil(&&rem_loan*(&&year_int_rate/100/12*POWER((1+&&year_int_rate/100/12),&&term))/
(POWER((1+&&year_int_rate/100/12),&&term)-1)*100)/100,
rem_loan[iteration_number] + mon_int[iteration_number]),
mon_paym[iteration_number] = mon_paym_tot[iteration_number] - mon_int[iteration_number],
loan_paid_tot[iteration_number] = loan_paid_tot[iteration_number - 1] + mon_paym[iteration_number],
tot_int[iteration_number] = tot_int[iteration_number - 1] + mon_int[iteration_number],
grand_tot[iteration_number] = grand_tot[iteration_number - 1] + mon_paym_tot[iteration_number]);
Result for the same loan of 1000 money units (lats, euros, dollars, pounds whatever) for 10 % year rate with term of 7 months looks as follows:

MONTH REM_LOAN LOAN_PAID_TO MON_INT TOT_INT MON_PAYM MON_PAYM_TOT GRAND_TOT
---------- ------------ ------------ ---------- ------------ ------------ ------------ ------------
0 1000.00 139.33 8.33 8.33 139.33 147.66 147.66
1 860.67 279.82 7.17 15.50 140.49 147.66 295.32
2 720.18 421.48 6.00 21.50 141.66 147.66 442.98
3 578.52 564.32 4.82 26.32 142.84 147.66 590.64
4 435.68 708.35 3.63 29.95 144.03 147.66 738.30
5 291.65 853.58 2.43 32.38 145.23 147.66 885.96
6 146.42 1000.00 1.22 33.60 146.42 147.64 1033.60
During my research I've found invaluable these resources:

Monday, July 14, 2008

One of 364 000 articles



According to google I've written another one of ~364 000 articles about SQL join types. So the question is why anyone should read it?
Here are a few reasons:

  • It is 25 pages long;

  • It contains join types metamodel or at least my attempt to create one;

  • It tries to describe what are different join types and how they relate to each other;

  • It contains visual pictures of cross, inner, and left, right, full outer joins and this time not with Venn diagramms, which I personally think are suitable for absolutely different operations (set operations);

  • To describe theory it contains 44 examples, which are tested on Oracle, SQL Server and MySQL;

  • It contains several links to other resources.


Because it is 25 pages I've decided not to put it here but it is on my website.

I have one wish though - if You are reading this article and have access to another DBMS (than already mentioned in article i.e. Oracle, SQL Server, MySQL) and have a bit free time, please run examples on your DBMS and post results either here or send me to mail gints.plivna@gmail.com. Then I will put them in original article along with contributor's name.

Saturday, July 05, 2008

Latvian Nationwide Song and Dance Celebration in Riga

Today is the beginning of 24th song festival - unique celebration of choral songs, which started 135 years ago with the first song festival. In these times under the rule of Russian Czar, later they continued during the short freedom period as well as even in Soviet times. This year about 35 000 singers and dancers will come to Riga to sing and dance in various concerts. This means more than 2% of all Latvians are coming together to sing and dance and many many more are coming to see them on the concerts or via TV.

Brief history

The first All Latvian Song Festival took place in Riga, year 1873. There were 1003 singers in these times. Under the Russian Czar there were 5 festivals. During the Latvian independence there were 4 festivals and participant count raised to more than 14 000 people. During the Soviet times these festivals partially become the weapon of Soviet propaganda, however these were like two-edged sword, for example, the 100th anniversary of song festival in 1973 was some kind of emotional uprising against Soviet regime. Since 1990 song festivals are held in independent state again and the participant count has raised to impressive more than 2% of all Latvians. The learning process of songs and dances is continued all the time and it is worth to mention that noone pays a single santīms (centime) to participants both for preparation process lasting a few years and also the very festival, although of course there is governmental material support for the organizational purposes of the festival.

Events

During the festival there are many concerts and other kind of events. I'll mention a few of them. The first is participant parade. In the picture below one can look at one of the best choirs in the world "Kamēr", which has won many international awards and also takes part in this festival.
Latvian Song Festival choir Kamēr
The second one is main dance concert. In recent times more than 10 000 dancers are taking part in it.
Latvian Song and Dance Festival
The last and biggest event is the final concert with all the singers in one big choir. It is very impressive moment.
Latvian Song and Dance Festival

Some links

Friday, June 27, 2008

Trip to Estonia


During my holidays I've been in a 4 days trip to Estonia. For Oracle adepts this is the country where Tanel Poder comes from ;)
The trip was made up by myself and my wife, during it we drove ~1700 km. The general idea was to drive around all Estonia (it is not very big country) and look at some (hopefully) most interesting places. Here are 26 most interesting photos:

A view from Munamagi hill
A view from Munamagi hill, the highest point in Baltics

Tartu Dom Cathedral

Tartu Dom Cathedral, it is being rebuilt now

Tartu monument

Interesting fountain in Tartu, second largest Estonian city

Rainbow over Peipsi lake

Rainbow over Peipsi lake

Street at Peipsi

There is 8 km long street through the old villages along the Peipsi lake. It looks like that all lengthways


Ivangorod castle

Ivangorod castle over the river in Russia. This castle is just in the opposite side of Narva river from the Narva castle


Narva castle

Narva castle. This castle is just in the opposite side of Narva river from the Ivangorod castle


Bells in Narva castle

Aija along with bells in Narva castle


Estonia northern coast

Northern coast (Gulf of Finland) mostly consists of limestone rocks up to 56 metres high


Sunset over Gulf of Finland

Sunset over Gulf of Finland


Valaste waterfall

Valaste waterfall is the highest waterfall of Estonia


Kivioli hills

Estonia has some kind of oil shale, this hill is man-made and consists of slag of oil shales. It is 116 metres high. The picture was taken from a bit lower hill of the same origin, just older one.


Symbol of Rakvere town

Symbol of Rakvere town


Bikes at Palmse manor

Bikes at Palmse manor, which is in the very centre of Lahemaa national park, a beautiful sightseeing place


Jagala waterfall

Jagala waterfall near from Tallinn


Tallinn city wall

Tallinn has more than 2km long old city wall with many towers and everything in very well preserved


Tallinn old town

Tallinn old town


Ferries to Saaremaa

There was a bit rainy when we crossed over to Saaremaa island (actually Muhu island, which is connected to Saaremaa by dam). The sail is very short just ~20 minutes.


Liiva church

13th century painting in Liiva church, Muhu island


Maasilinn stronghold

Maasilinn stronghold in Saaremaa


Angla windmills

Angla windmills. There were multitude of windmills in Saremaa (~800) these are just a few of forever gone old might.


Kaali meteoritic lake

It is very easy to get to this Kaali meteoritic lake, it is just 17 km from Kuressaare, centre of Saaremaa


Kuressaare castle

A passage in Kuressaare castle, Saremaa


Toilett of Middle Ages

Toilett of Middle Ages in Kuressaare castle, Saremaa


Kihelkonna church

Organ of Kihelkonna church, Saremaa


Kihelkonna church

Kihelkonna church, Saremaa. It was possible to climb up to the tower and look around through the windows

If you enjoyed these you can look also at some of my photos from Latvia.

Thursday, June 26, 2008

New improved price list

Obviously fluctuations of $ is affecting also Oracle ;) and as a result we have new global price list. Changes have affected also all Oracle database editions except Express, which is for free. So now Standard Edition One license is for 5 800 (was 4 995), Standard Edition is for 17 500 (was 15 000) and Enterprise Edition 47 500 (was 40 000) per processor. Personal Edition named user plus licence now is 460 US dollars (was 400). All new prices are here. These are the bad news.

The good news are that Oracle has created very informative Pricing and Licensing Rules, Tools page (at least I was not aware of that), which has many links to other valuable documents regarding Oracle pricing and licensing, for example, Oracle product prices in other currencies using Oracle exchange rates.

So get accustomed to new prices ;) and don't forget that there are other editions than Enterprise as well as there are such thing like term licensing (licenses for 1 till 5 years starting with 20% and ending with 70% of list price)!

Thursday, May 22, 2008

SQL Merge and Sequence gaps

I hope You my dear reader isn't one of the people cursed by requirement for gapless sequences ;)
Yesterday I just found another nail in the coffin for oracle sequence objects used as gapless sequence generators. Of course there are many causes not to do that, just like simple rollback, users changing their minds, network connection error, flushing cache etc. But I didn't know at least till yesterday, that MERGE statements generate gaps with quite big guarantee. It seems that Merge statement is generating the same number of calls for sequence next value as row count of source table or subquery.
Let's look at example:

CREATE TABLE target (pk number, data varchar2(10));
CREATE TABLE source (pk_s number, data_s varchar2(10));
INSERT INTO target VALUES (1, 'a');
INSERT INTO target VALUES (3, 'c');
INSERT INTO source VALUES (1, 'a');
INSERT INTO source VALUES (2, 'b');
INSERT INTO source VALUES (3, 'c');
INSERT INTO source VALUES (4, 'd');
COMMIT;
CREATE SEQUENCE seq START WITH 5;

So now we have 2 rows in target table, and 4 rows in source table. We'll compare them using data and data_s column. We'd insert only two rows, because another 2 already exists and even won't use WHEN MATCHED clause for update.

MERGE INTO target
USING source
ON (data = data_s)
WHEN NOT MATCHED THEN
INSERT VALUES (seq.nextval, data_s);
SQL> SELECT * FROM target;
PK DATA
---------- ----------
1 a
3 c
7 d
8 b

Although the sequence should have started from 5 new pk values are 7 and 8. Let's add another row into source and look what happens:

INSERT INTO source VALUES (5, 'e');
MERGE INTO target
USING source
ON (data = data_s)
WHEN NOT MATCHED THEN
INSERT VALUES (seq.nextval, data_s);
SQL> SELECT * FROM target;
PK DATA

---------- ----------
1 a
3 c
7 d
8 b
13 e

So now the value is 13 i.e. 8 + count of rows from source table (5). Now let's add another row and delete all previous.

INSERT INTO source VALUES (6, 'f');
DELETE source WHERE pk_s <=5;
MERGE INTO target
USING source
ON (data = data_s)
WHEN NOT MATCHED THEN
INSERT VALUES (seq.nextval, data_s);
SQL> SELECT * FROM target;
PK DATA

---------- ----------
1 a
3 c
7 d
8 b
13 e
14 f

As we can see pk is incremented only by 1.
After my experiments I found also metalink note "Merge Increments SEQUENCE.NEXTVAL for Both Insert and Update" (Note:554656.1), which also assures this as expected situation and not bug. So another reason not to worry about sequence values, just uniqueness of them ;)

Friday, May 16, 2008

Minus All and Intersect All


Today I rediscovered Oracle Mix ideas and remembered my surprise that there are also Minus all and Intersect all operators in SQL standard just like Union and Union all. I'll briefly remind You what exactly they were. Let's imagine we have two sets:

T1: 1, 2, 2, 2, 3, 4, 4
T2: 2, 3, 4, 4, 4, 5


Then T1 INTERSECT T2 is 2, 3, 4
T1 INTERSECT ALL T2 would be 2, 3, 4, 4


T1 MINUS T2 is 1
T1 MINUS ALL T2 would be 1, 2, 2


So by default Intersect and Minus have DISTINCT operator removing all non-unique values. But in case we add keyword ALL then all values and their cardinalities are counted as well. I think these operators could be quite handy in process of analysing two data sets and finding what exactly the difference is. So if You are thinking the same way then I kindly ask you to support my idea about intersect all and minus all with your voice in Oracle Mix.

Thank You in advance for any decision :) and I hope sooner or later (better sooner of course ;) to find that in New features guide.

Wednesday, May 07, 2008

DBMSes are different...

Recently had to do some work with SQL Server. As my experience with it is quite limited I've got a few interesting impressions, which probably are absolutely trivial for SQL Server adepts :)

Do you know that SELECT query can create new tables?

OK in SQL Server it can. So I had to create backup of a table and firstly was quite frustrated not finding anything like CTAS (CREATE TABLE ... AS SELECT). I was on a wrong track however. The right syntax is SELECT INTO as in following example:

CREATE TABLE persons (
prs_id INT IDENTITY NOT NULL PRIMARY KEY,
prs_name VARCHAR(30) NOT NULL) ;

INSERT INTO persons (prs_name) VALUES ('GINTS');
INSERT INTO persons (prs_name) VALUES ('JOE');

SELECT *
INTO persons_bkp
FROM persons;

SELECT * FROM persons_bkp;
-------------------------
1 GINTS
2 JOE


So till now I was under the impression that SELECT is DML statement however obviously that's not true at least for SQL Server.

Do you know that SQL Server can handle recursive queries?

At least since version 2005 it can. Long ago I have heard that SQL Server has nothing similar to START WITH CONNECT BY. This site comparing SQL Server and Oracle seemed to approve my thoughts. However DBMSes are different and one has to look for different ways accomplishing the same goals. And in SQL Server one can use Common table expressions (aka query factoring clause or with clause in Oracle world) to get hierarchical results. And BTW here SQL Server "outfunctions" Oracle because it allows to recursively reference the same common table in its definition, which is not allowed by Oracle. Here are two articles if you are interested in how exactly it is done:

So in conclusion these two features which are possible in both DBMSes, but are implemented in completely different ways supports my previous article that all databases are different :)

Wednesday, January 30, 2008

The year has gone..

..since I started blogging. Actually I started it because of real anger against Oracle. The reason was one of the most famous problems in OTN history - inability to change e-mails ;) However about a month or two later it was resolved.
It seems the last year Oracle has made advances towards the community and:
1) listened to it (above mentioned case) as well as for example the case with AWR and ASH licensing. Probably all these steps were already on their way but I'm quite sure blog entries accelerated the result.
2) created public resources (oracle wiki) probably not veeeeery actively used until now.
3) granted free access for active members of public community to Oracle open world.
4) most probably made other positive steps I cannot remember or simply haven't heard about.

Last year was also big improvement in so called Oracle blogosphere - orana.info was founded along with some other aggregators, which can be found on oracle wiki.

For me it was a year of realizing the fact that there are tremendous resources about databases and Oracle in English, but just few in my mother tongue Latvian. So as I don't think English is the only language in the world even for databases :) I started to blog about databases in Latvian. Quite a bit of free time goes there.

So in my little anniversary I wish Oracle remember and support its community (probably sponsor a trip to Open world next year for bloggers khe khe :))) and myself to continue work about databases and Oracle resources especially in Latvian!

Friday, January 25, 2008

SQL Join Types


New enhanced 25 pages long article with 44 examples and visual pictures describing SQL join types can be found here.

I'm studying a bit SQL joins and trying to make clear at least for me what they are and what the relationships among them are. As I've always thought that picture is worth hundred words I've tried to create ER diagram describing metainfo about joins. So the question for everyone is - is this diagram OK? And aren't there obvious bugs? :)
Probably from the strict viewpoint of Set theory and relational algebra it is not OK, but I'm more concerned of practical SQL usage and understanding. In case you know similar diagramm or whatever else visual explanation of relationships among join types please leave comment with link.
So the diagram is as follows (please click on it to get bigger):

SQL Join Types

Now for those that aren’t familiar with all these join types and terms in diagram above I'll try to give SQL examples (without data and results) for each one of them. If you'd like to have also examples with data cehck one of the links at the very end of this post.
Assume we have 2 tables - TableA (id, title) and TableB (id, description).
What the result are for each one of them you can find in other places for example:

  • Cross join:

SELECT * FROM TableA CROSS JOIN TableB

  • Join with restriction:

It is either natural join or qualified join. Each join with restriction has one cross join which is the degraded case of it.

  • Natural join:

Natural joins can be Inner or one of Outer joins, but they always are equi joins. Without explicitly specified inner or outer it is inner join. More about natural joins in my previous blog post.

SELECT * FROM TableA NATURAL JOIN TableB

  • Qualified join:

Qualified joins can be written either specifying join columns (named columns join) or explicitly writing join condition.

  • Join type:

Could not find anything better to somehow unite inner and various outer joins under one roof.

  • Inner join:

SELECT * FROM TableA NATURAL INNER JOIN TableB
SELECT * FROM TableA INNER JOIN TableB USING (id)
SELECT * FROM TableA INNER JOIN TableB ON (tablea.id = tableb.id)

  • Outer join:

Outer join is one of left, right or full outer join.

  • Left outer join:

SELECT * FROM TableA NATURAL LEFT OUTER JOIN TableB
SELECT * FROM TableA LEFT OUTER JOIN TableB USING (id)
SELECT * FROM TableA LEFT OUTER JOIN TableB ON (tablea.id = tableb.id)

  • Right outer join and Full outer join:

Take the same examples as from left outer join and just replace left with right or outer.

  • Qualified join expression:

There are two ways how more explicitly write join condition either specifying join columns or explicitly writing join condition.

  • Named columns join:

This join is written with "USING" clause. Named columns join always is equi join.

SELECT * FROM TableA INNER JOIN TableB USING (id)

  • Conditional join:

This join is the real one that I'd like to suggest using. With explicitly specifying what the join condition is. This also is the only join where one can use not only equi join but also another predicate operator than "=".

SELECT * FROM TableA INNER JOIN TableB ON (tablea.id = tableb.id)

  • Predicate operator type:

Based on predicate operator type joins can be classified as either equi or theta (nonequi) joins.

  • Equi join:

Equi join is join where operator used in join condition is equivalence ("=").
Both natural joins and Named columns joins are always equi joins. Examples:

SELECT * FROM TableA NATURAL LEFT OUTER JOIN TableB
SELECT * FROM TableA FULL OUTER JOIN TableB USING (id)
SELECT * FROM TableA INNER JOIN TableB ON (tablea.id = tableb.id)

  • Theta (Nonequi) join:

Equi join is join where operator used in join condition is something other than equivalence ("=").

SELECT * FROM TableA INNER JOIN TableB ON (tablea.id <>

I've not showed here such beasts like UNION JOINS, SEMI JOINS and ANTI JOINS because the first one (UNION join) is neither in SQL standard nor in any database I worked with.
SEMI JOINS and ANTI JOINS are somehow less interesting for me.

Further reading:


Sunday, January 13, 2008

Some photos


I've been tagged by APC. Usually I don't respond to chain letters but this will be a bit of exception. However I won't tag anyone further. Looking at usual curses happening after NOT forwarding chain letters I hope all my Oracle installations won't silently transform to SQL Server ones ;)
So I won't tell anything about me, I'll just publish some of my best photos taken in various places in Latvia.







































Lilacs in Dobele


Opera square

Lilacs in Dobele gardenGarden if front of National Opera

Nest of Lesser Spotted Eagle


Heath

Nest of Lesser Spotted EagleAiring trail in heath

A lake in fog


Sun set

A lake in fogSun set view over my country landed property

Open air museum in Atte


Heath

Open air museum in AtteEaster eggs by my wife(colouring of course :)

National Opera House in Riga


Rundale Palace

National Opera House in RigaRundale Palace

Riga Dom Cathedral


House Of Blackheads and St.Peter church

Riga Dom Cathedral - biggest church in BalticsHouse Of Blackheads and St.Peter church

Railroad bridge at night


Art Noveau in Riga

Railroad bridge at nightArt Noveau in Riga

Thursday, January 10, 2008

MINUS ALL and INTERSECT ALL in Oracle Revisited

Yesterday I wrote an article explaining that MINUS ALL and INTERSECT ALL is not possible in Oracle.
However today after a bit thinking I got insight - multiset operations! Yes these should be the right thing!
Since 10g Release 1 Oracle supports MULTISET UNION/EXCEPT/INTERSECT for both ALL/DISTINCT. I have only to make some types, a bit type casting and everything should be ok!

So I've started with the simple tables I've shown yesterday.

CREATE TABLE t1 AS SELECT mod(rownum, 1000) rn
FROM dba_source WHERE rownum <=50000;
CREATE TABLE t2 AS SELECT * FROM t1;


Now I can query both tables and try to compare then. To use multiset operations I need to cast subquery as nested table. To be able to use such type casting I have to create two additional types. The first one is simple Object type containing records I'd like to compare. Second one is object table type, to which I'll cast my subqueries. So they are as follows:


CREATE TYPE t1_type
AS OBJECT ( rn NUMBER);
/
CREATE TYPE t1_tab_type
AS TABLE OF t1_type;
/


So far so good. Let's start building queries step by step. The first one just builds nested table from subquery.


SQL> select cast(multiset(
2 select rn from t1 where rownum <=3
3 ) as t1_tab_type)
4 from dual
5 /

CAST(MULTISET(SELECTRNFROMT1WHEREROWNUM<=3)AST1_TAB_TYPE)(RN)
---------------------------------------------------------------
T1_TAB_TYPE(T1_TYPE(1), T1_TYPE(2), T1_TYPE(3))

The second one already uses multiset intersect to get desired result.

SQL> select col1 multiset intersect distinct col2
2 from (
3 select cast(multiset(
4 select rn from t1 where rownum <=3
5 ) as t1_tab_type) col1,
6 cast(multiset(
7 select rn from t2 where rownum <=3
8 ) as t1_tab_type) col2
9 from dual
10 )
11 /

COL1MULTISETINTERSECTDISTINCTCOL2(RN)
---------------------------------------------------
T1_TAB_TYPE(T1_TYPE(1), T1_TYPE(2), T1_TYPE(3))

Now we just need to add one final step - cast nested table rows back to normal rows. Here us helps select from table construct:

SQL> select * from table (
2 select col1 multiset intersect distinct col2
3 from (
4 select cast(multiset(
5 select rn from t1 where rownum <=3
6 ) as t1_tab_type) col1,
7 cast(multiset(
8 select rn from t2 where rownum <=3
9 ) as t1_tab_type) col2
10 from dual
11 )
12 )
13 /

RN
----------
1
2
3

Voila! Everything works!
However then I was torn with doubt - how it will work when not used as a toy but on real volumes? I created million rows worth table with wide column and got - yeahh I got following:

ORA-22813: operand value exceeds system limits

Not pleasing.

Then I started to play with the tables defined above and got quite awful results. What is the performance of normal MINUS set operator?

SQL> select * from t1
2 minus
3 select * from t2 where rn <=998;
RN
----------
999
Elapsed: 00:00:00.10


What is the equivalent for multiset except distinct? I was a bit unpleasantly surprised again:

SQL> select * from table (
2 select col1 multiset except distinct col2
3 from (
4 select cast(multiset(
5 select rn from t1
6 ) as t1_tab_type) col1,
7 cast(multiset(
8 select rn from t2 where rn <= 998
9 ) as t1_tab_type) col2
10 from dual
11 )
12 )
13 /
RN
----------
999
Elapsed: 00:01:17.98

However for multiset except all I was simply shocked!

SQL> select * from table (
2 select col1 multiset except all col2
3 from (
4 select cast(multiset(
5 select rn from t1
6 ) as t1_tab_type) col1,
7 cast(multiset(
8 select rn from t2 where rn <= 998
9 ) as t1_tab_type) col2
10 from dual
11 )
12 )
13 /
RN
----------
999
...
999
50 rows selected.
Elapsed: 00:06:05.39

6 minutes for 2 * 50K rows!? Hmm is it implemented using bubble sort or what? :O

So normal SQL minus needed 0.10 seconds, multiset minus distinct 1 minute 18 seconds, but multiset except all more than 6 minutes.

Yeahh I got another strong argument NOT to use nested tables and their operations ;)

Wednesday, January 09, 2008

MINUS ALL and INTERSECT ALL in Oracle

Recently I was a bit studying SQL standard and a lot to my surprise found that there exists not only UNION ALL set operator, but also INTERSECT ALL and EXCEPT ALL (in Oracle version it would be MINUS ALL).

So you'd say Oracle haven't them and it is also (although partially - somehow minus has been forgotten) documented in Oracle Support for Optional Features of SQL/Foundation:2003.

Hmm, yea but a while ago I was doing several data migrations and then such set operators would really helped me. Now a bit what these non-existant (at least for Oracle) set operators are doing?

Imagine we have tables T1 and T2 with data as follows:

T1: 1, 2, 2, 2, 3, 4, 4
T2: 2, 3, 4, 4, 4, 5

So what is the result for SELECT * FROM T1 MINUS SELECT * FROM T2?

1

What would be the result for SELECT * FROM T1 MINUS ALL SELECT * FROM T2?

1, 2, 2

What is the result for SELECT * FROM T1 INTERSECT SELECT * FROM T2?

2, 3, 4

What would be the result for SELECT * FROM T1 INTERSECT ALL SELECT * FROM T2?

2, 3, 4, 4

As you can see ALL for EXCEPT/MINUS and INTERSECT retains cardinality and doesn't keep only unique records. Let's hope Oracle someday will stop this gap and give us this functionality :)