doucan1996 2011-03-22 05:37
浏览 19
已采纳

如何在Zend中优化我的查询?

This is my simple query in 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 ) );

In above zend query I am get getting patient records and their insurances based on some conditions in where clause. I have to get (1) Total No. of Records, (2) Total No. of filtered Records and also (3) Patient Records to show on webpage.

Problem is that in my above query I have to fetch records 3 times which slow the performance when there are 10,000 records. How can I optimize my query that it fetch the the records only once OR there should be a separate query for counting that will only get total No of records instead of fetching all records.

Every reply will be appreciated.

Thanks Thanks

  • 写回答

1条回答 默认 最新

  • doulanyan6455 2011-03-22 07:03
    关注

    Something like this should get you started, unfortunately I don't have a way of testing this currently.

    // 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);
    

    Note: You may be able to re-use the same Zend_Db_Select object by overwriting the $select->from() to remove the COUNT(*) addition.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看