这是我在 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(*)
添加。
关于php - 如何在 Zend 中优化我的查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5387278/