加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2021-07-06 04:17:26 所属栏目:大数据 来源: https://www.jb51.cc
导读:这是我在mysql / zend中的简单查询: // Get Patients$table = new Model_Patient_DbTable();$select = $table-select();$select-from( 'patient' );$select-setIntegrityCheck( false );// insurance join $select-joinLeft( 'insurance', 'patient.insuran

这是我在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 ) );

在上面的zend查询中,我会根据where子句中的某些条件获得患者记录及其保险.我必须得到(1)记录总数,(2)过滤记录总数以及(3)在网页上显示的患者记录.

问题是,在上面的查询中,我必须获取记录3次,这会在有10,000条记录时降低性能.如何优化我的查询,它只获取记录一次或者应该有一个单独的计数查询,只会获得记录总数而不是获取所有记录.

每个回复都将不胜感激.

谢谢
谢谢

解决方法:

这样的事情应该让你开始,不幸的是我目前没有办法测试这个.

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

注意:您可以通过覆盖$select-> from()来重新使用相同的Zend_Db_Select对象,以删除COUNT(*)加法.

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读