Solved

Views on Views slow in mysql?

Posted on 2010-11-17
5
518 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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