Query takes a very long time to execute

て烟熏妆下的殇ゞ 提交于 2019-12-25 00:54:53

问题


I have a query that I am trying to execute at the moment but after letting it load for a few minutes I am thinking there is probably something I am doing wrong.

Here is what I am trying to execute:

SELECT *
FROM open_hours
INNER JOIN open_times
    ON open_hours.id = open_times.open_hour_id
INNER JOIN off_peak_times
    ON open_hours.id = off_peak_times.open_hour_id;

I have three tables:

open_hours:

Roughly 14k rows

id;
owner_type;
owner_id;
seats;
time_interval;
max_at_one_time;
created_at;
updated_at;
minutes_in_advance;

open_times:

Roughly 18k rows

id;
open_hour_id;
time_start;
time_end;
weekday;
created_at;
meal_type;

off_peak_times:

Roughly 10k rows

id;
open_hour_id;
time;
discount;
seats;
created_at;
updated_at;
weekday;

I have tried reducing the SELECT * to a SELECT open_hours.id, open_times.meal_type since in reality that's the only number I'm trying to get right now. It gets me the results in roughly 30 seconds, which still sounds like a lot to me.

I run other queries on other tables that are a lot bigger and yet get results almost instantly, so is there something that I am missing or not considering?


回答1:


To make the query faster in your case there have three things you can do. Don't know it will help you or not but According to SQL optimizing query concepts, it must help.

  1. Create CLUSTERED INDEX for all your tables. Cluster index makes the SELECT query faster at 30:70 of ratio.
  2. You should make column list instead of STAR statement (avoid SELECT *). Extra columns make BUFFER-POOL heavy.
  3. You can use VIEW instead of the query. Because VIEW during JOIN statements is better than the normal query.

From above all of three, you must try first option (CLUSTER INDEX), which will really improve performance. Hope this will be helpful.




回答2:


Run this

Alter table open_times add index(open_hour_id);

Alter table off_peak_times add index(open_hour_id);

Then run your query again. These are small enough tables, i would not worry about reading a bunch right now about your execution plan. I think you will find this speeds it up.




回答3:


For a further analysis it is important to know the query behavior and table information. This is possible executing the statements below:

EXPLAIN EXTENDED SELECT * FROM open_hours INNER JOIN open_times ON open_hours.id = open_times.open_hour_id INNER JOIN off_peak_times ON open_hours.id = off_peak_times.open_hour_id\G
SHOW WARNINGS \G
EXPLAIN FORMAT=JSON SELECT * FROM open_hours INNER JOIN open_times ON open_hours.id = open_times.open_hour_id INNER JOIN off_peak_times ON open_hours.id = off_peak_times.open_hour_id\G           -- MYSQL 5.6+ only

Additionally, for each unique table involved:

SHOW TABLE STATUS FROM <db> LIKE '<tablename>' \G 
SHOW INDEXES FROM <db>.<tablename> \G 
SHOW CREATE TABLE <db>.<tablename> \G


来源:https://stackoverflow.com/questions/50919848/query-takes-a-very-long-time-to-execute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!