gpt4 book ai didi

php - 如何优化限制查询以更快地从一个巨大的表中访问数据?

转载 作者:可可西里 更新时间:2023-11-01 07:50:52 24 4
gpt4 key购买 nike

我正在尝试从一个大小为 9 GB 且拥有数百万条记录的表中获取数据。我正在用该数据填充 DataTable。我正在从表中获取记录,即通过 Ajax 和 SQL Limit 查询每页 10 个记录。

pagination

在上图中,您可以看到我们有 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 函数/ Controller :

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

最佳答案

Why does MYSQL higher LIMIT offset slow the query down?问题和答案,由 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 执行仅索引搜索。

对于第二种方法,您必须重写来自

的查询
SELECT ... 
FROM table t
WHERE ...
ORDER by t.id ASC
LIMIT 150000, 10

SELECT  ...
FROM (
SELECT id
FROM table
ORDER BY
id ASC
LIMIT 150000, 10
) o
JOIN table t
ON t.id = o.id
WHERE ...
ORDER BY t.id ASC

或者,由于您不局限于单个查询,您可以使用

检索页面上第一个项目的 ID
SELECT id 
FROM table
ORDER BY id ASC
LIMIT 150000, 1

然后使用所述 id 检索实际数据:

SELECT ...
FROM table
WHERE id >= $id
AND ...
ORDER BY id ASC
LIMIT 0, 10

关于php - 如何优化限制查询以更快地从一个巨大的表中访问数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53264899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com