Question

I have the following query which is optimized, yet leaves the servers cpu usage at 80% every time it is run. Explain shows that all tables are using correct indexes and minimum number of rows are being scanned. So I have no idea what could be causing this query to kill the server. Would appreciate any help.

SELECT
    DISTINCT
    c.id,
    c.sName AS sCompany,
    IF(
      u.isActive = 1
      AND c.bActive = 1,
      'yes',
      'no'
    ) AS bActive,
    COUNT(t1.id) AS nDownloads,
    (
      -- samples received
      SELECT
        COUNT(id)
      FROM tblSample
      WHERE nUserID = u.id
    ) AS nRequests,
    COUNT(t3.id) AS nClicks,
    COUNT(t2.id) AS nImpressions,
    COUNT(li.id) AS nActiveConnections
FROM tblUser u
LEFT JOIN tblCompany c
    ON c.id = u.nCompanyID
LEFT JOIN tblLink li
    ON (
        li.nToObjectID = c.id
        AND li.nFromObjectID != 0
        AND li.sType = 'company'
        AND li.sStatus = 'active'
    )
-- DOWNLOADS
LEFT JOIN tblTracker t1
    ON (
        t1.nUserID = u.id
        AND t1.sClass = 'Media'
        AND t1.sMethod IN ('download', 'image')
        AND t1.sAction = 'download'
    )
LEFT JOIN tblMedia m1
    ON m1.id = t1.nID
-- IMPRESSIONS
LEFT JOIN tblTracker t2
    ON (
        t2.nUserID = u.id
        AND t2.sClass = 'Media'
        AND t2.sMethod = 'imageThumbnail'
        AND t2.sAction = 'impression'
    )
LEFT JOIN tblMedia m2
    ON m2.id = t2.nID
-- PRODUCTS VIEWS
LEFT JOIN tblTracker t3
    ON (
        t3.nUserID = u.id
        AND t3.sClass = 'Product'
        AND t3.sMethod = 'view'
        AND t3.sAction = 'view'
    )
LEFT JOIN tblMedia m3
    ON m3.id = t3.nID
WHERE c.sType = 'media'
GROUP BY c.sName;

Img:

Explain plan

No correct solution

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