Solved

Views on Views slow in mysql?

Posted on 2010-11-17
5
514 Views
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                     |
+----+-------------+------------+--------+---------------------------------+---------+---------+-----------------------+--------+---------------------------------+
0
Comment
Question by:letharion
  • 3
5 Comments
 
LVL 3

Assisted Solution

by:Mestika
Mestika earned 500 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.
0
 
LVL 6

Accepted Solution

by:
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.
0
 
LVL 45

Expert Comment

by:Kdo
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,
Kent
0
 
LVL 6

Author Comment

by:letharion
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.
0
 
LVL 6

Author Closing Comment

by:letharion
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now