Hash join with partition limit from third table


Peter

My current problem is in 11g, but I'm also interested in how to solve this more intelligently in future releases.

I want to join two tables. Table A has 10 million rows, and Table B is huge, with a billion records on about a thousand partitions. A partition has about 10 million records. I didn't join the partition key. For most rows in table A, one or more rows will be found in table B. example:

select * from table_a a
inner join table_b b on a.ref = b.ref

The above will return about 50 million rows, and the results are from about 30 partitions of table b. I'm assuming the hash join is the correct join, hash table a and FTSing/index scan table b.

So 970 partitions were scanned for no reason. Also, I have a third query that tells oracle which 30 partitions to check for a join. Third query example:

select partition_id from table_c

This query gives exactly 30 partitions for the above query.

my question:

In PL/SQL, this problem can be solved by

  1. select 30 partition_ids into a variable (it's just aselect listagg(partition_id,',') ... into v_partitions from table_c
  2. Execute my query like this:

    execute immediate 'select * from table_a a 
    inner join table_b b on a.ref = b.ref 
    where b.partition_id in ('||v_partitions||')' into ...
    

Let's say this is done in 10 minutes.

Now, how can this be done in the same amount of time using pure SQL ?

just simply write

select * from table_a a
inner join table_b b on a.ref = b.ref 
where b.partition_id in (select partition_id from table_c)

It doesn't seem to do this, or maybe I'm aiming for the wrong plan.

I think the plan I want is

hash join
    table a
    nested loop
       table c
       partition pruning here
           table b

However, this will not recover within 10 minutes.

So, how to do this in SQL and for what execution plan? A variant that I haven't tried that might be a solution is

nested loop
   table c
   hash join
       table a
       partition pruning here (pushed predicate from the join to c)
            table b

My other feeling is that the solution might lie in joining table a to table c (but not sure what) and then joining this result to table b.

I'm not asking you to type everything out for me. Just a general idea of ​​how to do this in SQL (get the partition limit from the query) - what should I plan against?

thank you very much! Peter

Peter

Thank you all for discussing this with me. In my case this was solved by adding a join path between table_c and table_a and by overloading the join condition as follows (not by myself). In my case this was achieved by adding the column partition_id to table_a:

select * from
  table_c c
  JOIN table_a a ON (a.partition_id = c.partition_id)
  JOIN table_b b ON (b.partition_id = c.partition_id and b.partition_id = a.partition_id and b.ref = a.ref)

Here is the plan you want:

leading(c,b,a) use_nl(c,b) swap_join_inputs(a) use_hash(a)

So you get:

hash join
    table a
    nested loop
       table c
       partition list iterator
           table b

Related


SQL Join 2 case from third table

CompleteNeewb background I have three tables: MetaMachine(MM) MetaShift (MS Machine Data (MD) MM contains all relevant static data about a specific machine, including data on the shifts of the operating workers. MS contains different shifts, sub-shifts, StartT

SQL Join 2 case from third table

CompleteNeewb background I have three tables: MetaMachine(MM) MetaShift (MS Machine Data (MD) MM contains all relevant static data about a specific machine, including data on the shifts of the operating workers. MS contains different shifts, sub-shifts, StartT

SQL Join 2 case from third table

CompleteNeewb background I have three tables: MetaMachine(MM) MetaShift (MS Machine Data (MD) MM contains all relevant static data about a specific machine, including data on the shifts of the operating workers. MS contains different shifts, sub-shifts, StartT

SQL Join 2 case from third table

CompleteNeewb background I have three tables: MetaMachine(MM) MetaShift (MS Machine Data (MD) MM contains all relevant static data about a specific machine, including data on the shifts of the operating workers. MS contains different shifts, sub-shifts, StartT

SQL Join 2 case from third table

CompleteNeewb background I have three tables: MetaMachine(MM) MetaShift (MS Machine Data (MD) MM contains all relevant static data about a specific machine, including data on the shifts of the operating workers. MS contains different shifts, sub-shifts, StartT

join table insert third

Bella Table 1 id class_id class 1 2 3 Table 2 sameid class_id 2 20 3 30 1 40 table 3 class_id desc 30 forest 40 urban 20 water I want to insert Table3.class_id and Table3.desc into Table1.class_id

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to display LEFT JOIN result with condition from third table?

Mihek Mark I have the following table in a database called racing : Race race_id Meeting_id off-duty time title Meeting Meeting_id Meeting_date result result_id race_id Location horse I've been given a query task that will get all today's race titles + non-rac

How to display LEFT JOIN result with condition from third table?

Michael Mark I have the following table in a database called racing : Race race_id Meeting_id off-duty time title Meeting Meeting_id Meeting_date result result_id race_id Location horse I've been given a task to execute a query that will get all today's race t

Join two tables based on data from a third table

2020db9 I have 3 tables, let's call them for this exercise: Products, Price, and Discount. I'm trying to join the Products and Price tables only if the ProductID is found in Discount.ProductID (ProductID column in the discount table). product: Product number s

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to sort from a third table (related to Max(), join)

Josh Poole Let me illustrate the problem with the following example: (Long but easy to understand) This is what my MySQL database looks like: Table Name:general_info Movie_ID movie_title 1 Iron Man 2 Superman 3 Batman Table Na

How to display LEFT JOIN result with condition from third table?

Michael Mark I have the following table in a database called racing : Race race_id Meeting_id off-duty time title Meeting Meeting_id Meeting_date result result_id race_id Location horse I've been given a task to execute a query that will get all today's race t

How to join a table and take limit from the same table

User 11727742 The company database has information about table ID, ORDER_DATE, STATUS, CUSTOMER_ID, and the column name table name is ORDERS. 1. Need to extract the info which is not delivered. 2. If there is more than 5 orders select one with lowest order id

The limit in the join table is 1?

Leonia table ip2countryThere are 250,000 rows (ip ranges inserted in ascending order by from_ip) sessionsThere are 50 lines Obvious and slow (2.687 seconds): SELECT s.*, ip.country FROM sessions s JOIN ip2country ip ON s.ip_addr BETWEEN ip.from_ip AND

The limit in the join table is 1?

Leonia table ip2countryThere are 250,000 rows (ip ranges inserted in ascending order by from_ip) sessionsThere are 50 lines Obvious and slow (2.687 seconds): SELECT s.*, ip.country FROM sessions s JOIN ip2country ip ON s.ip_addr BETWEEN ip.from_ip AND

Join two tables on a third table

Jeremy I can join two tables on one value, but how can I join two tables with different values using a third table that contains two values? Such as: Table A CustomerID info info Table B CustomerName info info Table C CustomerID CustomerName

Join two table data in third table

vim I have two tables, one is sensor_info and the other is bays_info. this is the table id Sensor_Name stn_id value Station_id 1 Sensor1 0 4.4 1 2 Sensor2 0 2.1 1 3 Sensor3 0 3.2 1 4 Se