Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql VIEWS vs. PHP query

Tags:

I am working on a web application which involves create list of Restaurants in various lists like "Joe's must visit places". Now for each Restaurant and list, I have display on website which calculates

  • Calculating popularity of a Restaurant
  • Popularity of a list
  • Number of lists a Restaurant is present in

Currently I am using MySQL statements in PHP for this but planning to switch to MySQL VIEWS and do a simple select statement in PHP...

my question is, What is Advantage/Disadvantage of using VIEWS over writing sql queries in PHP?

like image 997
Tarun Avatar asked Dec 13 '10 08:12

Tarun


People also ask

Are views faster than queries MySQL?

Contrary to the answers - In my experience, for views with lots of joins, doing a direct query runs faster.

Should I use MySQL views?

Views should be used when:Simplifying complex queries (like IF ELSE and JOIN or working with triggers and such) Putting extra layer of security and limit or restrict data access (since views are merely virtual tables, can be set to be read-only to specific set of DB users and restrict INSERT )

Which is better MySQL or PHP?

PHP is a fast and feature-rich open source scripting language used to develop Web Applications or Internet / Intranet Applications. MySQL is a powerful open source database server built based on a relational database management system (RDBMS) and is capable of handling a large concurrent database connection.


2 Answers

Using views adds a level of abstraction : you may later change the structure of your tables, and you will not have to change the code that displays the information about the lists, because you will still be querying the view (the view definition may change, though).

The main difference is that views are updated after each insertion, such that the data is "ready" whenever you query the view, whereas using your custom query will have MySQL compute everything each time (there is some caching, of course).

The bottom line is that if your lists are updated less frenquently than they are viewed, you will see some gains in performance in using views.

like image 164
Wookai Avatar answered Oct 05 '22 22:10

Wookai


My complete answer would depend upon several things (from your application's perspective):

  • do you plan to allow users to create and share such lists?
  • can users create lists of any kind, or just by plugging values into existing query templates?

Assuming you have a couple of pre-defined lists to display:

Use of views offers a couple of advantages:

  • your code will be cleaner
  • the query to generate the views will not have to be parsed each time by mysql.

I'm not sure about this: I don't think mysql caches views as Tomasz suggests - I don't think views contain "already preparted data".

One disadvantage is that the logic involved in creating the list goes into the database instead of living in your PHP code - something I'm very averse to. In my world databases are for data, and code is for logic.

Cheers

like image 24
jrharshath Avatar answered Oct 05 '22 23:10

jrharshath