我正在使用 yii2 数据提供程序从数据库中提取数据。原始查询如下所示
SELECT `client_money_operation`.* FROM `client_money_operation`
LEFT JOIN `user` ON `client_money_operation`.`user_id` = `user`.`id`
LEFT JOIN `client` ON `client_money_operation`.`client_id` = `client`.`id`
LEFT JOIN `client_bonus_operation` ON `client_money_operation`.`id` = `client_bonus_operation`.`money_operation_id`
WHERE (`client_money_operation`.`status`=0) AND (`client_money_operation`.`created_at` BETWEEN 1 AND 1539723600)
GROUP BY `operation_code` ORDER BY `created_at` DESC LIMIT 10
执行此查询需要 107 秒。
表 client_money Operations
包含 132000 行。我需要做什么来优化这个查询,或者正确设置我的数据库?
最佳答案
尝试分页。但是,如果您必须一次性显示大量记录,请删除尽可能多的左连接。如果确实需要在一次性结果集中显示,您可以在 client_money_operation 表中复制一些数据。
关于php - yii2数据提供者查询需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52831636/