Coder Social home page Coder Social logo

Comments (5)

rkang30 avatar rkang30 commented on August 27, 2024

I have played around with current api system and I was able to reduce it to a little less than 1 minute. This time I only pull first 25 csrs. The time that burns the most is at looping stage for fetching 'plannedHours'. I know that I can get the collection of keys in array. Is there any way that I can pluck the one column value (for my case 'plannedHours') after find()? What I looking for is to fetch only 'plannedHours' values in array and sum those values. This would reduce significant time.

Thanks,
Ryan

	$results = [];
	$count = 0;
	foreach($csrs as $csr_id => $csr_name){

		$results[$count]['csr_id'] = $csr_id;
		$results[$count]['csr_name'] = $csr_name;

		$activeJobIds = $this->paceStartSql()
		->filter('@csr', (int) $csr_id)
		->sort('@job', true)
		->find()
        ->filterKeys(function($key){
            return preg_match('/^[0-9]{6,}$/', $key);
        })->keys();
		
		$active_dockets = count($activeJobIds);
		$results[$count]['active_dockets'] = $active_dockets;

        $parts = 0;
        $hours = 0;

        if(count($activeJobIds) > 0){
            $jobPlans = $pace->jobPlan
            ->filter(function($xpath) use ($activeJobIds){
                $c = 0;
                foreach($activeJobIds as $activeJobId){
                    if($c == 0){
                        $xpath->filter('@job', $activeJobId);
                      $c = 1;  
                    }else{
                        $xpath->orFilter('@job', $activeJobId);
                    }
                }
            })
            ->filter('@activityCode', $this->prePressJobPlanCode)
            ->filter('@plannedHours', '!=', 0)
            ->find();

            foreach($jobPlans as $jobPlan){
                $hours += $jobPlan->plannedHours;
            }
        
            $jobParts = $pace->jobPart
            ->filter(function($xpath) use ($activeJobIds){
                $c = 0;
                foreach($activeJobIds as $activeJobId){
                    if($c == 0){
                        $xpath->filter('@job', $activeJobId);
                      $c = 1;  
                    }else{
                        $xpath->orFilter('@job', $activeJobId);
                    }
                }
            })
            ->find()
            ->count();

            $parts += $jobParts;
        }

		$results[$count]['total_parts'] = $parts;    		
		$results[$count]['booked_hours'] = $hours;  

	  $count++;	
	}

from pace-api.

radoslavius avatar radoslavius commented on August 27, 2024

We usually do queries on data over the database, which is much much faster. Especially for inquiries through more than one object.

@robgridley wrote: "I hate the Pace database. Who ever named those columns should be flogged in the street. At least come up with a consistent naming scheme for relationship keys!" - it is big TRUE

But we are using phpstorm that make things much easier:
https://blog.jetbrains.com/phpstorm/2014/11/database-language-injection-configuration/

from pace-api.

rkang30 avatar rkang30 commented on August 27, 2024

Hi there,

I found 'pluck()' in KeyCollection class. But it didn't cut any time on fetching 'plannedHours'... I guess I should close this ticket for now..

from pace-api.

robgridley avatar robgridley commented on August 27, 2024

It does not really matter what you do in PHP, because your PHP code is not the bottleneck. It is the repeated API read object calls which are slow. It is known as an "N+1 problem". Unfortunately, the API does not allow you to read multiple objects at once or fetch only the properties you are interested in.

I agree with @radoslavius. This is a situation where querying Postgres directly—at least for summing the planned hours—would perform a lot better.

from pace-api.

rkang30 avatar rkang30 commented on August 27, 2024

Thanks for your answer, robgridley.

Could it be possible for you to add additional API feature that allows users to run raw psgresql queries?

e.g. $job = $pace->select('SELECT * FROM jobs WHERE job = ? ', [123]);

from pace-api.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.