Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Views - When to use & when not to

Tags:

sql

mysql

views

the mysql certification guide suggests that views can be used for:

  • creating a summary that may involve calculations
  • selecting a set of rows with a WHERE clause, hide irrelevant information
  • result of a join or union
  • allow for changes made to base table via a view that preserve the schema of original table to accommodate other applications

but from how to implement search for 2 different table data?

And maybe you're right that it doesn't work since mysql views are not good friends with indexing. But still. Is there anything to search for in the shops table?

i learn that views dont work well with indexing so, will it be a big performance hit, for the convenience it may provide?

like image 456
iceangel89 Avatar asked Sep 27 '09 13:09

iceangel89


People also ask

Should I use views in MySQL?

Views should be used when: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 ) Backward compatibility and query reusability. Working with computed columns.

When should I use a views in SQL?

Views are generally used to focus, simplify, and customize the perception each user has of the database. Views can be used as security mechanisms by letting users access data through the view, without granting the users permissions to directly access the underlying base tables of the view.

When should views be used?

Views are virtual tables that can be a great way to optimize your database experience. Not only are views good for defining a table without using extra storage, but they also accelerate data analysis and can provide your data extra security.

What is view in MySQL and why it is used?

MySQL ViewsA view contains rows and columns, just like a real table. The fields in a view are fields from one or more real tables in the database. You can add SQL statements and functions to a view and present the data as if the data were coming from one single table. A view is created with the CREATE VIEW statement.


1 Answers

A view can be simply thought of as a SQL query stored permanently on the server. Whatever indices the query optimizes to will be used. In that sense, there is no difference between the SQL query or a view. It does not affect performance any more negatively than the actual SQL query. If anything, since it is stored on the server, and does not need to be evaluated at run time, it is actually faster.

It does afford you these additional advantages

  • reusability
  • a single source for optimization
like image 82
Raj More Avatar answered Sep 21 '22 23:09

Raj More