dqs86517 2014-04-04 01:40
浏览 7

Mysql - 每个活动行的位置(高延迟)

I have an application to control reservations queue, and I need to get each row position. I already coded, but is getting a very high latency to return the data (heavy queries).

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.

Table:

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

Reservation.php (Model):

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;
}

Code:

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

Anyone have an idea how to optimize it or a better way to do that?

  • 写回答

1条回答 默认 最新

  • douwu8524 2014-04-04 02:02
    关注

    You're running two separate queries for each row you find. This should be put into a single sql query using appropriate join or joins. The schema for the data you are querying would be helpful, but it the answer will involve at least one join and counts.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程