【问题标题】:Select rows based on latest date with multiple joins -Code Ignitor根据具有多个连接的最新日期选择行-Code Ignitor
【发布时间】:2020-03-15 22:31:54
【问题描述】:

我的代码运行正常但速度很慢:

我的代码是:

    $this->db->select('BaseTbl.id');
    $this->db->from('tbl_all_calls as BaseTbl');
    $this->db->join('tbl_remark_info1 as Remarks','Remarks.id = (select max(id) from 
    tbl_remark_info1 as e2 where e2.cid=BaseTbl.id)','left');

    $this->db->where('BaseTbl.callcode', $num);
    $this->db->where('BaseTbl.deskid', $deskid);
    $this->db->where('Remarks.remarkDate<=',date('Y-m-d'));
    $this->db->where('Remarks.taken', 1);
    $this->db->where('BaseTbl.createdDtm!=', date('Y-m-d'));
    $this->db->group_by('BaseTbl.mobile');
    $this->db->order_by('remarkDate', 'DESC');
    $query = $this->db->get();
    $result = $query->num_rows();      
    return $result;

如何提高性能?

【问题讨论】:

  • 你检查在mysql中运行这个sql了吗?
  • 如果您只返回计数,那么为什么需要 order 或 group by,因为 total num_rows 将是一个值或?
  • 可以在 sql 中运行,但运行缓慢。你能在 CI 中给出查询,返回加入表中的最新记录吗?
  • 为表字段提供索引会增加执行时间。

标签: php jquery mysql ajax codeigniter


【解决方案1】:

您可以尝试为以下列添加索引,因为它会显着提高搜索速度。

    Remarks.id
    Remarks.taken
    BaseTbl.deskid
    e2.cid

【讨论】:

  • 你能提供创建索引的例子吗?
猜你喜欢
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 2016-01-31
  • 2019-12-07
  • 1970-01-01
  • 2021-01-25
  • 1970-01-01
  • 2017-02-19
相关资源
最近更新 更多