0

我想要做的是让所有用户都具备正确的条件,所以我正在构建foreach statment一个 sub_queries 以使其工作。

问题是我在数据库中有 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
4

1 回答 1

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
  • totalenswitch_mobile_users- 所有行的计数

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

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

CASE检查电话类型,如果它与您感兴趣的匹配,它会产生 a ,1它会被计算在内。如果不匹配,则生成 a NULL,不计算在内。

于 2013-07-31T14:23:20.513 回答