Vijaykumar Ponnusamy
Vijaykumar Ponnusamy

Reputation: 225

Mysql View Working Method?

How the Mysql View is working Internally ?

create view testView  as select * from employee;

if run this what will happen internally?

Upvotes: 1

Views: 79

Answers (1)

G-Nugget
G-Nugget

Reputation: 8846

Usually, crating a view doesn't really do much until it is called on. It pretty much just gets validated until it is used.

Views usually use the MERGE method which merges the query for the view with the query that calls it. The other method for views is TEMP TABLE which creates a temporary table whenever it is needed, but this is less common.

An basic example of a view using the MERGE method:

View Query:

SELECT t1.col1, t2.col2, t2.col3 FROM t1 JOIN t2 USING(id) WHERE x = 1

Calling Query:

SELECT col1, col3 FROM view WHERE y = 2

MySQL basically executes this query as:

SELECT col1, col3 FROM (SELECT t1.col1, t2.col2, t2.col3 FROM t1 JOIN t2 USING(id) WHERE x = 1) WHERE y = 2

However, since the queries get merged and optimized, it would be optimized to a simpler form like this:

SELECT col1, col3 FROM t1 JOIN t2 USING(id) WHERE x = 1 AND y = 2

Upvotes: 2

Related Questions