<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body bgcolor="#ffffff" text="#000000">
<div class="moz-text-html" lang="x-western">
Battleground was a machine running CentOS 5.5 and the two towers were 2
databases. First, PostgreSQL 8.4.4:<br>
<br>
<blockquote>s<tt><font color="#3333ff">cott=# select count(*) from emp
e1,emp e2, emp e3, emp e4,emp e5, emp e6;</font></tt><br>
<tt><font color="#3333ff"> count </font></tt><br>
<tt><font color="#3333ff">---------</font></tt><br>
<tt><font color="#3333ff"> 7529536</font></tt><br>
<tt><font color="#3333ff">(1 row)</font></tt><br>
<br>
<tt><font color="#3333ff">Time: 2127.345 ms</font></tt><br>
<tt><font color="#3333ff">scott=# select version();</font></tt><br>
<tt><font color="#3333ff">
version </font></tt><br>
<tt><font color="#3333ff"> </font></tt><br>
<tt><font color="#3333ff">--------------------------------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff">--------------------------------</font></tt><br>
<tt><font color="#3333ff"> PostgreSQL 8.4.4 on i686-redhat-linux-gnu,
compiled by GCC gcc (GCC) 4.1.2 2008</font></tt><br>
<tt><font color="#3333ff">0704 (Red Hat 4.1.2-46), 32-bit</font></tt><br>
<tt><font color="#3333ff">(1 row)</font></tt><br>
<br>
<tt><font color="#3333ff">Time: 1.008 ms</font></tt><br>
<tt><font color="#3333ff">scott=# explain analyze </font></tt><br>
<tt><font color="#3333ff">scott-# select count(*) from emp e1,emp e2,
emp e3, emp e4,emp e5, emp e6;</font></tt><br>
<tt><font color="#3333ff">
QUERY PLAN </font></tt><br>
<tt><font color="#3333ff">
</font></tt><br>
<tt><font color="#3333ff">--------------------------------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff">---------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff"> Aggregate
(cost=1052615959933232.62..1052615959933232.62 rows=1 width=0) (actu</font></tt><br>
<tt><font color="#3333ff">al time=13684.811..13684.811 rows=1 loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Nested Loop
(cost=16.60..935975959933232.62 rows=46656000000000000 width</font></tt><br>
<tt><font color="#3333ff">=0) (actual time=0.038..10508.108
rows=7529536 loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Nested Loop
(cost=0.00..2855959933216.00 rows=77760000000000 width</font></tt><br>
<tt><font color="#3333ff">=0) (actual time=0.025..820.829 rows=537824
loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Nested Loop
(cost=0.00..4759933216.00 rows=129600000000 widt</font></tt><br>
<tt><font color="#3333ff">h=0) (actual time=0.021..59.701 rows=38416
loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Nested Loop
(cost=0.00..7933216.00 rows=216000000 widt</font></tt><br>
<tt><font color="#3333ff">h=0) (actual time=0.018..4.424 rows=2744
loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Nested
Loop (cost=0.00..13216.00 rows=360000 wid</font></tt><br>
<tt><font color="#3333ff">th=0) (actual time=0.015..0.333 rows=196
loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Seq
Scan on emp e1 (cost=0.00..16.00 rows=</font></tt><br>
<tt><font color="#3333ff">600 width=0) (actual time=0.009..0.018
rows=14 loops=1)</font></tt><br>
<tt><font color="#3333ff"> -> Seq
Scan on emp e6 (cost=0.00..16.00 rows=</font></tt><br>
<tt><font color="#3333ff">600 width=0) (actual time=0.002..0.010
rows=14 loops=14)</font></tt><br>
<tt><font color="#3333ff"> -> Seq Scan
on emp e5 (cost=0.00..16.00 rows=600 wi</font></tt><br>
<tt><font color="#3333ff">dth=0) (actual time=0.002..0.008 rows=14
loops=196)</font></tt><br>
<tt><font color="#3333ff"> -> Seq Scan on emp
e4 (cost=0.00..16.00 rows=600 width=0)</font></tt><br>
<tt><font color="#3333ff"> (actual time=0.001..0.007 rows=14
loops=2744)</font></tt><br>
<tt><font color="#3333ff"> -> Seq Scan on emp e3
(cost=0.00..16.00 rows=600 width=0) (actu</font></tt><br>
<tt><font color="#3333ff">al time=0.001..0.007 rows=14 loops=38416)</font></tt><br>
<tt><font color="#3333ff"> -> Materialize
(cost=16.60..22.60 rows=600 width=0) (actual time=0.00</font></tt><br>
<tt><font color="#3333ff">0..0.006 rows=14 loops=537824)</font></tt><br>
<tt><font color="#3333ff"> -> Seq Scan on emp e2
(cost=0.00..16.00 rows=600 width=0) (actu</font></tt><br>
<tt><font color="#3333ff">al time=0.003..0.012 rows=14 loops=1)</font></tt><br>
<tt><font color="#3333ff"> Total runtime: 13684.977 ms</font></tt><br>
<tt><font color="#3333ff">(14 rows)</font></tt><br>
<tt><font color="#3333ff">Time: 13714.576 ms</font></tt><br>
<tt><font color="#3333ff">scott=# \d+ emp</font></tt><br>
<tt><font color="#3333ff"> Table
"public.emp"</font></tt><br>
<tt><font color="#3333ff"> Column | Type |
Modifiers | Storage | Description </font></tt><br>
<tt><font color="#3333ff">----------+-----------------------------+-----------+----------+-------------</font></tt><br>
<tt><font color="#3333ff"> empno | smallint |
not null | plain | </font></tt><br>
<tt><font color="#3333ff"> ename | character varying(10) |
not null | extended | </font></tt><br>
<tt><font color="#3333ff"> job | character varying(9)
| | extended | </font></tt><br>
<tt><font color="#3333ff"> mgr | smallint
| | plain | </font></tt><br>
<tt><font color="#3333ff"> hiredate | timestamp without time zone
| | plain | </font></tt><br>
<tt><font color="#3333ff"> sal | double precision
| | plain | </font></tt><br>
<tt><font color="#3333ff"> comm | double precision
| | plain | </font></tt><br>
<tt><font color="#3333ff"> deptno | smallint
| | plain | </font></tt><br>
<tt><font color="#3333ff">Indexes:</font></tt><br>
<tt><font color="#3333ff"> "emp_pkey" PRIMARY KEY, btree (empno)</font></tt><br>
<tt><font color="#3333ff"> "emp_deptno_i" btree (deptno)</font></tt><br>
<tt><font color="#3333ff"> "emp_ename_id" btree (ename)</font></tt><br>
<tt><font color="#3333ff"> "ind_emp_deptno" btree (deptno)</font></tt><br>
<tt><font color="#3333ff">Foreign-key constraints:</font></tt><br>
<tt><font color="#3333ff"> "fk_deptno" FOREIGN KEY (deptno)
REFERENCES dept(deptno)</font></tt><br>
<tt><font color="#3333ff">Has OIDs: no</font></tt><br>
</blockquote>
<br>
Now, Oracle 11.2.0.1 with the April patch:<br>
<blockquote><tt><font color="#3333ff">SQL> set autotrace on</font></tt><br>
<tt><font color="#3333ff">SQL> select count(*) from emp e1,emp e2,
emp e3, emp e4,emp e5, emp e6;</font></tt><br>
<br>
<tt><font color="#3333ff"> COUNT(*)</font></tt><br>
<tt><font color="#3333ff">----------</font></tt><br>
<tt><font color="#3333ff"> 7529536</font></tt><br>
<br>
<tt><font color="#3333ff">Elapsed: 00:00:00.85</font></tt><br>
<br>
<tt><font color="#3333ff">Execution Plan</font></tt><br>
<tt><font color="#3333ff">----------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff">Plan hash value: 1049923164</font></tt><br>
<br>
<tt><font color="#3333ff">------------------------------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff">| Id | Operation |
Name | Rows | Cost (%CPU)| Time |</font></tt><br>
<tt><font color="#3333ff">------------------------------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff">| 0 | SELECT STATEMENT
| | 1 | 157K (1)| 00:31:25 |</font></tt><br>
<tt><font color="#3333ff">| 1 | SORT AGGREGATE
| | 1 | | |</font></tt><br>
<tt><font color="#3333ff">| 2 | MERGE JOIN CARTESIAN
| | 7529K| 157K (1)| 00:31:25 |</font></tt><br>
<tt><font color="#3333ff">| 3 | MERGE JOIN CARTESIAN
| | 537K| 11224 (1)| 00:02:15 |</font></tt><br>
<tt><font color="#3333ff">| 4 | MERGE JOIN CARTESIAN
| | 38416 | 808 (1)| 00:00:10 |</font></tt><br>
<tt><font color="#3333ff">| 5 | MERGE JOIN CARTESIAN
| | 2744 | 62 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 6 | MERGE JOIN CARTESIAN
| | 196 | 7 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 7 | INDEX FULL SCAN |
PK_EMP | 14 | 1 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 8 | BUFFER SORT
| | 14 | 6 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 9 | INDEX FAST FULL SCAN|
PK_EMP | 14 | 0 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 10 | BUFFER SORT
| | 14 | 62 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 11 | INDEX FAST FULL SCAN |
PK_EMP | 14 | 0 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 12 | BUFFER SORT
| | 14 | 808 (1)| 00:00:10 |</font></tt><br>
<tt><font color="#3333ff">| 13 | INDEX FAST FULL SCAN |
PK_EMP | 14 | 0 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 14 | BUFFER SORT
| | 14 | 11224 (1)| 00:02:15 |</font></tt><br>
<tt><font color="#3333ff">| 15 | INDEX FAST FULL SCAN |
PK_EMP | 14 | 0 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">| 16 | BUFFER SORT
| | 14 | 157K (1)| 00:31:25 |</font></tt><br>
<tt><font color="#3333ff">| 17 | INDEX FAST FULL SCAN |
PK_EMP | 14 | 0 (0)| 00:00:01 |</font></tt><br>
<tt><font color="#3333ff">------------------------------------------------------------------------------</font></tt><br>
<br>
<br>
<tt><font color="#3333ff">Statistics</font></tt><br>
<tt><font color="#3333ff">----------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff"> 0 recursive calls</font></tt><br>
<tt><font color="#3333ff"> 0 db block gets</font></tt><br>
<tt><font color="#3333ff"> 21 consistent gets</font></tt><br>
<tt><font color="#3333ff"> 0 physical reads</font></tt><br>
<tt><font color="#3333ff"> 0 redo size</font></tt><br>
<tt><font color="#3333ff"> 425 bytes sent via SQL*Net to
client</font></tt><br>
<tt><font color="#3333ff"> 419 bytes received via SQL*Net
from client</font></tt><br>
<tt><font color="#3333ff"> 2 SQL*Net roundtrips to/from
client</font></tt><br>
<tt><font color="#3333ff"> 5 sorts (memory)</font></tt><br>
<tt><font color="#3333ff"> 0 sorts (disk)</font></tt><br>
<tt><font color="#3333ff"> 1 rows processed</font></tt><br>
<br>
<tt><font color="#3333ff">SQL> </font></tt><br>
<br>
<tt><font color="#3333ff">SQL> select * from v$version;</font></tt><br>
<br>
<tt><font color="#3333ff">BANNER</font></tt><br>
<tt><font color="#3333ff">--------------------------------------------------------------------------------</font></tt><br>
<tt><font color="#3333ff">Oracle Database 11g Enterprise Edition
Release 11.2.0.1.0 - Production</font></tt><br>
<tt><font color="#3333ff">PL/SQL Release 11.2.0.1.0 - Production</font></tt><br>
<tt><font color="#3333ff">CORE 11.2.0.1.0 Production</font></tt><br>
<tt><font color="#3333ff">TNS for Linux: Version 11.2.0.1.0 -
Production</font></tt><br>
<tt><font color="#3333ff">NLSRTL Version 11.2.0.1.0 - Production</font></tt><br>
</blockquote>
<br>
Both databases have current statistics, the data is exactly the same,
as shown by the results. Oracle finished in 0.85 second (850
milliseconds) while PostgreSQL took 2127.345 ms, 2.5 times slower than
Oracle. The difference that is obvious is the access path: Postgres
chose nested loops, while Oracle chose merge join and utilized the
primary key for the table. Postgres, apparently cannot do "fast full
scan" of an index and doesn't know how to utilize the primary key in
cases like this. However, my gripe is that the optimizer should have
selected merge join, just like Oracle did. In this case, nested loops
are definitely the wrong choice. Rule based optimizers, the kind of the
optimizer that takes into consideration only the structure of the
table, usually ends up being dominated by the nested loops method.
Nested loops method usually dominates the OLTP type applications but
can really mess up large reports. I am under the impression that
Postgres query planner is geared toward the OLTP type of the database.
Maybe a new parameter is needed that would somehow shift gears to "data
warehouse use", on demand? I have to say, the advantage is still on the
side of Sauron.<br>
<br>
<br>
<pre class="moz-signature" cols="72">--
Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
<a class="moz-txt-link-abbreviated" href="http://www.vmsinfo.com">www.vmsinfo.com</a>
</pre>
</div>
<pre class="moz-signature" cols="72">--
Mladen Gogala
Sr. Oracle DBA
1500 Broadway
New York, NY 10036
(212) 329-5251
<a class="moz-txt-link-abbreviated" href="http://www.vmsinfo.com">www.vmsinfo.com</a>
</pre>
</body>
</html>