你可以帮助调整查询

时间:2013-12-23 08:56:17

标签: mysql optimization

对于25000条记录,此查询大约需要45秒到1分钟。我需要一个替代案例 expirydate字段的条件

SELECT a.id,
       a.name,
       a.lastname,
       a.plainpass,
       a.email,
       a.custom_usertype,
       a.custom_registertype,
       a.content_state,
       a.year,
       a.bflag,
       a.block,
       a.trail_expiry,
       a.subscribe_date,
       CASE a.custom_usertype
          WHEN (5)
          THEN
             (IF(
                     (SELECT custom_usertype
                        FROM skw_users
                       WHERE id = a.parent_id AND custom_usertype = 11) = 11
                 AND a.custom_registertype = "subscribed",
                 (SELECT DATE_FORMAT(prod.expiry_date, "%Y-%m-%d")
                    FROM skw_product prod
                   WHERE prod.student_id = a.id
                  ORDER BY prod.expiry_date DESC
                   LIMIT 1),
                 IF(
                        (SELECT custom_usertype
                           FROM skw_users
                          WHERE id = a.parent_id AND custom_usertype = 10) =
                           10
                    AND a.custom_registertype = "subscribed",
                    (SELECT DATE_FORMAT(prod.expiry_date, "%Y-%m-%d")
                       FROM skw_product prod
                      WHERE prod.User_id = a.parent_id
                     ORDER BY prod.expiry_date DESC
                      LIMIT 1),
                    (SELECT DATE_FORMAT(trail_expiry, "%Y-%m-%d")
                       FROM skw_users
                      WHERE id = a.parent_id))))
          WHEN (3)
          THEN
             (IF(a.custom_registertype = "subscribed",
                 (SELECT DATE_FORMAT(prod.expiry_date, "%Y-%m-%d")
                    FROM skw_product prod
                   WHERE prod.User_id = a.parent_id
                  ORDER BY prod.expiry_date DESC
                   LIMIT 1),
                 (SELECT DATE_FORMAT(trail_expiry, "%Y-%m-%d")
                    FROM skw_users
                   WHERE id = a.parent_id)))
          ELSE
             (IF(a.custom_registertype = "subscribed",
                 (SELECT DATE_FORMAT(prod.expiry_date, "%Y-%m-%d")
                    FROM skw_product prod
                   WHERE prod.User_id = a.id
                  ORDER BY prod.expiry_date DESC
                   LIMIT 1),
                 (SELECT DATE_FORMAT(trail_expiry, "%Y-%m-%d")
                    FROM skw_users
                   WHERE id = a.id)))
       END
          AS expiry_date_n,
       CASE a.custom_usertype
          WHEN (5)
          THEN
             (SELECT TRIM(NAME)
                FROM skw_users
               WHERE id = a.parent_id AND custom_usertype = 10)
          WHEN (3)
          THEN
             (SELECT TRIM(NAME)
                FROM skw_users
               WHERE id = a.parent_id)
          WHEN (10)
          THEN
             (SELECT TRIM(NAME)
                FROM skw_users
               WHERE id = a.id AND a.custom_usertype = 10)
       END
          AS schoolname
  FROM    `skw_users` AS a
       INNER JOIN
          skw_user_usergroup_map AS map2
       ON map2.user_id = a.id
 WHERE map2.group_id = 2
GROUP BY a.id
ORDER BY trim(a.name) ASC

对于25000条记录,此查询大约需要45秒到1分钟。我需要一个替代案例 expirydate字段的条件

1 个答案:

答案 0 :(得分:0)

尝试添加Primary Keya.id上的索引以及您应用a.custom_usertype

案例的列

希望这会加快查询的执行速度:)