In case you have your own slow SQL query, you can optimize it automatically here.
For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
ALTER TABLE `Ac2018` ADD INDEX `ac2018_idx_accesstype_utcaccesseddt` (`AccessType`,`UTCAccessedDT`);
ALTER TABLE `Ac2019` ADD INDEX `ac2019_idx_accesstype_utcaccesseddt` (`AccessType`,`UTCAccessedDT`);
ALTER TABLE `Ac2019` ADD INDEX `ac2019_idx_lbid` (`lbid`);
ALTER TABLE `Ass` ADD INDEX `ass_idx_associd` (`AssocID`);
ALTER TABLE `LBNames` ADD INDEX `lbnames_idx_topassembly` (`TopAssembly`);
ALTER TABLE `LockBox` ADD INDEX `lockbox_idx_lbid` (`LBID`);
SELECT
ass.Name,
lbnm.ProductName,
COUNT(*) AS lb_count
FROM
(SELECT
ac.lbid
FROM
lockbox.Ac2018 ac
WHERE
ac.AccessType NOT IN (
'Gen'
)
AND ac.UTCAccessedDT > '2018-10-10 00:00:00'
UNION
SELECT
ac.lbid
FROM
lockbox.Ac2019 ac
WHERE
ac.AccessType NOT IN (
'Gen'
)
AND ac.UTCAccessedDT > '2019-10-01 00:00:00'
GROUP BY
ac.lbid
) p
JOIN
lockbox.LBMFG mfg
ON p.LBID = mfg.LBID
JOIN
lockbox.LockBox lb
ON mfg.LBID = lb.LBID
JOIN
lockbox.Ass ass
ON ass.AssocID = lb.AssocID
JOIN
lockbox.LBNames lbnm
ON lbnm.TopAssembly = mfg.TopAssembly
GROUP BY
ass.Name,
lbnm.ProductName
ORDER BY
NULL