使查询更有效(查询速度非常慢!)

时间:2013-07-31 14:13:52

标签: php mysql sql subquery

我要做的是让所有用户都拥有合适的条件 所以我正在使用foreach statment个子查询来构建它。

问题是我在数据库中获得了100,000多条记录。 而且这种查询需要永远运行。

我知道我没有以最好的方式做到这一点,但我也试过了左连接,这也很慢..

这是我正在使用的功能:

    public function get_affected_users_by_conditions($conditions, $mobile_type)
    {
        // Basic Query 
        // Selecting all of the users from `enswitch_mobile users` table
        // The total and the `users` with the conditions
        $sql = "SELECT COUNT(*) AS `users`,
                       (SELECT COUNT(*) FROM `enswitch_mobile_users`) AS `total`
                FROM 
                    `enswitch_mobile_users` AS `musers`
                WHERE 
                    `musers`.`phone_type` = :mobile_type";

        $value_counter = 0;
        $values = array();
        // This is the foreach loop I was talking about
        // I am looping all the conditons.
        // and when theres a value i'm adding it as a subquery.
        foreach($conditions as $cnd) {
            switch ($cnd['condition']) {

                // REALLY SLOW SUB-QUERY:
                case 'talked_atleast':
                    $value_counter++;
                    // Here I'm trying to CUT the query by users who talked atleast $value seconds
                    $sql .= " AND (SELECT SUM(TIME_TO_SEC(TIMEDIFF(`finished_call`,`start_call`))) FROM `enswitch_calls` WHERE `user_id` = `musers`.`id`) >= :value".$value_counter;
                    $values[$value_counter] = $cnd['value'];
                    break;

// REALLY SLOW SUB-QUERY:
                case 'purchase_atleast':
                    // Here I am trying to CUT the users by subquery who check if the users has bought at least $value times
                    $value_counter++;
                    $sql .= " AND (SELECT COUNT(*) FROM (SELECT user_id FROM enswitch_new_iphone_purchases
                                                    UNION
                                                  SELECT user_id FROM enswitch_new_android_purchases) AS p WHERE `status` > 0 AND user_id` = `musers`.`id`) >= :value".$value_counter;
                    $values[$value_counter] = $cnd['value'];
                    break;
// REALLY SLOW SUB-QUERY:
                case 'never_purchase':
                    // Here I am trying to CUT the users by subquery to get only the users who never made a puchase.
                    $sql .= ' AND (SELECT COUNT(*) FROM (SELECT user_id FROM enswitch_new_iphone_purchases
                                                    UNION
                                                  SELECT user_id FROM enswitch_new_android_purchases) AS p WHERE `status` = 0 AND `user_id` = `musers`.`id`) = 0';
                    break;
            }
        }


        $query = DB::query(Database::SELECT, $sql);
        $query->bind(':mobile_type', $mobile_type);
        // Looping the values and binding it into the SQL query!
        foreach ($values as $k => $v) {
            $query->bind(':value'.$k, $values[$k]);
        }
        // Executing query
        $result = $query->execute();
        return array('total_users' =>$result[0]['total'], 'affected_users'=>$result[0]['users']);
    }

编辑: 请求的最慢查询:(MySQL)

SELECT COUNT(*) AS `users`,
       ( SELECT COUNT(*) 
           FROM `enswitch_mobile_users`
       ) AS `total`
  FROM `enswitch_mobile_users` AS `musers`
 WHERE `musers`.`phone_type` = 'iphone'
   AND ( SELECT COUNT(*) 
           FROM ( SELECT `status`,
                         `user_id` 
                    FROM `enswitch_new_iphone_purchases`
                  UNION
                  SELECT `status`,
                         `user_id` 
                    FROM `enswitch_new_android_purchases`
                ) AS `p` 
          WHERE `p`.`status` > 0 
            AND `p`.`user_id` = `musers`.`id`
       ) >= 2

1 个答案:

答案 0 :(得分:1)

第二个SELECT列中的子查询将针对通过m_users条件的每个WHERE行执行:

SELECT
  COUNT(*) AS users,
  (SELECT COUNT(*) FROM enswitch_mobile_users) AS total <-- here's the problem
FROM enswitch_mobile_users AS musers
WHERE musers.phone_type = whatever

如果我正确读取此内容,则需要包含以下列的单行结果:

  • users - 具有指定enswitch_mobile_users
  • phone_type行数
  • total - 所有enswitch_mobile_users行的数量

您可以使用此查询获得相同的结果:

SELECT
  COUNT(CASE WHEN musers.phone_type = whatever THEN 1 END) AS users,
  COUNT(*) AS total
FROM enswitch_mobile_users

CASE检查手机类型,如果匹配您感兴趣的手机类型,则会产生1,这会被计算在内。如果不匹配,则会产生NULL,不计算在内。