Recently I had some problem in performance of my query. The thing is described here: poor Hibernate select performance comparing to running directly – how debug?
After long time of struggling, I’ve finally discovered that the query with select prefix like:
SELECT sth.* FROM Something as sth...
Is 300x times slower then query started this way:
SELECT * FROM Something as sth..
Could somebody help me, and answer why is that so? Some external documents on this would be really useful.
The table used for testing was:
SALES_UNIT table contains some basic info abot sales unit node such as name and etc. The only association is to table SALES_UNIT_TYPE, as ManyToOne. The primary key is ID and field VALID_FROM_DTTM which is date.
SALES_UNIT_RELATION contains relation PARENT-CHILD between sales unit nodes. Consists of SALES_UNIT_PARENT_ID, SALES_UNIT_CHILD_ID and VALID_TO_DTTM/VALID_FROM_DTTM. No association with any tables. The PK here is ..PARENT_ID, ..CHILD_ID and VALID_FROM_DTTM
The actual queries I’ve used were:
SELECT s.*
FROM sales_unit s LEFT JOIN sales_unit_relation r
on (s.sales_unit_id = r.sales_unit_child_id)
WHERE r.sales_unit_child_id IS NULL
SELECT *
FROM sales_unit s LEFT JOIN sales_unit_relation r
on (s.sales_unit_id = r.sales_unit_child_id)
WHERE r.sales_unit_child_id IS NULL
Same query, both uses left join and only difference is with select.
they are two different queries of course. the plan CAN change with the selects being different. i.e. in the sth.* it may be choosing a full/fast full index scan on the left joined table. whereas on the first it will possibly be a full table scan.
in order to help you further, can we see the plans please? preferably do this in SQL*PLUS
EDIT
given your explain plan, you see theres CARDINALITY=1 on every step? you’ve gathered stats when the tables were empty! see this:
so see it used 200314 IO and took a few seconds. Also see ROWS = 1 on every step (i.e the full scans)..lets gather stats:
and now rerun:
SQL> select s.* from sales_unit s left join sales_unit_relation r on (s.sales_unit_id = r.child_sales_unit_id) where r.child_sales_unit_id is null;
now we used 2537 gets only and the plan shows the right ROWS and a HASH join (better for our needs). my test tables are probably smaller than your real ones which is why the timings are closer