6
I have a query that is taking 6/8 seconds to execute.
The database I’m using is Mysql.
In Phpmyadmin I did the following:
EXPLAIN SELECT id_categoria, sc.categoria, categoria_principal, associada FROM
get_produto_categoria gpc INNER JOIN shop_categoria sc USING(id_categoria) WHERE
gpc.id_produto = 2254 ORDER BY categoria_principal DESC, associada DESC, sc.ord ASC,
sc.id_categoria ASC
See below the query result:
I have always done optimizations using only tables, but the above query uses a view (get_product_category).
I can’t understand the first line, where it says "Table: derived2 / Rows:23248".
How would the interpretation of the above result take into account that I am using a View.
Okay, but I can’t come up with a solution to optimize the query based on the information above. I’m using a view, but it doesn’t accept indexes. What I have to do to optimize the consultation?
– Filipe Moraes
@Filipe To help with this question just by looking at the tables and the indexing of each one. Yours view will generate the data in real time whenever you access it. The delay is not view in itself but yes of the tables that the same query. The most common is that queries and/or relation keys are being performed in non-indexed fields.
– Zuul
Okay, but according to the image I posted about the explain result, are using indexed fields, or am I wrong?
– Filipe Moraes
I think
derived2
refers precisely to the view, and searching for all of its 23248 lines must be causing the slowness. This may be the case of materializing the view in a table, or using Mariadb, which has native support to materialized views.– bfavaretto
@Philip According to your
EXPLAIN
the second line from the bottom is problematic because Mysql has not found any useful indexing. See more here. But without knowing the structure of the tables it is difficult to state the origin of the problem. Just run:DESCRIBE nomeTabela
, take a screenshot and put in your question.– Zuul