Attached is a fiddle that lays out my schema with a comment along side each table to indicate the number of rows in each of the aforementioned tables. The query looks like so, in case the fiddle is blocked:
select distinct cat_name,cat_age, co_cat_owners_id,cat_weight,cs_is_alive,os_is_current, cos_is_current,
sum(cat_age) over(partition by co_cat_owners_id) running_total
from
(
select co.cat_owners_id co_cat_owners_id,
co.cat_id co_cat_id,
co.owner_id co_owner_id,
co.vet_id co_vet_id,
cos.is_current cos_is_current,
os.is_current os_is_current,
cs.is_alive cs_is_alive,
cat.name cat_name,
cat.age cat_age,
cat.weight cat_weight
from cat_owners co,
cat_owner_statuses cos,
cat_statuses cs,
cats cat,
owners o,
owner_statuses os
where o.owner_id = co.owner_id
and cat.cat_id = co.cat_id
and cos.last_visit >= sysdate - 4/24
)
where cs_is_alive = '1'
and (cos_is_current = '1' OR os_is_current='1')
group by cat_name,cat_age,cat_weight,cs_is_alive,os_is_current,co_cat_owners_id,cos_is_current;
In my development environment the explain plan lays out very closely to what is inside the fiddle, in terms of steps, however I do have several steps where Memory size is 15E (exabytes) with a row count of 4000P (petabytes). My question is where along the lines of index creation/bad SQL did I manage to generate a 15 exabyte solution to a problem that should be solvable in far less space and time. I have noticed that tweaking some of the composite index creation steps yields slightly different results, but I am still blocked by an Exabyte space requirement.
NOTE
In case someone in the future doesn’t read all of the comments, running the following function in conjunction with correct joins helped:
analyze table table_name_here compute statistics;
Your query has 6 tables in the
FROMclause but you have only specified two join conditions.OWNERShas 10 million rows,CAT_OWNERShas 120 million rows so I’m guessing that the join produces 120 million rows. Then you join toCATSwhich has 1 million rows so I’m assuming at this point that you’ve got 120 million rows. From there, however, you have no more join conditions. So your 120 million row interim result gets cartesian joined with the 500 million rowCAT_OWNER_STATUSEStable which produces 120 million * 500 million rows which is 60 quadrillion rows. Cartesian join that to the 10 million rowCAT_STATUSEStable and you end up with 60 quadrillion * 10 million rows and now we’re up to 6 * 10^23 rows. And then you Cartesian join that again toOWNER_STATUSESwhich has 90 million rows which gives you 5.4 * 10^31 rows. If you want a reasonable number of results, you are going to need to specify additional join conditions to avoid the Cartesian products.It is, unfortunately, a bit hard to follow the data model in your fiddle because there appear to be a number of potentially contradictory ways to join your tables together.
CAT_VETSmaps cats to vets but so doesCAT_OWNERS, for example. Without data and expected results, that makes it very tough to guess at how the tables should be joined. My guess is that you want something like this modified fiddle