duanhan8757 2015-08-26 02:25
浏览 24

PHP + MySQL为ecomm运行各种报告

So we have tons of sale data and product data. We want to run a variety of reports and I am struggling on the best approach to do so. A lot of the data is redundant and since there is a ton of data I want to make sure I save as much resources as possible to make the reports fast and avoid the dreaded "Fatal error, memory exhausted" message.

There are 8 different data-sets. Each one is unique in itself but data could be shared be shared between them all. Some of the data is displayed in graphs others are displayed in tables.

What I was planning on doing was crating a few classes that would gather all the data and start to organize it. I was trying to build a central hub of data in an array/object that I can then pick bits and pieces of it to display the data-sets I would need. Doing so the problem I run into is it becomes very linear. To give you an example, here is a bit of code from what I have started:

class Reports {
   public $soldData = array();
   public $sizes = array();
   public $products = array();

   public function __construct() {
       $this->getSoldData();
       $this->getSizes();
       $this->getProductData();
   }

   public function getSoldData() {
       $data = $this->db->fetchAssoc("SELECT product_id, price FROM orders WHERE sold = 1 AND UNIX_TIMESTAMP(created) BETWEEN 1388534400 AND 1440555051");
       foreach($data as $d) {
           $this->soldData[$d['product_id']] = array('price' => $d['price']);
       }
   }

   public function getSizes() {
       $productIDs = implode(',', array_keys($this->soldData));
       $data = $this->db->fetchAssoc("SELECT id, name, abbr FROM sizes WHERE id IN ({productIDs})";
       foreach($data as $d) {
            $this->sizes[$d['id']] = array('name' => $d['name'], 'abbr' => $d['abbr'];
       }
   }

   public function getProductData() {
       $productIDs = implode(',', array_keys($this->soldData));
       $this->products = $this->db->fetchAssoc("SELECT * FROM products WHERE id IN ({productIDs})";
   }
}

Basically I have to grab one set of data before I can move on. You can't go out of order on this or the whole thing collapses in on itself. Also my end size of the class Reports is huge because it contains thousands of keys and nested arrays.

I feel like my thought process and approach to this is over complicated. I do these big all-in-one queries so I can limit the MySQL footprint to save resources. I will grab a bunch of IDs and run one query to pull a bunch of the same unique data back.

I am curious to see what problems this approach poses and if the StackOverflow community could shed some helpful pointers or tips to achieve my goal.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集