2011-03-22 3 views
4

이 mysql을/젠드 내 간단한 쿼리입니다 :Zend에서 내 쿼리를 최적화하는 방법은 무엇입니까?

// Get Patients 
$table = new Model_Patient_DbTable(); 
$select = $table->select(); 
$select->from('patient'); 
$select->setIntegrityCheck(false); 

// insurance join                
$select->joinLeft('insurance', 'patient.insuranceId=insurance.Id', 
           array('insName' => 'insName')); 

// Get total no of records 
$totalRecords = count($table->fetchAll($select)); 

// Filters 
if($inactive) { 
    $select->where('patient.inactive = ?', $inactive); 
} 
// Other where clause conditions 

// Fetch filtered patient records   
$patientRecords = $table->fetchAll($select); 

// Get total no of filtered patient records 
$filteredRecords = count($table->fetchAll($select)); 

내가 WHERE 절에 일부 조건에 따라 점점 환자 기록과 보험을 얻을입니다 위 젠드 쿼리합니다. 나는 (1) 총 레코드 수, (2) 총계 필터링 된 레코드 수 및 (3) 웹 페이지에 표시 할 환자 기록.

문제는 위의 쿼리에서 10,000 레코드가있을 때 성능이 저하되는 레코드를 3 회 가져와야한다는 것입니다. 쿼리를 최적화하여 레코드를 한 번만 가져 오거나 모든 레코드를 가져 오는 대신 레코드의 총 수만 가져 오는 계산을위한 별도의 쿼리가 있어야합니다.

모든 답변을 부탁드립니다. 이 같은

+0

먼저 PHP 카운트를해서는 안됩니다. MySQL의 count 메소드를 사용하여 기준에 맞는 레코드 수를 파악하십시오. – JohnP

+0

@JohnP : 네가 쓰고 있습니다.하지만 젠드에서 어떻게하는지 설명 할 수 있습니까? 감사. – Awan

답변

3

뭔가 얻어야한다

감사 덕분에 당신은 불행하게도 나는 현재이 테스트 방법이 없어 시작했다.

// Get Patients 
$table = new Model_Patient_DbTable(); 

// Get Total records 
$select = $table->select(); 
$select->from($table, array('COUNT(*) as row_count')); 
$select->setIntegrityCheck(false); 
$select->joinLeft('insurance', 'patient.insuranceId = insurance.Id', array('insName' => 'insName')); 
$result = $table->fetchAll($select); 
$totalRecords = $result[0]->row_count; 

// Filters 
if ($inactive) { 
    $select->where('patient.inactive = ?', $inactive); 
} 

// Get Total filtered records 
$result = $table->fetchAll($select); 
$filteredRecords = $result[0]->row_count; 

// Get filtered records 
$select = $table->select(); 
$select->from($table); 
$select->setIntegrityCheck(false); 
$select->joinLeft('insurance', 'patient.insuranceId = insurance.Id', array('insName' => 'insName')); 
if ($inactive) { 
    $select->where('patient.inactive = ?', $inactive); 
} 
$patientRecords = $table->fetchAll($select); 

참고 : 당신은 COUNT(*) 추가 제거 $select->from()을 덮어 쓰기하여 다시 사용하는 것과 동일한 Zend_Db_Select 개체 수 있습니다.

+0

+1 감사합니다. 나는 그것을 시험 할 것이다. – Awan

+0

더 최적화 할 수 있습니까? 여기에 반복적 인 코드가 있습니다. 예를 들어 나는 많은 양의 필터를 가지고있다. – Awan

관련 문제