Solved

Views on Views slow in mysql?

Posted on 2010-11-17
5
522 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: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,
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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