gpt4 book ai didi

php - MySQL + PHP 数百万行,对多个查询的数据进行排序。内存消耗

转载 作者:行者123 更新时间:2023-11-29 23:51:09 30 4
gpt4 key购买 nike

所以我有两张 table 。一张表包含所有实际的campaign_id信息。第二个表包含 Campaign_id 的展示次数/统计信息

我在页面上有一个表格(我使用ajax,但这不是重点)。我想对一列进行“排序”,但所有行都是由 Campaign_id 表生成的,我首先运行每个营销事件的所有统计信息,然后将它们链接到每一行。然后在所有信息/数据完成后,它会对所有信息/数据进行排序。这使用了大量的内存和资源。 这真的有效吗?有没有更好的解决方案来排序大量数据?

// I have to increase memory because the sorting takes a lot of resource
ini_set('memory_limit','1028M');

// the column I want to sort
$sortcolumn = $this->input->post('sortcolumn');
// direction of the sort ASC/DESC
$sortby = $this->input->post('sortby');

// millions of impression data that is linked with campaign_id
$cdata= array();
$s = "SELECT report_campaign_id,";
$s .= "SUM(report_imps) AS imps ";
$s .= "FROM Campaign_Impressions";
$s .= "GROUP BY report_campaign_id ";
$r = $this->Qcache->result($s,0,'campaignsql');
foreach($r as $c) {

$cdata[$c->report_campaign_id]['imps'] = ($c->imps) ? $c->imps : 0;
}


// 500,000+ thousand campaigns
// I draw my table from these campaigns
$rows = array();
$s = "SELECT * FROM Campaigns ";
$r = $this->db->query($s)->result();
foreach($r as $c)
{
$row= array();
$row['campaign_id'] = $c->campaign_id;
// other campaign info here...

// campaign statistics here...
$row['campaign_imps'] = $cdata[$c->campaign_id]['imps'];

// table row
$rows[] = $row;
}


// prepare the columns i want to sort
$sortc = array();
foreach($rows as $sortarray) {
if (!isset($sortarray[ $sortcolumn ])) continue;
$sortc[] = str_replace(array('$',','),'',$sortarray[ $sortcolumn ]);
}

// sort columns and direction
array_multisort($sortc,(($sortby==='asc')?SORT_ASC:SORT_DESC),SORT_NATURAL,$rows);

正如您所看到的,“campaign_impressions”表正在运行“每个”广告系列的数据,看起来效率不高,但比每行运行查询来了解数据更有效。

(我不显示所有广告系列,但我需要运行每一个广告系列才能了解所有广告系列的排序)

最佳答案

你应该让MySQL使用order by来完成我的工作

如果这在 MySQL 方面仍然需要大量时间,请考虑在列上使用排序索引

关于php - MySQL + PHP 数百万行,对多个查询的数据进行排序。内存消耗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25648449/

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