简体   繁体   中英

Breaking a complex query into temporary tables in MYSQL

I am developing a personal proyect for academic books. I have some tables with +30.000 rows each for works, editions, authors and so on. All the information of the books —genres, subjects, authors, publishers, etc— is spread over a lot of tables with different types of relations.

I have a query for the main page that works, but the site takes six seconds to load. A lot of time… I was wondering which would be the proper approach for obtaining all the data I need with temporary tables.

What I want to do now is to join the temporary table _work with the related data of another table, say «genre». But the relationship between «work» and «genre» is done with the temporary table «work_has_genre».I know how to do that with normal tables in a single query:

SELECT *
FROM work a
LEFT JOIN (
    SELECT GROUP_CONCAT(f_a.id SEPARATOR '|') AS genre_id, GROUP_CONCAT(f_a.genre SEPARATOR '|') AS genre_name, f_b.work_id AS _work_id
    FROM genre f_a
    INNER JOIN (
        SELECT *
        FROM work_has_genre f_b_a
    ) f_b
    ON f_a.id=f_b.genre_id
    GROUP BY f_b.work_id
) f
ON a.id=f._work_id
WHERE a.id=13

I suppose the idea would be to break this actions in parts, but I don't know how. Could someone help me with a bit of pseudocode? Or maybe this is not the best approach. Any idea will be very welcomed!

A.

As I said in comments, I would first suggest reworking/flattening the subqueries as much as possible first, but once you get to semi-independent aggregations temp tables can be helpful.

Generally, the pattern is to put each such aggregation subquery's results into it's own temp table (with an index on the field the subquery was joined to the main query on) even if that means adding tables (and the main query's WHERE) to the original subquery, and then joining to the temp table in the main query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM