Question

SELECT tm.MAGAZINE_ID, tm.publisher_id, tu.begin_date, tu.report_from,
units `MAGAZINE_NAME`
FROM `tbl_itunes_report` tu 
LEFT JOIN tbl_magazine_subscription_dtl tsd 
  ON tsd.subscription_key = tu.sku_key 
  AND ((tu.begin_date >= tsd.start_date 
  AND tu.begin_date < tsd.end_date) OR (tu.begin_date >= tsd.start_date 
  AND tsd.end_date = '0000-00-00')) 
LEFT JOIN tbl_magazine_subscription ts 
  ON ts.magazine_subscription_id = tsd.subs_id 
LEFT JOIN tbl_magazine_issue ti 
  ON ti.PurchaseKey = tu.sku_key AND ti.OS_SELECT = 0 
LEFT JOIN tbl_magazine tm 
  ON tm.magazine_id = ts.magazine_id OR tm.magazine_id =  ti.magazine_id 
WHERE `product_type_identifier` LIKE 'IA%' 
  AND ( tsd.subscription_key IS NOT NULL OR ti.PurchaseKey IS NOT NULL ) 
GROUP BY tm.MAGAZINE_ID,tsd.no_of_issues 
ORDER BY tm.magazine_name, tsd.no_of_issues

Using this query I will generate my report. There are 4 joins in it. In the case tbl_magazine_subscription_dtl, tbl_magazine_issue uses the sku_key field for joining. The sku_key value is present in only one table at a time, but the joining will lead to decreased query speed. Is it possible to check if sku_key is present or not before the join is performed?

Was it helpful?

Solution

I don't completely understand your question, do you want only the rows where sku_key is not null and present in both tbl_magazine_issue and tbl_magazine_subscription_dtl? In that case, you should do an inner join instead of a left outer join on tbl_magazine_issue, and add a AND tu.sku_key IS NOT NULL to the WHERE clause. The query engine should be smart enough to apply this condition before the join.

You should also make sure that sku_key and PurchaseKey are indexed if you're concerned about performance of this join.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top