I would like to improve the speed of SQL statements (with code) that INNER JOIN 12 tables.I have changed it to LEFT OUTER JOIN, but it does not lead to improvement.

Asked 1 years ago, Updated 1 years ago, 78 views

SQL statements include:
In this SQL, there are only two tables that are INNER JOIN and WHERE statements.
This will take about 10 seconds to complete the aggregation.

However, due to this requirement, there are 12 tables to INNER JOIN. Similarly, adding and executing INNER JOIN and WHERE statements will take more than an hour.

SELECT artists.user_id, artists.staff_id,
Sum(events.sum), \
Sum(goods.sum),\
FROM artists\
INNER JOIN`events`ON`artists`.`id`=`events`.`artist_id`\
INNER JOIN`goods`ON`artists`.`id`=`goods`.`artist_id`\ 
WHERE(`events`.`created_at`BETWEEN'2017-01-01'AND'2017-07-30')\
AND(`goods`.`created_at`BETWEEN'2017-01-01'AND'2017-07-30')\
GROUP BY artists.user_id, artists.staff_id;

I would like to improve the speed of SQL, but there are no further conditions to narrow it down, so
We are looking for ways to improve by reviewing SQL statements.

Also, the idea of batch processing was not allowed due to the requirements of this time.

As stated in the raw SQL statement, it actually runs on Ruby on Rails application.
Therefore, we are also looking for asynchronous processing, but in order to INSERT the results of the aggregation into DB,
We are concerned that asynchronous processing may prevent data from being included in the record we want.

I've tried and failed to make INNER JOIN LEFT OUTER JOIN, but it's hard to improve.

Ideally, I want you to complete it in 1-2 minutes.

Also, this aggregation process may be performed in different periods, but the same SQL statement may not be used over and over again, so
I don't think it makes much sense to do query caching.

Also, I have an index key around the foreign key.

Thank you for your cooperation.

mysql sql

2022-09-30 21:24

1 Answers

This is not the answer you want, but if you do EXPLAIN for the SQL statement you want to execute, you will see which description (search criteria, merge, etc.) is taking a long time.

# The grammar is slightly different from EXPLAIN PLAN for Oracle, EXPLAIN ANALYE for PostgreSQL, and EXPLAIN for MySQL, but the same is true that you can get an execution plan.

Example: For PostgreSQL:

EXPLAIN ANALYZE
SELECT artists.user_id, artists.staff_id,
Sum(events.sum), \
Sum(goods.sum),\
FROM artists\
INNER JOIN`events`ON`artists`.`id`=`events`.`artist_id`\
INNER JOIN`goods`ON`artists`.`id`=`goods`.`artist_id`\ 
WHERE(`events`.`created_at`BETWEEN'2017-01-01'AND'2017-07-30')\
AND(`goods`.`created_at`BETWEEN'2017-01-01'AND'2017-07-30')\
GROUP BY artists.user_id, artists.staff_id;

EXPLAIN I think it's better to look at the results of EXPLAIN to find out where it's slow.


2022-09-30 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.