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

标签 php mysql zend-framework query-optimization

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

相关文章:

PHP 数据 session 存储

存在编码问题的 PHP/MySQL

php - 暂时禁用 Laravel 追加

javascript - 单击提交按钮并将数据插入 php 中的数据库表时如何显示隐藏表?

php - 如何注册命名空间的自定义 View 助手?

php - Zend_Test_PHPUnit_ControllerTestCase : Test view parameters and not rendered output

php - Zend 查询大于或等于

php - 多维 PHP 数组到 JSON 数组

php - 使用 codeigniter php 插入数据后发送电子邮件

PHP SESSION 变量未从 MySQL 记录插入