Php 使查询更高效(真正缓慢的查询永远耗时!)
我想做的是让所有的用户都具备合适的条件 因此,我正在构建一个Php 使查询更高效(真正缓慢的查询永远耗时!),php,mysql,sql,subquery,Php,Mysql,Sql,Subquery,我想做的是让所有的用户都具备合适的条件 因此,我正在构建一个foreach statmentsub_查询,以使其工作 问题是我在数据库中得到了100000多条记录。 而这种查询需要永远运行 我知道我做的不是最好的方式,但我也尝试了左连接,这也是非常缓慢的 这是我正在使用的函数: public function get_affected_users_by_conditions($conditions, $mobile_type) { // Basic Query
foreach statment
sub_查询,以使其工作
问题是我在数据库中得到了100000多条记录。
而这种查询需要永远运行
我知道我做的不是最好的方式,但我也尝试了左连接,这也是非常缓慢的
这是我正在使用的函数:
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
列中的子查询将对通过其中
条件的每个MU用户
行执行:
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
CASE
检查电话类型,如果它与您感兴趣的电话类型匹配,则会生成一个1
,并将其计算在内。如果不匹配,它将生成一个NULL
,该值不被计算。因为这不是一个真正的php问题,如果您只发布生成的sql语句,它可能会更有用,更容易阅读。显示生成的查询和数据库架构。并显示问题查询的解释,以查看索引(如果有)它实际上是在使用。每行运行一个子查询基本上是让事情变得疯狂缓慢的最佳方式。
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
SELECT
COUNT(CASE WHEN musers.phone_type = whatever THEN 1 END) AS users,
COUNT(*) AS total
FROM enswitch_mobile_users