View to save server load

Hi,
I’ve to do a heavy query
so I’m wondering if create a view
can help to save server load.
I mean if I’ve to do the query on the
users table I could create a view before
to filter records like


CREATE VIEW myuser AS SELECT id,email,first_name,last_name FROM user WHERE status='confirmed';

than


SELECT id,email,first_name,last_name FROM myuser WHERE ... //extra where clauses

What do you think about it ?

Bye.

the select in the “view” is run every time you access the view so I’m not sure there are any significant reductions in server load by using a view.

me too
this is the reason I made the thread :slight_smile:
Is there any advance to create a view (in this example) ?

I’m not sure what you are asking now.

A view is simply a “virtual” or “logical” table. The rows in a view are not actually stored anywhere and so every time you access the view in a query, the select specified in the view is run every time.

A view is mainly used to hide some columns in the actual table(s) from users who don’t need to see/access them.

Why is this query so heavy?
Maybe you’d be better off investigating if you can optimize indexes so that this query performs faster?
Try:
EXPLAIN YOUR_QUERY

@Kalon
so it’s a catch-22 it not worth the try :frowning:

@Aleksejs
The problem is the query is build dinamically at run time
by a form so it’s difficult to optimize all the fields.
But I’ll get it a try.

I ended up making a store procedure that creates
a new table to make the heavy query.

… to make the heavy query???

Generaly, no - views in mysql make things slower.
http://stackoverflow.com/questions/2878227/how-do-mysql-views-work

sorry, that’s not true

in fact, the page you linked to simply says[indent]Because a view is a derived table, the performance of the view is only as good as the query it is built on[/indent]

… but not worse

:slight_smile:

Hmmm… It also says the next sentence:
Because a view is a derived table, the performance of the view is only as good as the query it is built on. If that query sucks, the performance issue will just snowball…

I am not a native English speaker, so I assumed, that “will just snowball”==“will be at least as bad or worse”.

as bad or worse than what?

if the query performs poorly, the view will not be worse

reason? a view is nothing but a stored query!!

:slight_smile: