[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


Views on Views slow in mysql?

Posted on 2010-11-17
Medium Priority
Last Modified: 2012-05-10
I was asked to pull certain data from a DB, and the query turned out to be rather complicated.

First, I realised there was already a View (1) that did part of the work to pull together some data I needed.
Second, I created a second View (2), which pulls data from View (1), and does further joins to prepare the final data I need.
Third, the final query does a group-wise max select:
SELECT co1.run, co1.id, co1.score FROM score_view AS co1 JOIN score_view AS co2  ON co1.run = co2.run where co1.project = 6110 GROUP BY co1.run, co1.id HAVING co1.score = MAX(co2.score);

My problem is that the query now takes 25 minutes. Being mainly a programmer, I think of Views as "functions", but I suspect that using a View like I would a function isn't very clever.
While I understand that "Using filesort" is bad for performance, I don't know what it takes to remove it. Is the main issue that I nest Views? Or that I use them at all?
Posting my explain below.
| id | select_type | table      | type   | possible_keys                   | key     | key_len | ref                   | rows   | Extra                           |
|  1 | PRIMARY     | <derived3> | ALL    | NULL                            | NULL    | NULL    | NULL                  | 248647 | Using temporary; Using filesort |
|  1 | PRIMARY     | S          | eq_ref | PRIMARY,Index_2,FK_Strategies_2 | PRIMARY | 4       | BaseStatisticsView.id |      1 | Using where                     |
|  1 | PRIMARY     | <derived5> | ALL    | NULL                            | NULL    | NULL    | NULL                  | 248647 | Using join buffer               |
|  1 | PRIMARY     | S          | eq_ref | PRIMARY,FK_Strategies_2         | PRIMARY | 4       | BaseStatisticsView.id |      1 | Using where                     |
|  5 | DERIVED     | V          | ref    | Index_4,id,Index_3              | Index_4 | 18      |                       | 174420 | Using where; Using temporary    |
|  5 | DERIVED     | T          | ref    | Index_4,id,Index_3              | Index_4 | 23      | PPS.V.id              |      1 | Using where                     |
|  5 | DERIVED     | A          | ref    | Index_4,id,Index_3              | Index_4 | 23      | PPS.V.id              |      1 | Using where                     |
|  3 | DERIVED     | V          | ref    | Index_4,id,Index_3              | Index_4 | 18      |                       | 174420 | Using where; Using temporary    |
|  3 | DERIVED     | T          | ref    | Index_4,id,Index_3              | Index_4 | 23      | PPS.V.id              |      1 | Using where                     |
|  3 | DERIVED     | A          | ref    | Index_4,id,Index_3              | Index_4 | 23      | PPS.V.id              |      1 | Using where                     |
Question by:letharion
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Assisted Solution

Mestika earned 2000 total points
ID: 34153693
Hi letharion,

a VIEW in MySQL (and databases general) can boost your performance but it most certainly can also slow down your querying.

In databases it is always a question of trade-offs but in your case, on the top of my head, I would believe that you will have more use of creating smart indexes on your tables (draw them up as a B+-tree and see which primary index you will need).

The view can be handy sometimes but my own rule of thump, especially in MySQL is, that I try to avoid them unless they give me a performance boost.

But you must remember that the DBMS is creating an "temporary" table and that table has to be maintained and that can cost a great deal.

And again I must emphasize that it is a trade-off which is would recommend you to use the EXPLAIN in MySQL to see the cost, the plan, the indexes in use and so on.

But start by leaving the view2 and see how it goes.

Another method, which may not be important or advantageous for you is to use the notion of denormalization to get quick queries but again you have to think: What is my queries doing for me. Is they only read (SELECT) or do they perform updates and insertions.
If you are interested, you can always look up denormalization.

Please ask if there is other question or I've explained myself poorly.

Accepted Solution

letharion earned 0 total points
ID: 34153877
Thanks for the reply Mestika :)

What I tried myself was to bypass View (1), and recreated View (2) with all the necessary data instead.
I'm not entirely sure why this was an improvement, but my query time dropped from 25-30 minutes, to 4-5 seconds. 360-375x speed increase. Nice.

What I can see is that the explain looks completely different afterwards, but exactly why I can't tell.
LVL 46

Expert Comment

by:Kent Olsen
ID: 34154928
Hi Letharion,

Just a quick follow-up.

Views are atomic operations.  That is, the result of a view is a derived table.  

If you apply a filter (WHERE clause) to the query of a view, the execution is usually dependent upon how the view is structured.  If the view has no filter (no WHERE clause) the filter specified on the query is processed as if it were part of the view.  If the view does have a filter, the view returns a derived table, and your query is then applied to it.

The timings that you provided suggest that that is what occurred.  When you rewrote the query so that all of the logic was in one place, the DBMS handled it as a single query and was able to skip the intermediate step(s) that took so much time.

Good Luck,

Author Comment

ID: 34156092
I object to myself ;)
Kdo provided further very interesting information, that explained why I was having my initial problem to begin with. I suggest splitting the answer on all three posts instead of just the two first.

Author Closing Comment

ID: 34186318
I'm marking my own answer, because my own experimentation gave great results, but I feel that you're suggestions are valuable, and will mark then as well.

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question