1

I got that the only solution to avoid the Maximum time execution CodeIgniter 3 issue is to increase the time execution from 30 to 300 for example.

I'm using CodeIgniter in a news website. I'm loading only 20 latest news in the news section page and I think that it's not a big number to make the server out of execution time. (Notice that the news table has more than 1400 news and the seen table has more than 150.000 logs).

I say that it's not logical that the user should wait for more than 50 seconds to get the respond and load the page.## Heading ##

Is there any useful way to load the page as fast as possible without "maximum time execution"?

My Code in the model:

public function get_section_news($id_section = 0, $length = 0, $id_sub_section = 0, $id_news_lessthan = 0) {
    $arr = [] or array();
    //
    if (intval($id_section) > 0 and intval($length) > 0) {
        //
        $where = [] or array();
        $where['sections.activity'] = 1;
        $where['news.deleted'] = 0;
        $where['news.id_section'] = $id_section;

        $query = $this->db;
        $query
            ->from("news")
            ->join("sections", "news.id_section = sections.id_section", "inner")
            ->order_by("news.id_news", "desc")
            ->limit($length);
        //
        if (intval($id_sub_section) > 0) {
            $where['news.id_section_sub'] = $id_sub_section;
        }
        if ($id_news_lessthan > 0) {
            $where['news.id_news <'] = $id_news_lessthan;
        }
        //
        $get = $query->where($where)->get();
        $num = $get->num_rows();

        if ($num > 0) {
            //
            foreach ($get->result() as $key => $value) {
                $arr['row'][] = $value;
            }
        }
        $arr['is_there_more'] = ($length > $num and $num > 0) ? true : false;
    }
    return $arr;
}
nael_d
  • 27
  • 1
  • 11
  • can you add your query that fetch the records from database? – Gokul Shinde Jun 11 '16 at 13:47
  • @GokulShinde refresh the page plz – nael_d Jun 11 '16 at 14:04
  • Its not the framework Codeigniter 3 is extremely fast. I'm looking at your requirement "I'm loading only 20 latest news" - that should not need all the code you have here. Put a datetime field in the news table, index it, and just grab 20 records sorted by your date field.Thats going to be very fast. Next use codeigniter caching. Even if your content changes every 5 minutes, you will still save a bunch of database calls by using caching. – cartalot Jun 11 '16 at 16:16
  • I know it's complex code but to make any request i need dynamic and flexible. However, i think that it's the same if i got the last 20 news (or any number) ordered by their id_news desc or by datetime desc. Is there really any differences between ordering by id_news or datetime? And please could you guide me how to cache them? Thanks so much for you. – nael_d Jun 11 '16 at 18:56

1 Answers1

0

This usually has nothing to do with the framework. You may run the following command on your mysql client and check if there are any sleeping queries on your database.

    SHOW FULL PROCESSLIST

most likely you have sleeping queries since you are not emptying result set with

   $get->free_result();

Another problem may be slow queries on this I recommend the following

1) make sure you are using the same database engine on all tables for this I recommend INNODB as some engines lock the whole table during a transaction which is undesirable You should have noticed this already when you ran show full processlist

2) Run your queries on a mysql client and observe how long they will take to execute. If they take too long it may be a result of unindexed tables. You may Explain your query to identify unindexed tables. You may follow these 1,2,3 tutorials on indexing your tables. Or you can do it easily with tools like navicat

Community
  • 1
  • 1
Stephen Mudere
  • 444
  • 3
  • 10
  • I'm using InnoDB for all the tables in the database, and using mysqli in php5.6. However, I've got your answer clearly but i want to ask you a little bit: $get->free_result() must be put after the last foreach() {}, yeah? – nael_d Jun 11 '16 at 16:17
  • yes you must put it after $num = $get->num_rows(); – Stephen Mudere Jun 11 '16 at 16:35