gpt4 book ai didi

php - Mysql - 每个事件行的位置(高延迟)

转载 作者:行者123 更新时间:2023-11-29 13:04:55 24 4
gpt4 key购买 nike

我有一个应用程序来控制预订队列,我需要获取每一行的位置。我已经编码了,但是返回数据的延迟非常高(大量查询)。

linePositionTotal - Represents the position based on all active(status) rows.
linePositionSamePeople - Represents the position based on all active(status) rows with the same people number.
<小时/>

表:

:: Reservation
id Int AI
status Tinyint (0-Inactive/1-Active)
people Int
created Datetime

Reservation.php(模型):

class Reservation extends CActiveRecord
{
const STATUS_ACTIVE = 1;

public $linePositionTotal;
public $linePositionSamePeople;

...

public function afterFind() {
$criteria = new CDbCriteria;
$criteria->condition = 'created<:created AND status=:status';
$criteria->params=array(':created'=>$this->created, ':status'=>Reservation::STATUS_ACTIVE);
$countReservations = Reservation::model()->count($criteria);
$this->linePositionTotal=$countReservations;

$criteria = new CDbCriteria;
$criteria->condition = 'people=:people AND created<:created AND status=:status';
$criteria->params=array(':people'=>$this->people, ':created'=>$this->created, ':status'=>Reservation::STATUS_ACTIVE);
$countReservations = Reservation::model()->count($criteria);
$this->linePositionSamePeople=$countReservations;
}

代码:

$model = Reservation::model()->findAll();
// Problem: If have more than 15 active rows it's taking longer than 10 seconds.
<小时/>

有人知道如何优化它或更好的方法吗?

最佳答案

您正在为找到的每一行运行两个单独的查询。应使用适当的联接将其放入单个 SQL 查询中。您正在查询的数据的架构会很有帮助,但答案将至少涉及一个连接和计数。

关于php - Mysql - 每个事件行的位置(高延迟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22852018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com