我有七个表,当我执行以下 sql 查询时,处理查询需要很长时间,并且我的系统在执行此操作时会挂起。
SELECT DISTINCT
M.MerchantId,
M.MerchantName,
M.Image
FROM
CCodes as CC,
Merchants as M,
MemberStores as MS,
MemberCategories as MC,
Categories as C,
MembersLogin as ML,
CategoryAndMerchant as CM
WHERE
(
(
M.MerchantId = MS.MerchantId
AND MS.MemberId = ML.Id
)
OR
(
ML.Id = MC.MemberId
AND MC.CategoryId = C.Id
AND C.Id = CM.CategoryId
AND CM.MerchantId = M.MerchantId
AND CC.MerchantId = M.MerchantId
)
)
AND CC.Status = 1
AND M.Status = 1
AND C.Status = 1
AND CC.Status = 1
AND EndDate BETWEEN '$todaysDate' AND '$endDate'
AND ML.Id = $id
ORDER BY EndDate asc
您可以假设 $todaysDate、$endDate 和 $id 的任何值。
请帮我优化查询,使执行时间尽可能少。