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 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)