Solr5.4 indexing using DataImportHandler(joins) - join

I am using Solr 5.4. Working on indexing entities using Data Import Handler which are having inner joins and left joins as below.
Does solr5.4 supports inner joins and left joins?
I am having a query which has relationship between 7 tables. I am doing inner joins for few and left joins for few tables.
below is my query declared in data-config.xml file.
SELECT rel.*,
PRODUCT_SRC_SYS.PRODUCT_SRC_NME AS sourceName
FROM (SELECT REF.*,
PRODUCT_RLSP.OBJ_ID AS relObjId
FROM (SELECT prdct.*,
PRODUCT_CATEGORY.OBJ_ID AS refObjId
PRODUCT_CATEGORY_MAPG.category_mapng_Name
AS refCategoryMapngName,
PRODUCT_CATEGORY_LIFE_CYL.LIFE_CYL_STAT_TYP
AS refStatus
FROM (SELECT PRODUCT.RGSTRY_ID
AS id
PRODUCT.OBJ_ID
AS obj_id,
PRODUCT.PRODUCT_NAME
As product_name,
PRODUCT_STAT.PRODUCT_STAT_TYP
AS status
FROM PRODUCT
LEFT JOIN PRODUCT_STAT
ON PRODUCT.OBJ_ID =
PRODUCT_STAT.OBJ_ID)
prdct
LEFT JOIN PRODUCT_CATEGORY
ON prdct.product_name =
PRODUCT_CATEGORY.category_name
INNER JOIN PRODUCT_CATEGORY_MAPG
ON PRODUCT_CATEGORY.OBJ_ID =
PRODUCT_CATEGORY_MAPG.OBJ_ID
INNER JOIN PRODUCT_CATEGORY_LIFE_CYL
ON PRODUCT_CATEGORY.OBJ_ID =
PRODUCT_CATEGORY_LIFE_CYL.OBJ_ID)
REF
LEFT JOIN PRODUCT_RLSP
ON REF.obj_id =
PRODUCT_RLSP.OBJ_ID) rel
LEFT JOIN PRODUCT_SRC_SYS
ON rel.obj_id = PRODUCT_SRC_SYS.OBJ_ID
Any help is highly appreciated!!

Related

How do you conduct an outer join in typeorm?

I am using typeorm in a project and want to conduct a left outer join. After going through their docs there is no mention of this at all and i am not convinced that left join will do what i need it to do. Is there a way to conduct an outer join in typeorm?
Just to share, this is the query i am trying to convert to typeorm:
SELECT bsm."bId"
FROM (
SELECT bs."id" "statusId", bs.label label, bs_mapping."bId" "bId"
FROM bs_mapping
LEFT JOIN bs
ON bs.id = bs_mapping."binStatusId"
) bsm
LEFT OUTER JOIN tt_bs
ON tt_bs."statusId" = bsm."statusId"
WHERE ((bsm.label = 'inactive' OR bsm.label = 'block') AND
tt_bs."ttId" IS NULL')
GROUP by bsm."bId"

Hive-How to join tables with OR clause in ON statement

I've got the following problem. In my oracle db I have query as follows:
select * from table1 t1
inner join table2 t2 on
(t1.id_1= t2.id_1 or t1.id_2 = t2.id_2)
and it works perfectly.
Nowadays I need to re-write query on hive. I've seen that OR clause doesn't work in JOINS in hive (error warning : 'OR not supported in JOIN').
Is there any workaround for this except splitting query between two separate and union them?
Another way is to union two joins, e.g.,
select * from table1 t1
inner join table2 t2 on
(t1.id_1= t2.id_1)
union all
select * from table1 t1
inner join table2 t2 on
(t1.id_2 = t2.id_2)
Hive does not support non-equi joins. Common approach is to move join ON condition to the WHERE clause. In the worst case it will be the CROSS JOIN + WHERE filter, like this:
select *
from table1 t1
cross join table2 t2
where (t1.id_1= t2.id_1 or t1.id_2 = t2.id_2)
It may work slow because of rows multiplication by CROSS JOIN.
You can try to do two LEFT joins instead of CROSS and filter out cases when both conditions are false (like INNER JOIN in your query). This may perform faster than cross join because will not multiply all the rows. Also columns selected from second table can be calculated using NVL() or coalesce().
select t1.*,
nvl(t2.col1, t3.col1) as t2_col1, --take from t2, if NULL, take from t3
... calculate all other columns from second table in the same way
from table1 t1
left join table2 t2 on t1.id_1= t2.id_1
left join table2 t3 on t1.id_2 = t3.id_2
where (t1.id_1= t2.id_1 OR t1.id_2 = t3.id_2) --Only joined records allowed likke in your INNER join
As you asked, no UNION is necessary.

SQL Join issue with 3 tables and a subquery

Tables Involved:
account, user, service, accesshist
I want to include all records in the account table, and only the data from the other tables when it exists.
Count from account: 5064
rows returned from query below: 4915
select u.last_name, u.first_name, a.username, ll.mxlogin, si.servicename, a.islockedout
from account a
join service si on a.serviceid = si.serviceid
left outer join user u on u.loginid = a.username
left outer join(select max(loginattemptdate) as MxLogin, usernameattempted from accesshist where isloginsuccessful = 1
group by usernameattempted) ll
on a.username = ll.usernameattempted
where a.isenabled = 1
order by ll.mxlogin, u.last_name
I've narrowed it down that the subquery join is the part causing the number of rows to be reduced, but I am unsure how to correct it. Any insight is greatly appreciated!
Have you tried changing the first join to a left outer join?
select u.last_name, u.first_name, a.username, ll.mxlogin, si.servicename, a.islockedout
from account a
left outer join service si on a.serviceid = si.serviceid

query from multiple tables using inner joins takes long to execute

I have an sql query below that is taking too long to execute. kindly check the query and optimise it for me, i need to count number of files from a file_Actions table but combining it three other tables using inner join
SELECT count(*) as total
FROM (SELECT t1.cfid as cfid,MAX(t1.timestamp) d
FROM file_actions t1
INNER JOIN case_files t2 ON t2.cfid=t1.cfid
INNER JOIN case_file_allocations t3 ON t1.cfid=t3.cfid
INNER JOIN cbeta_user t4
WHERE t4.id=t1.user_id
AND t4.team_leader='$user' and t2.closed<>'yes' AND
t2.deleted<>1 AND
t3.reallocated<>'yes' GROUP BY t1.cfid) a
WHERE d < '$yesterday'
I think it is the inner joins that causes the query to take so long to execute causing the system to slow
Try including the WHERE d < '$yesterday' into the subquery a. Remove the fields and place the Count(*). If your tables aren't indexed on those values that you are using for conditions and relations, try to make an index.
SELECT count(*) as total
FROM file_actions t1
INNER JOIN case_files t2
ON t2.cfid=t1.cfid
INNER JOIN case_file_allocations t3
ON t1.cfid=t3.cfid
INNER JOIN cbeta_user t4
WHERE t4.id=t1.user_id
AND t4.team_leader='$user' and t2.closed<>'yes'
AND t2.deleted<>1
AND t3.reallocated<>'yes'
AND d < '$yesterday'
GROUP BY t1.cfid
Recommended reading: http://www.code-fly.com/5-tips-to-make-your-sql-queries-faster/

Number of joins in select query

I have a business table and in that we have 50 foreign key columns which refers other master data tables.
to fetch all the data my query has to join all the 50 reference tables like
select ct.id , ct.name , ct.description , st.value , pr.value , sv.value , ....
from
core_table ct
left outer join domain_value st on ct.status_fk = st.id
left outer join domain_value pr on ct.priority_fk = pr.id
left outer join domain_value svon ct.severity_fk = sv.id
.......
.......
so like this i need to make 50 left outer joins.
is this right to do 50 left outer joins like this or do we have any other optimized way to achieve this ?
Is too many Left Joins a code smell?
It's a perfectly legitimate solution for some designs.

Resources