php - 优化 codeigniter 查询、Join 或 Where

标签 php mysql performance codeigniter query-optimization

我对 CI 比较陌生,我读到使用 join 可能会减慢报告速度,在我的数据库中会有超过 100k 条记录。我已经在 CI 中编写了这两个查询,但没有发现太大的区别。我想知道我是否正确地编写了这些查询,或者是否有更好的方法来获取此类查询,不幸的是,我不知道如何将这些发布到类似结构的示例 fiddle 中。

function select_data_specific_customer($from, $to, $customer, $threshold = '') {

        //die($from);
        $currentuserid = $this->session->userdata('userid');
        $this->db->select('timestamp');
        $this->db->select('count');
        $this->db->select('price');
        $this->db->select('totalPrice');
        $this->db->select('reference');
        $this->db->select('customers_r');
        $this->db->select('userID_r');
        $this->db->from('logger');
        $this->db->from('users_customer');
        $this->db->where(array('users_customer.custo_id' => "logger.customers"), NULL, FALSE);      
        //users_customer contains connection between users and customers (  id  userID  custo_id    checked )
        $this->db->where(array('users_customer.userID' => $currentuserid), NULL, FALSE);
        $this->db->where(array('users_customer.checked' => '1'), NULL, FALSE);
        $this->db->where('cast(timestamp as date) BETWEEN "' . date('Y-m-d', strtotime(str_replace('-', '/', $from))) . '" and "' . date('Y-m-d', strtotime(str_replace('-', '/', $to))) . '"');


        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }

            return $data;
        }

        return false;
    }

这是第二个函数

function select_data_specific_customer($from, $to, $customer, $threshold = '') {

        //die($from);
        $currentuserid = $this->session->userdata('userid');
         $this->db->select('timestamp');
        $this->db->select('count');
        $this->db->select('price');
        $this->db->select('totalPrice');
        $this->db->select('reference');
        $this->db->select('customers_r');
        $this->db->select('userID_r');
        $this->db->from('logger');
        $this->db->join("users_customer", "users_customer.custo_id=logger.customers", 'inner');
        $this->db->where(array('users_customer.userID' => $currentuserid), NULL, FALSE);
        $this->db->where(array('users_customer.checked' => '1'), NULL, FALSE);
        $this->db->where('cast(timestamp as date) BETWEEN "' . date('Y-m-d', strtotime(str_replace('-', '/', $from))) . '" and "' . date('Y-m-d', strtotime(str_replace('-', '/', $to))) . '"');


        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            foreach ($query->result() as $row) {
                $data[] = $row;
            }

            return $data;
        }

        return false;
    }

我想知道是否有更优化的方式来编写相同的查询,因为此时两个函数都需要大约 6 秒才能显示。 结果可能会增长到超过 200k,并且显示它的时间可能会增加。

我们将不胜感激。

最佳答案

return $this->db->query("your query here");

您是否尝试过上面的查询,并使用 * 进行选择

关于php - 优化 codeigniter 查询、Join 或 Where,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30801595/

相关文章:

php - 如何从php中的根目录外部访问文件

php - Drupal - 无法从 db_query 获得结果

c - 为什么对 makefile 的更改会使性能提高?

sql-server - 在 SQL Server 中有效地添加列

php - 在本地主机上使用 crontab 的 cronjob 没有结果

mysql - 记录 mysql 查询

phpmyadmin 和 mysql pdo lastInsertId()

php - 使用 REST API 在 wordpress 中上传图像

php - 在 PHP/Mysql 中生成随机数作为主键

c++ - MySQL, C++ - 以编程方式,MySQL Autoincrement 如何工作?