Yii Framework Forum: Widget/DB Optimization Design - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Widget/DB Optimization Design Rate Topic: -----

#1 User is online   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 489
  • Joined: 09-April 10

Posted 04 June 2012 - 10:39 AM

Hi,

I'm trying to find a design that allows me to make a single DB query for a list of widgets - to increase performance. These widgets might be changing rapidly so I don't want to use caching. The main goal is to delegate DB queries to a marshal object. The issue is delaying the marshal/DB queries until all widget instances have been loaded, so I know which IDs to query.

Originally, each widget was calling 3 queries to find counts of Votes - upVoteCount, downVoteCount, and totalVoteCount. 5 voting stations/page @ 3 queries each = 15 DB queries!

I've refactored it to make a single DB call per widget and calculate the vote counts server side. 5 voting stations/page @ 1 query = 5 DB queries. I still think that's too high.

Attached File  db_queries.jpg (167.03K)
Number of downloads: 11

My first idea is to
  • disable the widget instances as they're called
  • add them to a list from their init method
  • perform a single query based on their properties
  • create and populate an object (Information Marshal?) with values
  • re-enable and run the widgets - they can then access the retrieved data

Can anybody shed some light on how to disable/enable the widgets or a similar technique?

Thanks,

Matt
0

#2 User is offline   Da:Sourcerer 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 782
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 04 June 2012 - 10:45 AM

Hm, how did you go about "redesigning" those widgets? Have you used eager loading?
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#3 User is online   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 489
  • Joined: 09-April 10

Posted 04 June 2012 - 10:59 AM

View PostDa:Sourcerer, on 04 June 2012 - 10:45 AM, said:

Hm, how did you go about "redesigning" those widgets? Have you used eager loading?

No - just refactored it like so


public function loadData()
{
    $votes = Vote::model()->findAll('model=:modelName && model_id=:modelId', array(
        ':modelName' => $this->candidateName,
        ':modelId' => $this->candidateId));

    $this->upVoteCount = 0;
    $this->downVoteCount = 0;
    $this->totalVoteCount = 0;

    foreach ($votes as $vote)
    {
        switch ($vote->direction)
        {
            case Vote::DIRECTION_UP:
                $this->upVoteCount++;
                break;
            case Vote::DIRECTION_DOWN:
                $this->downVoteCount++;
                break;
            default:
                $this->totalVoteCount++;
                break;
        }
    }
}

0

#4 User is offline   Da:Sourcerer 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 782
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 04 June 2012 - 11:00 AM

:blink:
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#5 User is online   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 489
  • Joined: 09-April 10

Posted 04 June 2012 - 11:03 AM

Posted Image
0

#6 User is offline   Da:Sourcerer 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 782
  • Joined: 30-March 11
  • Location:Berlin, Germany

Posted 04 June 2012 - 11:05 AM

Let me put it like this: You're optimizing at the wrong end. Search the Yii guide for the following terms: Statistical queries and eager loading. And I'm pretty sure you'll get a fine CDbCacheDependency out of the total number of votes.
programmer /ˈprəʊgramə/, noun: a device that converts ►coffee into ►code
0

#7 User is online   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 489
  • Joined: 09-April 10

Posted 04 June 2012 - 11:15 AM

Thanks, I'll take a look at CDBCacheDependency and post back some results.

Matt
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users