max date [message #228354] |
Mon, 02 April 2007 12:16 |
svercha
Messages: 2 Registered: April 2007
|
Junior Member |
|
|
hello, I was wondering if I can get advise on this. I have 5 tables that among other things all have an INSERT_DATE column in them. So when these tables are populated, sysdate is inserted in these columns. I have to find a max date of all these dates in these 5 tables. So I can accomplish this by doing max and then max with union. So my query looks as follows:
select max(mxDt) as lastDate
from (select max(tbl1.insert_date) as mxDt from table1
UNION
select max(tbl2.insert_date) as mxDt from table2
UNION
select max(tbl3.insert_date) as mxDt from table3
UNION
select max(tbl4.insert_date) as mxDt from table4
UNION
select max(tbl5.insert_date) as mxDt from table5
)
this works, but is kind of slow..is there a better way to accomplish this? thank you in advace.
|
|
|
|
|
|
Re: max date [message #228397 is a reply to message #228362] |
Mon, 02 April 2007 22:38 |
rleishman
Messages: 3728 Registered: October 2005 Location: Melbourne, Australia
|
Senior Member |
|
|
Whoa! Back up!
Revert to your original query, but create an index on insert_date in each table. When you perform a MAX() on an indexed column, Oracle can pick the maximum value straight off the top of the index. You may have to gather statistics first.
If (as suggested earlier) you only have a MAX on the outer query, Oracle will have to fast-full scan all of the indexes instead, which will be much slower.
With your original query and the indexes in place, you may use UNION or UNION ALL; a unique sort of 5 rows will trouble Oracle too much.
Ross Leishman
|
|
|