Our website sometimes causes extreme server strain due to a complex MySQL query. The site actually goes down.
The webhoster warned that if we don't get this in order they will suspend our account.
Could someone give some pointers at which parts of this query eat the most resources?
Any suggestions on making this better?
SELECT SQL_CALC_FOUND_ROWS wp_s3mv0r_posts.ID
FROM wp_s3mv0r_posts
INNER JOIN wp_s3mv0r_term_relationships ON (wp_s3mv0r_posts.ID = wp_s3mv0r_term_relationships.object_id)
INNER JOIN wp_s3mv0r_postmeta ON ( wp_s3mv0r_posts.ID = wp_s3mv0r_postmeta.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt1 ON ( wp_s3mv0r_posts.ID = mt1.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt2 ON ( wp_s3mv0r_posts.ID = mt2.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt3 ON ( wp_s3mv0r_posts.ID = mt3.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt4 ON ( wp_s3mv0r_posts.ID = mt4.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt5 ON ( wp_s3mv0r_posts.ID = mt5.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt6 ON ( wp_s3mv0r_posts.ID = mt6.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt7 ON ( wp_s3mv0r_posts.ID = mt7.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt8 ON ( wp_s3mv0r_posts.ID = mt8.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt9 ON ( wp_s3mv0r_posts.ID = mt9.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt10 ON ( wp_s3mv0r_posts.ID = mt10.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt11 ON ( wp_s3mv0r_posts.ID = mt11.post_id )
INNER JOIN wp_s3mv0r_postmeta AS mt12 ON ( wp_s3mv0r_posts.ID = mt12.post_id )
WHERE 1=1 AND (
wp_s3mv0r_term_relationships.term_taxonomy_id IN (11,24,25)
) AND (
( wp_s3mv0r_postmeta.meta_key = 'acf_house_minprice' AND CAST(wp_s3mv0r_postmeta.meta_value AS SIGNED) BETWEEN '274990' AND '599990' )
AND
( mt1.meta_key = 'acf_house_minlotwidth' AND CAST(mt1.meta_value AS SIGNED) BETWEEN '16' AND '16' )
AND
( mt2.meta_key = 'acf_location_area' AND CAST(mt2.meta_value AS CHAR) IN ('South of river') )
AND
( mt3.meta_key = 'acf_house_bedroom' AND CAST(mt3.meta_value AS CHAR) = '4' )
AND
( mt4.meta_key = 'acf_house_studyroom' AND CAST(mt4.meta_value AS SIGNED) > '0' )
AND
( mt5.meta_key = 'acf_house_theaterroom' AND CAST(mt5.meta_value AS SIGNED) > '0' )
AND
( mt6.meta_key = 'acf_house_alfresco' AND CAST(mt6.meta_value AS SIGNED) > '0' )
AND
( mt7.meta_key = 'acf_house_activityroom' AND CAST(mt7.meta_value AS SIGNED) > '0' )
AND
( mt8.meta_key = 'acf_house_doublegarage' AND CAST(mt8.meta_value AS SIGNED) > '0' )
AND
( mt9.meta_key = 'acf_house_reargarage' AND CAST(mt9.meta_value AS SIGNED) > '0' )
AND
( mt10.meta_key = 'acf_house_islbeninkitchen' AND CAST(mt10.meta_value AS SIGNED) > '0' )
AND
( mt11.meta_key = 'acf_house_frontmasterbedroom' AND CAST(mt11.meta_value AS SIGNED) > '0' )
AND
( mt12.meta_key = 'acf_house_rearmaster' AND CAST(mt12.meta_value AS SIGNED) > '0' )
) AND wp_s3mv0r_posts.post_type = 'house' AND (wp_s3mv0r_posts.post_status = 'publish')
GROUP BY wp_s3mv0r_posts.ID
ORDER BY wp_s3mv0r_posts.post_date DESC
LIMIT 0, 10
How about this:
SELECT p.ID, count(*) AS 'PostCount'
FROM wp_s3mv0r_posts p INNER JOIN wp_s3mv0r_term_relationships r ON (p.ID = r.object_id)
INNER JOIN wp_s3mv0r_postmeta pm ON (p.ID = pm.post_id)
WHERE
r.term_taxonomy_id IN (11,24,25) AND p.post_type = 'house' AND p.post_status = 'publish' AND (
(pm.meta_key = 'acf_house_minprice' AND pm.meta_value BETWEEN '274990' AND '599990') OR
(pm.meta_key = 'acf_house_minlotwidth' AND pm.meta_value BETWEEN '16' AND '16') OR
(pm.meta_key = 'acf_location_area' AND pm.meta_value = 'South of river') OR
(pm.meta_key = 'acf_house_bedroom' AND pm.meta_value = '4') OR
(pm.meta_key IN ('acf_house_studyroom', 'acf_house_theaterroom', 'acf_house_alfresco', 'acf_house_activityroom',
'acf_house_doublegarage', 'acf_house_reargarage', 'acf_house_islbeninkitchen', 'acf_house_frontmasterbedroom',
'acf_house_rearmaster') AND pm.meta_value > '0'))
GROUP BY p.ID
ORDER BY p.post_date DESC
LIMIT 0, 10
Related
I'm trying to update 2 tables using inner Join and setting values with 2 results from variables. I've finally succeeded to have it work for a SELECT.
I want to updates my tables having a long where clause. And set the result from a select query.
Code is fine with select I get my results :
SET #DefID = "5289";
SELECT pt1.ID, pt1.post_title, pt1.post_content, mt2.meta_value
FROM wp_posts AS pt1
INNER JOIN wp_postmeta AS mt1
ON ( pt1.ID = mt1.post_id )
INNER JOIN wp_postmeta AS mt2
ON ( pt1.ID = mt2.post_id )
INNER JOIN wp_postmeta AS mt3
ON ( pt1.ID = mt3.post_id )
WHERE
pt1.ID != #DefID
AND
pt1.post_title LIKE '%Banda Sea DIVING Cruise with Tidak%'
AND
mt1.meta_key = 'tourmaster-tour-date-avail'
AND
CAST(mt1.meta_value AS DATE) >= '2019-01-23'
AND
mt2.meta_key = '_cornerstone_data'
AND
mt3.meta_key = 'tourmaster-tour-duration'
AND
mt3.meta_value = '10'
AND
pt1.post_type = 'tour'
AND
pt1.post_status = 'publish'
ORDER BY mt1.meta_value ASC
But not working with UPDATE
SET #DefID = "myID";
SET #post_content = ( SELECT wp_posts_bak.post_content FROM wp_posts_bak WHERE wp_posts_bak.ID = #DefID );
SET #meta_value = ( SELECT wp_postmeta_bak.meta_value FROM wp_postmeta_bak WHERE wp_postmeta_bak.post_id = #DefID AND wp_postmeta_bak.meta_key = '_cornerstone_data');
UPDATE wp_posts_bak AS pt1, wp_postmeta_bak AS mt0
INNER JOIN wp_postmeta_bak AS mt1 ON pt1.ID = mt1.post_id
INNER JOIN wp_postmeta_bak AS mt2 ON pt1.ID = mt2.post_id
INNER JOIN wp_postmeta_bak AS mt3 ON pt1.ID = mt3.post_id
SET
pt1.post_content = #post_content,
mt0.meta_value = #meta_value
WHERE
pt1.ID != #DefID
AND
pt1.post_title LIKE '%Banda Sea DIVING Cruise with Tidak%'
AND
mt1.meta_key = 'tourmaster-tour-date-avail'
AND
CAST(mt1.meta_value AS DATE) >= '2019-01-23'
AND
mt2.meta_key = '_cornerstone_data'
AND
mt3.meta_key = 'tourmaster-tour-duration'
AND
mt3.meta_value = '10'
AND
pt1.post_type = 'tour'
AND
pt1.post_status = 'publish'
Got the following error : Unknown column 'pt1.ID' in 'on clause.
I finally found the reason of the problem. In the UPDATE I had to remove the 2 tables.
So the solution is :
SET #DefID = "5289";
SET #post_content = ( SELECT wp_posts_bak.post_content FROM wp_posts_bak WHERE wp_posts_bak.ID = #DefID );
SET #meta_value = ( SELECT wp_postmeta_bak.meta_value FROM wp_postmeta_bak WHERE wp_postmeta_bak.post_id = #DefID AND wp_postmeta_bak.meta_key = '_cornerstone_data');
UPDATE wp_posts_bak AS pt1
INNER JOIN wp_postmeta_bak AS mt1 ON pt1.ID = mt1.post_id
INNER JOIN wp_postmeta_bak AS mt2 ON pt1.ID = mt2.post_id
INNER JOIN wp_postmeta_bak AS mt3 ON pt1.ID = mt3.post_id
SET
pt1.post_content = #post_content,
mt2.meta_value = #meta_value
WHERE
pt1.ID != #DefID
AND
pt1.post_title LIKE '%Banda Sea DIVING Cruise with Tidak%'
AND
mt1.meta_key = 'tourmaster-tour-date-avail'
AND
CAST(mt1.meta_value AS DATE) >= '2019-01-23'
AND
mt2.meta_key = '_cornerstone_data'
AND
mt3.meta_key = 'tourmaster-tour-duration'
AND
mt3.meta_value = '10'
AND
pt1.post_type = 'tour'
AND
pt1.post_status = 'publish'
I've a query like this which is working fine but I want to create a pure CI query from this. I'm new in CI so can anyone help me to figure out how this can be done or suggest me a reference website or link from where i can get help. Thanks
SELECT
user_des,
IFNULL((sent),0) as sent,
IFNULL((viewed),0) as viewed,
CONCAT(IFNULL(fname_usr,user_des),' ',lname_usr,user_des) as fullname,
IFNULL(sum(duration),0) as totalviewtime,
IFNULL((totalviews),0) as views,
IFNULL(sum(sharedcount),0) as shares,
IFNULL(sum(shared),0) as shared
FROM dem_senddemo
INNER JOIN
(SELECT * FROM dem_demos WHERE id_dem IN (746,943,748) AND customer_dem = '1') as demosfiltered
ON demo_des = id_dem
LEFT JOIN
(
(
Select senddemo_wis, sum(duration) as duration, max(datereceived_wis) as datereceived_wis
FROM
(
select senddemo_wis, invited_wis, sum(IFNULL(duration_fea,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM (sta_views
LEFT JOIN sta_featureswisitavisits as s ON visit_fvi = id_vis
LEFT JOIN
(
(SELECT shortvideovid_fea as videoid_fea, shortvideoduration_fea as duration_fea
FROM dem_features
where shortvideoduration_fea > 0)
UNION
(SELECT longvideovid_fea as videoid_fea, longvideoduration_fea as duration_fea
FROM dem_features
where longvideoduration_fea > 0)
) as x
ON videoid_fea = showedvideo_fvi)
LEFT JOIN
sta_wistia as w ON invited_fvi = invited_wis AND s.showedvideo_fvi = w.mediaid_wis AND wistia_vis = email_wis
WHERE countvisit_wis <> 'N'
GROUP BY invited_wis
) as durfea
GROUP BY senddemo_wis
) UNION
(
SELECT senddemo_wis,sum(IFNULL(mainvideoduration_dem ,0) * IFNULL(percentviewed_wis,0)) as duration, max(datereceived_wis) as datereceived_wis
FROM sta_wistia as w
INNER JOIN
(select *, customer_dem as cuss FROM dem_demos) as demcorp ON demo_wis = id_dem AND mainmessagingvideovid_dem = w.mediaid_wis
LEFT JOIN sta_views ON email_wis = wistia_vis
WHERE customer_dem = 1 AND senddemo_wis > 0 AND countvisit_wis <> 'N'
GROUP BY senddemo_wis
)
) as c ON id_des = senddemo_wis
LEFT JOIN
(
SELECT user_des as user2_des, count(id_vis) as totalviews
FROM sta_views
LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748)
GROUP BY user_des
) as z ON user_des = user2_des
LEFT JOIN
(
SELECT user_des as user3_des, count(id_des) as sent
FROM dem_senddemo
LEFT JOIN dem_invited ON id_senddemo = id_des
WHERE createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND demo_des IN (746,943,748) AND first_invited = 'YES'
GROUP BY user_des
) as za ON user_des = user3_des
LEFT JOIN
(
SELECT user_des as user4_des, count(id_vis) as viewed
FROM sta_views LEFT JOIN dem_invited ON id_invited = invited_vis
LEFT JOIN dem_senddemo ON id_des = id_senddemo
WHERE NOT ISNULL(id_senddemo) AND countvisit_wis != 'N' AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59' AND state = 'VIEWED'
AND first_invited = 'YES' AND demo_des IN (746,943,748)
GROUP BY user_des
) as zb ON user_des = user4_des
LEFT JOIN
(
SELECT id_senddemo as iddemdemos2, count(id_senddemo) as shared, allshares as sharedcount
FROM
(
SELECT id_senddemo, count(id_senddemo) as allshares
FROM dem_invited
LEFT JOIN dem_senddemo on id_senddemo = id_des
WHERE first_invited <> 'YES' AND demo_des IN (746,943,748)
GROUP BY id_senddemo
) as x
GROUP BY id_senddemo
) as zc ON iddemdemos2 = id_des
LEFT JOIN
sec_users ON user_des = id_usr
WHERE customer_dem = 1 AND createdon_des BETWEEN '2015-05-31 00:00:00' AND '2015-06-29 23:59:59'
GROUP BY user_des
So, I'm doing a custom wordpress query to pull events (Events are stored in custom fields (meta data) hence the need for the JOINs) and I want to get a count of the rows returned. My problem is that I'm getting a bunch of rows returned each containing a count against each post ID returned.
My query looks a bit like this:
SELECT count(*)
FROM wp_posts AS wposts
LEFT JOIN wp_postmeta AS metafeatured
ON wposts.id = metafeatured.post_id
LEFT JOIN wp_postmeta AS metastart
ON wposts.id = metastart.post_id
LEFT JOIN wp_postmeta AS metaend
ON wposts.id = metaend.post_id
LEFT JOIN wp_term_relationships
ON ( wposts.id = wp_term_relationships.object_id )
LEFT JOIN wp_term_taxonomy
ON ( wp_term_relationships.term_taxonomy_id =
wp_term_taxonomy.term_taxonomy_id )
WHERE wposts.post_type = 'll_events'
AND wposts.post_status = 'publish'
AND ( ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) < 1342997940 )
AND ( metaend.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) > 1342525664 ) )
OR ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) <= 1342997940 )
AND ( metaend.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) >= 1342525664
) )
OR ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) BETWEEN
1342525664 AND 1342997940
)
AND ( metastart.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) BETWEEN
1342525664 AND 1342997940
) )
OR ( ( metastart.meta_key = 'll_events_startdate'
AND Cast(metastart.meta_value AS UNSIGNED) BETWEEN
1342525664 AND 1342997940
)
AND ( metaend.meta_key = 'll_events_enddate'
AND Cast(metaend.meta_value AS UNSIGNED) <= 1 ) ) )
GROUP BY wposts.ID, metastart.post_id, metaend.post_id, wp_term_relationships.object_id
ORDER BY metastart.meta_value ASC
I've tried loads of combinations of group, count and such and can't get the expected result (a single row in the result-set containing a 'count'). Instead, my resultset looks something like:
count(*)
24
30
30
30
12
16
1
... and so on, where I need:
count(*)
139
Just remove the GROUP BY Clause
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags_relations` AS `tr` ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
WHERE (v.video_id IN (
SELECT gc2.video_id
FROM video_categories gc2
WHERE gc2.genre_id IN(44)
GROUP BY gc2.video_id
HAVING COUNT(gc2.video_id)=1))
AND (v.video_id IN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1))
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
Query takes 0.4s~ to run. It's because of subquery in WHERE statement:
v.video_id IN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1
)
Without it, it runs 0.07s~
Maybe try something with advanced joins? shy
Thanks ;)
In MySQL, never use WHERE in (SELECT ... it is notoriously slow.
Use a join instead:
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM video AS v
INNER JOIN video_categories AS gc ON gc.video_id = v.video_id
AND gc2.genre_id = '44'
LEFT JOIN video_genres AS gg ON gg.genre_id = gc.genre_id
LEFT JOIN tags_relations AS tr ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN tags AS tg ON tg.tag_id = tr.tag_id
INNER JOIN tags_relations tr2 ON (tr2.parent_id = v.video_id
AND tr2.tag_id = 14 AND tr2.department=18
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
SELECT `v`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags_relations` AS `tr` ON tr.department=18 AND tr.parent_id = v.video_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
JOIN (
SELECT gc2.video_id
FROM video_categories gc2
WHERE gc2.genre_id IN(44)
GROUP BY gc2.video_id
HAVING COUNT(gc2.video_id)=1
) vc ON v.video_id = vc.video_id
JOIN (
SELECT gc2.parent_id
FROM tags_relations gc2
WHERE gc2.tag_id IN(14) AND gc2.department=18
GROUP BY gc2.parent_id
HAVING COUNT(gc2.parent_id)=1
) tr ON v.video_id = tr.parent_id
GROUP BY `v`.`video_id`
ORDER BY `v`.`video_id` DESC
Edit:
I took a closer look at your query and think it can be reduced to the following:
SELECT `v1`.*
, GROUP_CONCAT(DISTINCT(`gg`.genre_id)) AS `genre_ids`
, GROUP_CONCAT(DISTINCT(`gg`.genre_ru)) AS `genres`
, GROUP_CONCAT(DISTINCT(`tg`.tag_id)) AS `tags_ids`
, GROUP_CONCAT(DISTINCT(`tg`.name)) AS `tags`
FROM (
SELECT v.*
FROM `video` AS `v`
LEFT JOIN `video_categories` AS `gc` ON gc.video_id = v.video_id
LEFT JOIN `tags_relations` AS `tr` ON tr.parent_id = v.video_id
WHERE gc.genre_id IN(44)
AND tr.tag_id IN(14)
AND tr.department=18
GROUP BY `v`.`video_id`
HAVING COUNT(*) = 1
) v1
LEFT JOIN `video_genres` AS `gg` ON gg.genre_id = gc.genre_id
LEFT JOIN `tags` AS `tg` ON tg.tag_id = tr.tag_id
GROUP BY v1.video_id
ORDER BY v1.video_id DESC
ms sql server 2008
SELECT Action.*
FROM Mailing
INNER JOIN ActionCategoryMailingBunch
ON Mailing.MailingID = ActionCategoryMailingBunch.MailingID
INNER JOIN ActionCategory
ON ActionCategoryMailingBunch.ActionCategoryID =
ActionCategory.ActionCategoryID
INNER JOIN Action
ON ActionCategory.ActionCategoryID = Action.ActionCategoryID
WHERE ( Mailing.MailingID = 7 )
AND ( Mailing.MailingID NOT IN (SELECT MailingID
FROM MailingReport) )
AND ( Action.ActionID NOT IN (SELECT ActionID
FROM MailingReport) )
AND ( ActionCategoryMailingBunch.MailingBunchStatusID = 4 )
especially this block
(Mailing.MailingID NOT IN (SELECT MailingID FROM MailingReport))
AND (Action.ActionID NOT IN (SELECT ActionID FROM MailingReport))
Add the below code in the select statement
Left Join MailingReport MR on MR.ActionID = Mailing.MailingID
Add the below code in Where clause
(MR.ActionID is null)
Performance upgrade due to following block
(Mailing.MailingID NOT IN (SELECT MailingID FROM MailingReport))
Final Query
SELECT Action.*
FROM Mailing
INNER JOIN ActionCategoryMailingBunch
ON Mailing.MailingID = ActionCategoryMailingBunch.MailingID
INNER JOIN ActionCategory
ON ActionCategoryMailingBunch.ActionCategoryID =
ActionCategory.ActionCategoryID
INNER JOIN Action
ON ActionCategory.ActionCategoryID = Action.ActionCategoryID
Left Join MailingReport MR on MR.ActionID = Mailing.MailingID
WHERE ( Mailing.MailingID = 7 )
--AND ( Mailing.MailingID NOT IN (SELECT MailingID
-- FROM MailingReport) )
AND
(MR.ActionID is null)
AND ( Action.ActionID NOT IN (SELECT ActionID
FROM MailingReport) )
AND ( ActionCategoryMailingBunch.MailingBunchStatusID = 4 )
Similarly you can do for the second block
Performance due to second block
AND (Action.ActionID NOT IN (SELECT ActionID FROM MailingReport))
Final Query for both blocks
SELECT Action.*
FROM Mailing
INNER JOIN ActionCategoryMailingBunch
ON Mailing.MailingID = ActionCategoryMailingBunch.MailingID
INNER JOIN ActionCategory
ON ActionCategoryMailingBunch.ActionCategoryID =
ActionCategory.ActionCategoryID
INNER JOIN Action
ON ActionCategory.ActionCategoryID = Action.ActionCategoryID
Left Join MailingReport MR on MR.ActionID = Mailing.MailingID
Left Join MailingReport MRA on MRA.ActionID = Action.ActionID
WHERE ( Mailing.MailingID = 7 )
AND
(MR.ActionID is null)
AND
(MRA.ActionID is null)
AND ( ActionCategoryMailingBunch.MailingBunchStatusID = 4 )