• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 550
  • Last Modified:

Views on Views slow in mysql?

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
letharion
Asked:
letharion
  • 3
2 Solutions
 
MestikaCommented:
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
 
letharionAuthor Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
letharionAuthor Commented:
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
 
letharionAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now