[Solved] Broadcasting multiple view in SQL in pyspark

EverSQL Database Performance Knowledge Base

Broadcasting multiple view in SQL in pyspark

I want to use BROADCAST hint on multiple small tables while joining with a large table. In the example below SMALLTABLE2 is joined multiple times with the LARGETABLE on different joining columns. Now to get the better performance I want both SMALLTABLE1 and SMALLTABLE2 to be BROADCASTED. Can this be achieved by simply adding the hint /* BROADCAST (B,C,D,E) */ or there is a better solution? SMALLTABLE1 & SMALLTABLE2 I am getting the data by querying HIVE tables in a Dataframe and then using createOrReplaceTempView to create a view as SMALLTABLE1 & SMALLTABLE2; which is later used in the query like below.

Is there anyway BROADCASTING view created using createOrReplaceTempView function?

SELECT A.COL1, A.COL2, A.COL3, B.COL4, C.COL5, D.COL6, E.COL7
FROM LARGETABLE A
JOIN SMALLTABLE1 B
ON A.LCOL = B.SCOL
JOIN SMALLTABLE2 C
ON A.LCOL1 = C.SCOL
JOIN SMALLTABLE2 D
ON A.LCOL2 = D.SCOL
JOIN SMALLTABLE2 E
ON A.LCOL3 = E.SCOL

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `SMALLTABLE1` ADD INDEX `smalltable1_idx_scol` (`SCOL`);
ALTER TABLE `SMALLTABLE2` ADD INDEX `smalltable2_idx_scol` (`SCOL`);
The optimized query:
SELECT
        A.COL1,
        A.COL2,
        A.COL3,
        B.COL4,
        C.COL5,
        D.COL6,
        E.COL7 
    FROM
        LARGETABLE A 
    JOIN
        SMALLTABLE1 B 
            ON A.LCOL = B.SCOL 
    JOIN
        SMALLTABLE2 C 
            ON A.LCOL1 = C.SCOL 
    JOIN
        SMALLTABLE2 D 
            ON A.LCOL2 = D.SCOL 
    JOIN
        SMALLTABLE2 E 
            ON A.LCOL3 = E.SCOL

Related Articles



* original question posted on StackOverflow here.