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

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.

  1. Which one is the best case in Codeigniter?

The first case is the best in CI or any other situation because the second case is a perfect example of repeating yourself. Be DRY - Don't Repeat Yourself.

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

One at a time.

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

As @Joshua answered - it will depend on the size of the tables.

What follows is not really what you asked for but I cannot help myself. You can make your code a lot more concise and potentially efficient.

Consider this sample_function()

public sample_function()
{
    $data = array();
    foreach(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) as $n)
    {
        $table = "table".$n;
        $data[$table."_data"] = $this->common_model->get_table($table);
    }
    return $data;
}

A small block of code returns a nice array full of table results. And it is DRY.

Your model could be improved by not using Query Builder (QB). QB is handy in some circumstances - but not in the case you present. QB is overkill for such a simple query. Using QB means you execute a large amount of code that ultimately (and almost literally) amounts to $this->db->query('YOUR QUERY HERE');

Consider this version of get_table

function get_table($table_name)
{
    $query = $this->db->query("select * from $table_name");
    return $query->num_rows() > 0 ? $query->result() : NULL;
}

Admittedly this is a "micro-optimization" for such a simple query. But, IMO it is much easier to write and to read.