Reputation: 313
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?
Upvotes: 0
Views: 12207
Reputation: 695
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.
CLUSTERED INDEX
for all your tables. Cluster index makes the SELECT
query faster at 30:70 of ratio.SELECT *
). Extra columns make BUFFER-POOL heavy.From above all of three, you must try first option (CLUSTER INDEX), which will really improve performance. Hope this will be helpful.
Upvotes: 0
Reputation: 561
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.
Upvotes: 2
Reputation: 56
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
Upvotes: 0