Codeigniter mysql views vs query

I develop Codeigniter, and when it gets into complex database queries I use

$this->db->query('my complicated query'); 

then move to the object array using $query->result();

and still it’s very good and useful

Now my question

  • What if I want to create a mysql view and select it? Will be

     $this->db->from('mysql_view') 

    take mysql view as table or not?

  • And if I do, there will be some kind of performance difference - are these views faster than regular database queries?

  • Which would be best used in the Codeigniter and MYSQL databases dealing with complex queries, as I understand that ActiveRecord is just a query builder, and some tests are even a little slower

Thanks in advance for your advice.

+4
source share
1 answer
  • MySQL views are queried in the same way as tables, on a side note; you cannot have a table and view with the same name.
  • Depending on the query you use in the view, the views can be internally cached, so in the long run, yes, they are faster.
  • The best practice in this case is to use what you think is convenient for yourself and your team, I personally stick to using $this->db->query(); , since it’s easier for me to modify a simple query of this type to have some advanced features, such as subqueries or other things that are difficult and / or impossible to do with the CI query builder. My advice would be to stick to one of the query methods - if you use ->query() , then use them everywhere if you use the query builder and then use it wherever possible to achieve the result with it.
+5
source

All Articles