我正在尝试从一个大小为9gb+且有数百万条记录的表中获取数据。我正在用这些数据填充datatable。我通过ajax和sqllimit查询从表中获得了大量的记录,即每页10条记录。
分页
在上图中你可以看到我们有 223,740
因此,当我尝试访问最后一页时,查询将永远无法加载数据。但是,当我尝试访问第一页时,数据加载速度更快。但是直接访问更高偏移量的页面需要很长时间才能加载。
public static function getAllEvaluationsWithNameForDataTable($start){
$queryBuilder = new Builder();
return $queryBuilder
->from(array('e' => static::class))
->leftJoin('Cx\Framework\Models\Common\User\CxUser', 'e.cx_hc_user_id = u.id', 'u')
->columns('e.id, e.first_name, u.initials as assigned_coach, e.gender, e.email, e.phone, e.age, e.version, e.evaluation_status, e.ip_address, e.date_created, e.date_updated')
->orderBy('e.id asc')
->limit(10, $start)
->getQuery()
->execute()
->toArray();
}
php函数/控制器:
public function getEvaluationsAction() {
// Enable Json response
$this->setJsonResponse();
// This action can be called only via ajax
$this->requireAjax();
// Forward to access denied if current user is not allowed to view evaluation details
if (!$this->CxAuth->currentUserIsAllowedTo('VIEW', CxEbEvaluation::getClassResourceName()))
return $this->forwardToAccessDeniedError();
if(isset($_GET['start'])){
$start = $this->request->get('start');
}else{
$start = 10;
}
$recordsTotal = count(CxEbEvaluation::getAllForDataTable(array('id')));
//Get Evaluations from DB
$evaluation_quizzes = CxEbEvaluation::getAllEvaluationsWithNameForDataTable(intval($start));
//for getting base URL
$url = new Url();
$data = array();
foreach ($evaluation_quizzes as $key => $quiz) {
$data[ $key ][ 'id' ] = $quiz[ 'id' ];
$data[ $key ][ 'first_name' ] = $quiz[ 'first_name' ];
if($quiz[ 'assigned_coach' ]){
$data[ $key ][ 'assigned_coach' ] = $quiz['assigned_coach'];
}else{
$data[ $key ][ 'assigned_coach' ] = "Not assigned";
}
$data[ $key ][ 'gender' ] = $quiz[ 'gender' ];
$data[ $key ][ 'email' ] = $quiz[ 'email' ];
$data[ $key ][ 'phone' ] = $quiz[ 'phone' ];
$data[ $key ][ 'age' ] = $quiz[ 'age' ];
$data[ $key ][ 'version' ] = $quiz[ 'version' ];
$data[ $key ][ 'quiz' ] = $url->get('/admin/get-evaluation-quiz-by-id');
$data[ $key ][ 'manage-notes-messages-and-calls' ] = $url->get('/admin/manage-notes-messages-and-calls');
$data[ $key ][ 'date_created' ] = date("m/d/Y H:i:s", $quiz[ 'date_created' ]);
$data[ $key ][ 'evaluation_status' ] = $quiz[ 'evaluation_status' ];
}
// Return data array
return array(
"recordsTotal" => $recordsTotal,
"recordsFiltered" => $recordsTotal ,
"data" => $data //How To Retrieve This Data
);
// Return data
}
javascript代码:
cx.common.data.cxAdminDataTables.EbEvaluation = $CxRecordsTable.cxAdminDataTable({
ajaxUrl: '<?php echo $this->CxHelper->Route('eb-admin-get-evaluations')?>' + eqQuizIdQueryString,
serverSide: true,
processing: true,
recordsFiltered :true,
columns: [
cx.common.admin.tableEditColumn('id',{ delete: true }),
{ data: 'first_name' },
{ data: 'assigned_coach' },
{ data: 'gender' },
{ data: 'email' },
{ data: 'phone' },
{ data: 'age' },
cx.common.admin.tableLinkColumn('quiz', quizLinkOptions),
cx.common.admin.tableEditColumn('id', healthCoachLinkOptions),
cx.common.admin.tableLinkColumn('manage-notes-messages-and-calls', manageNotesMessagesAndCalls),
{ data: 'date_created' },
cx.common.admin.tableSwitchableColumn('evaluation_status', {
editable: true,
createdCell: function (td, cellData, rowData, row, col){
$(td).data('evaluation-status-id', rowData.id);
},
onText: 'Complete',
offText: 'In progress'
})
],
toolbarOptions:{
enabled: false
}, success: function (data) {
cx.common.data.cxAdminDataTables.EbEvaluation.cxAdminDataTable("reloadAjax");
}
});
}
else {
$row.removeClass('alert');
}
});
}
});
我希望问题很清楚。如果有任何其他需要,只要更新我,我会提供。
(来自评论)
SELECT e.id` AS id, e.first_name AS first_name,
u.initials AS assigned_coach,
e.gender AS gender, e.email AS email, e.phone AS phone,
e.age AS age, e.version AS version,
e.evaluation_status AS evaluation_status,
e.ip_address AS ip_address, e.date_created AS date_created,
e.date_updated AS date_updated
FROM evaluation_client AS e
LEFT JOIN cx_user AS u ON e.cx_hc_user_id = u.id
ORDER BY e.id ASC
LIMIT :APL0 OFFSET, :APL1
3条答案
按热度按时间idfiyjo81#
模式
SELECT whatever FROM vast_table ORDER BY something LIMIT 10 large_number
是一个臭名昭著的性能反模式。为什么?因为它必须检查很多行才能返回一些。如果你的
id
值是可以分页的主键(或任何索引列)或者你可以试试
如果您的
id
价值观之间有差距。但它的表现还算不错。clj7thdc2#
为什么mysql上限偏移会减慢查询速度?问题和答案,由masivuye cokile链接,以及https://explainextended.com/2009/10/23/mysql-order-by-limit-performance-late-row-lookups/ 这里提供的链接包含了一个极好的关于为什么大偏移量查询速度慢的详细说明。基本上,为了
LIMIT 150000, 10
mysql仍然会扫描整个150000行,即使以后会丢弃它们。要加快速度,您可以:使用顺序分页,即“在id#n之后显示10个条目”,这工作速度非常快,是一个很好的替代方法,但会丢弃实际页码;您的用户将留下“下一页/上一页”链接和/或您可以使用
count
查询。或在上创建索引
id
,然后强制mysql执行仅索引搜索。对于第二种方法,您必须重写
到
或者,由于不局限于单个查询,因此可以使用
然后使用所述id检索实际数据:
wn9m85ua3#
问题与我的表中的日期列数据类型有关。我用的是
int
日期字段的数据类型,以及何时将日期列的数据类型更改为datetime
,搜索结果以秒为单位。我找到解决方案的来源@http://dbscience.blogspot.com/2008/08/can-timestamp-be-slower-than-datetime.html