dsi36131 2016-10-12 14:43
浏览 30

Codeigniter:常见功能。 vs多个功能。 数据库查询性能

I hope to get answers based on experience and knowledge based on Codeigniter for this question. Lets say I have 10 tables in a database and I want to query all the rows in the 10 tables at the same time in the same controller. and there are two ways of doing this,

Case 01 - Use common query function like below,

Controller

sample_function(){
    'table1_data'        => $this->common_model->get_table( 'table1' ),
    'table2_data'        => $this->common_model->get_table( 'table2' ),
    ...
    'table9_data'        => $this->common_model->get_table( 'table9' ),
    'table10_data'        => $this->common_model->get_table( 'table10' )
}

and then in the common_model

function get_table( $table_name ){
    $this->db->select()->from( $table_name );
    $sql_stmt = $this->db->get();
    return $sql_stmt->result();
}

so in this case the get_table( $table_name ) will run 10 times.

Case 02 - Use seperate functions for each 10 tables like below,

Controller

sample_function(){
    'table1_data'        => $this->common_model->get_table1(),
    'table2_data'        => $this->common_model->get_table2(),
    ...
    'table9_data'        => $this->common_model->get_table9(),
    'table10_data'        => $this->common_model->get_table10()
}

so that the common_model will be like this and here we have 10 functions not like use the same function in Case01

function get_table1()
{
    $this->db->select()->from( 'table1' );
    $sql_stmt = $this->db->get();
    return $sql_stmt->result();
}
function get_table2()
{
    $this->db->select()->from( 'table2' );
    $sql_stmt = $this->db->get();
    return $sql_stmt->result();
}
....
function get_table9()
{
    $this->db->select()->from( 'table9' );
    $sql_stmt = $this->db->get();
    return $sql_stmt->result();
}
function get_table10()
{
    $this->db->select()->from( 'table10' );
    $sql_stmt = $this->db->get();
    return $sql_stmt->result();
}

in this case 10 seperate functions run one time each.

It is obvious the Case 01 is the best when we consider code usability but my quiestion is when we consider the performance,

  1. Which one is the best case in Codeigniter?

  2. When it comes to Case 01, the get_table will run simultaniously or one at a time?

  3. Whis case is the best for performance in CI?

Thnaks

  • 写回答

2条回答 默认 最新

  • doushizhou4477 2016-10-12 14:47
    关注

    Wild guess based on years of experience in backend development: Both will run equally fast/slow depending on how many rows you have in your tables.

    As you already have the code, just give it a try with a suitable benchmark. Let both routines run a couple thousand times and take the average.

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错