SELECT contacts.do_not_call -- do_not_call -- , contacts.lead_source lead_source -- , -- jt1_accounts.id account_id, -- jt1_accounts.assigned_user_id account_id_owner, jt1_accounts.name account_name, jt1_accounts.assigned_user_id account_name_owner, jt3_reports_to_link.salutation rel_report_to_name_salutation, jt3_reports_to_link.first_name -- rel_report_to_name_first_name, jt3_reports_to_link.last_name rel_report_to_name_last_name, jt3_reports_to_link.assigned_user_id report_to_name_owner, -- contacts.portal_active portal_active, contacts.preferred_language -- preferred_language, -- jt4_campaign_contacts.name campaign_name, jt4_campaign_contacts.assigned_user_id campaign_name_owner, -- case when jt5_user_sync.id IS NOT NULL then 1 else 0 end sync_contact, contacts.mkto_sync mkto_sync, -- jt7_assigned_user_link.first_name rel_assigned_user_name_first_name, jt7_assigned_user_link.last_name rel_assigned_user_name_last_name, jt7_assigned_user_link.created_by assigned_user_name_owner, -- contacts.acl_team_set_id -- acl_team_set_id, contacts.team_set_id team_set_id, -- LTRIM(RTRIM(CONCAT(IFNULL(jt8_team_link.name,''),' ',IFNULL(jt8_team_link.name_2,'')))) team_name, jt8_team_link.created_by team_name_owner, -- contacts_cstm.nationality_c -- nationality_c, contacts_cstm.gender_c gender_c, contacts_cstm.contact_type_c contact_type_c, contacts.reports_to_id reports_to_id, contacts.campaign_id campaign_id, contacts.assigned_user_id assigned_user_id, contacts.salutation -- rel_full_name_salutation, contacts.first_name rel_full_name_first_name, contacts.last_name rel_full_name_last_name, contacts.phone_mobile phone_mobile, -- jt9_email_addresses_primary.email_address email, contacts.phone_work -- phone_work, -- contacts_cstm.drivers_license_c drivers_license_c, contacts_cstm.interests_c interests_c, contacts_cstm.occupation_c occupation_c, contacts.mkto_lead_score mkto_lead_score, contacts_cstm.mkto_role_c mkto_role_c, -- contacts.mkto_id mkto_id, contacts.birthdate birthdate, contacts.primary_address_street primary_address_street, contacts.primary_address_city primary_address_city, contacts.primary_address_state primary_address_state, -- contacts.primary_address_postalcode primary_address_postalcode, contacts.primary_address_country primary_address_country, -- jt11_created_by_link.first_name rel_created_by_name_first_name, jt11_created_by_link.last_name -- rel_created_by_name_last_name, jt11_created_by_link.created_by created_by_name_owner, jt12_modified_user_link.first_name rel_modified_by_name_first_name, jt12_modified_user_link.last_name rel_modified_by_name_last_name, -- jt12_modified_user_link.created_by modified_by_name_owner, -- contacts.date_entered date_entered, contacts.date_modified date_modified, contacts.title title, contacts.salutation salutation, contacts.first_name first_name, -- contacts.last_name last_name, contacts.created_by created_by, contacts.modified_user_id modified_user_id, -- case when jt13_following_link.id IS NOT NULL then 1 else 0 end following, case when jt15_favorite_link.id IS NOT NULL then -- 1 else 0 end my_favorite, -- contacts.team_id team_id, contacts.id id -- , jt9_email_addresses_primary.email_address email_addresses_primary__email_address FROM contacts INNER JOIN ( select tst.team_set_id from team_sets_teams tst INNER JOIN team_memberships team_memberships ON tst.team_id = team_memberships.team_id AND team_memberships.user_id = '3b502028-67c7-11e7-9de6-fa163e4dd901' AND team_memberships.deleted=0 group by tst.team_set_id -- NOTE: can remove above group by line and it still leaks - just by 3 handles instead of 150 ) contacts_tf on contacts_tf.team_set_id = contacts.team_set_id LEFT JOIN sugarfavorites sf_contacts ON (contacts.id = sf_contacts.record_id -- AND sf_contacts.deleted = 0 -- AND sf_contacts.module = 'Contacts' -- AND sf_contacts.created_by = '3b502028-67c7-11e7-9de6-fa163e4dd901' ) -- LEFT JOIN accounts_contacts jt2_accounts_contacts ON (contacts.id = jt2_accounts_contacts.contact_id AND jt2_accounts_contacts.deleted = 0 AND jt2_accounts_contacts.primary_account = '1') -- LEFT JOIN accounts jt1_accounts ON (jt1_accounts.id = jt2_accounts_contacts.account_id AND jt1_accounts.deleted = 0) -- LEFT JOIN accounts_cstm jt1_accounts_cstm ON (jt1_accounts_cstm.id_c = jt1_accounts.id) -- LEFT JOIN contacts jt3_reports_to_link ON (contacts.reports_to_id = jt3_reports_to_link.id AND jt3_reports_to_link.deleted = 0) -- LEFT JOIN contacts_cstm jt3_reports_to_link_cstm ON (jt3_reports_to_link_cstm.id_c = jt3_reports_to_link.id) -- LEFT JOIN campaigns jt4_campaign_contacts ON (contacts.campaign_id = jt4_campaign_contacts.id AND jt4_campaign_contacts.deleted = 0) -- LEFT JOIN contacts_users jt6_contacts_users ON (contacts.id = jt6_contacts_users.contact_id AND jt6_contacts_users.deleted = 0 AND jt6_contacts_users.user_id = '3b502028-67c7-11e7-9de6-fa163e4dd901') -- LEFT JOIN users jt5_user_sync ON (jt5_user_sync.id = jt6_contacts_users.user_id AND jt5_user_sync.deleted = 0) -- LEFT JOIN users_cstm jt5_user_sync_cstm ON (jt5_user_sync_cstm.id_c = jt5_user_sync.id) -- LEFT JOIN users jt7_assigned_user_link ON (contacts.assigned_user_id = jt7_assigned_user_link.id AND jt7_assigned_user_link.deleted = 0) -- LEFT JOIN users_cstm jt7_assigned_user_link_cstm ON (jt7_assigned_user_link_cstm.id_c = jt7_assigned_user_link.id) -- LEFT JOIN teams jt8_team_link ON (contacts.team_id = jt8_team_link.id AND jt8_team_link.deleted = 0) LEFT JOIN email_addr_bean_rel jt10_email_addr_bean_rel ON (contacts.id = jt10_email_addr_bean_rel.bean_id -- AND jt10_email_addr_bean_rel.deleted = 0 AND jt10_email_addr_bean_rel.bean_module = 'Contacts' AND -- jt10_email_addr_bean_rel.primary_address = '1' ) LEFT JOIN email_addresses jt9_email_addresses_primary ON (jt9_email_addresses_primary.id = jt10_email_addr_bean_rel.email_address_id -- AND jt9_email_addresses_primary.deleted = 0 ) -- LEFT JOIN users jt11_created_by_link ON (contacts.created_by = jt11_created_by_link.id AND jt11_created_by_link.deleted = 0) -- LEFT JOIN users_cstm jt11_created_by_link_cstm ON (jt11_created_by_link_cstm.id_c = jt11_created_by_link.id) -- LEFT JOIN users jt12_modified_user_link ON (contacts.modified_user_id = jt12_modified_user_link.id AND jt12_modified_user_link.deleted = 0) -- LEFT JOIN users_cstm jt12_modified_user_link_cstm ON (jt12_modified_user_link_cstm.id_c = jt12_modified_user_link.id) -- LEFT JOIN subscriptions jt14_subscriptions ON (contacts.id = jt14_subscriptions.parent_id AND jt14_subscriptions.deleted = 0 AND jt14_subscriptions.parent_type = 'Contacts' AND jt14_subscriptions.created_by = -- '3b502028-67c7-11e7-9de6-fa163e4dd901') -- LEFT JOIN users jt13_following_link ON (jt13_following_link.id = jt14_subscriptions.created_by AND jt13_following_link.deleted = 0) -- LEFT JOIN users_cstm jt13_following_link_cstm ON (jt13_following_link_cstm.id_c = jt13_following_link.id) -- LEFT JOIN users jt15_favorite_link ON (jt15_favorite_link.id = sf_contacts.modified_user_id AND jt15_favorite_link.deleted = 0) -- LEFT JOIN users_cstm jt15_favorite_link_cstm ON (jt15_favorite_link_cstm.id_c = jt15_favorite_link.id) -- LEFT JOIN contacts_cstm ON contacts_cstm.id_c = contacts.id WHERE -- contacts.deleted = 0 AND (contacts.first_name LIKE 'James%' OR contacts.last_name LIKE 'Will not be found%' ) -- ORDER BY -- jt9_email_addresses_primary.email_address DESC, -- contacts.id DESC LIMIT 0,31