Inner join is very slow. How can I optimize?How can I prevent SQL injection in PHP?Left Join outperforming Inner Join?INNER JOIN ON vs WHERE clauseMysql Query optimization, Move a CASE Exists (SELECT…) into a left joinWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?Inner join ambiguous column name checking superfluous?Query optimization with inner queryMySQL — Explicit INNER JOIN with selection from multiple tablesmysql random selection in inner join
Is Trump personally blocking people on Twitter?
Does Lufthansa weigh your carry on luggage?
Integer Lists of Noah
How do I set up a beta channel for my Steam game?
Optimization terminology: "Exact" v. "Approximate"
Why are they 'nude photos'?
How did the hit man miss?
How would vampires avoid contracting diseases?
Machine learning and operations research projects
How is angular momentum conserved for the orbiting body if the centripetal force disappears?
Has anyone in space seen or photographed a simple laser pointer from Earth?
Shortest hex dumping program
Why weren't bootable game disks ever common on the IBM PC?
What's the point of having a RAID 1 configuration over incremental backups to a secondary drive?
Was I subtly told to resign?
Why are Hobbits so fond of mushrooms?
Confirming the Identity of a (Friendly) Reviewer After the Reviews
If a non-friend comes across my Steam Wishlist, how easily can he gift me one of the games?
Why does the U.S. tolerate foreign influence from Saudi Arabia and Israel on its domestic policies while not tolerating that from China or Russia?
Why did Harry Potter get a bedroom?
Does throwing a penny at a train stop the train?
How can I calculate the sum of 2 random dice out of a 3d6 pool in AnyDice?
During copyediting, journal disagrees about spelling of paper's main topic
Constructive proof of existence of free algebras for infinitary equational theories
Inner join is very slow. How can I optimize?
How can I prevent SQL injection in PHP?Left Join outperforming Inner Join?INNER JOIN ON vs WHERE clauseMysql Query optimization, Move a CASE Exists (SELECT…) into a left joinWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?Inner join ambiguous column name checking superfluous?Query optimization with inner queryMySQL — Explicit INNER JOIN with selection from multiple tablesmysql random selection in inner join
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to find the intersection of multiple queries on the time attribute.
I tried this inner joining of all of them, but it takes an extreme amount of time and never finishes.
SELECT
pdx.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND time >= '1993' AND time <= '2018'
) as pdx
ON aggregate_power_demands.time = pdx.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND time >= '1993' AND time <= '2018'
) as sea
ON pdx.time = sea.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND time >= '1993' AND time <= '2018'
) as boi
ON sea.time = boi.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND time >= '1993' AND time <= '2018'
) as geg
ON boi.time = geg.time
mysql
|
show 5 more comments
I want to find the intersection of multiple queries on the time attribute.
I tried this inner joining of all of them, but it takes an extreme amount of time and never finishes.
SELECT
pdx.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND time >= '1993' AND time <= '2018'
) as pdx
ON aggregate_power_demands.time = pdx.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND time >= '1993' AND time <= '2018'
) as sea
ON pdx.time = sea.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND time >= '1993' AND time <= '2018'
) as boi
ON sea.time = boi.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND time >= '1993' AND time <= '2018'
) as geg
ON boi.time = geg.time
mysql
What is type of a time column on which you are making join? Do you have indexes in all tables on this field?
– Pustovalov Dmitry
Mar 25 at 18:43
it is a DATETIME, not sure what you mean by indexes
– John Karasev
Mar 25 at 18:45
no I dont have indexes
– John Karasev
Mar 25 at 18:46
which MySQL / MariaDB version you are using
– Bernd Buffen
Mar 25 at 18:51
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
– John Karasev
Mar 25 at 19:01
|
show 5 more comments
I want to find the intersection of multiple queries on the time attribute.
I tried this inner joining of all of them, but it takes an extreme amount of time and never finishes.
SELECT
pdx.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND time >= '1993' AND time <= '2018'
) as pdx
ON aggregate_power_demands.time = pdx.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND time >= '1993' AND time <= '2018'
) as sea
ON pdx.time = sea.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND time >= '1993' AND time <= '2018'
) as boi
ON sea.time = boi.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND time >= '1993' AND time <= '2018'
) as geg
ON boi.time = geg.time
mysql
I want to find the intersection of multiple queries on the time attribute.
I tried this inner joining of all of them, but it takes an extreme amount of time and never finishes.
SELECT
pdx.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND time >= '1993' AND time <= '2018'
) as pdx
ON aggregate_power_demands.time = pdx.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND time >= '1993' AND time <= '2018'
) as sea
ON pdx.time = sea.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND time >= '1993' AND time <= '2018'
) as boi
ON sea.time = boi.time
INNER JOIN
(
SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND time >= '1993' AND time <= '2018'
) as geg
ON boi.time = geg.time
mysql
mysql
edited Mar 27 at 6:18
Philosophist
10312 bronze badges
10312 bronze badges
asked Mar 25 at 18:40
John KarasevJohn Karasev
74 bronze badges
74 bronze badges
What is type of a time column on which you are making join? Do you have indexes in all tables on this field?
– Pustovalov Dmitry
Mar 25 at 18:43
it is a DATETIME, not sure what you mean by indexes
– John Karasev
Mar 25 at 18:45
no I dont have indexes
– John Karasev
Mar 25 at 18:46
which MySQL / MariaDB version you are using
– Bernd Buffen
Mar 25 at 18:51
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
– John Karasev
Mar 25 at 19:01
|
show 5 more comments
What is type of a time column on which you are making join? Do you have indexes in all tables on this field?
– Pustovalov Dmitry
Mar 25 at 18:43
it is a DATETIME, not sure what you mean by indexes
– John Karasev
Mar 25 at 18:45
no I dont have indexes
– John Karasev
Mar 25 at 18:46
which MySQL / MariaDB version you are using
– Bernd Buffen
Mar 25 at 18:51
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
– John Karasev
Mar 25 at 19:01
What is type of a time column on which you are making join? Do you have indexes in all tables on this field?
– Pustovalov Dmitry
Mar 25 at 18:43
What is type of a time column on which you are making join? Do you have indexes in all tables on this field?
– Pustovalov Dmitry
Mar 25 at 18:43
it is a DATETIME, not sure what you mean by indexes
– John Karasev
Mar 25 at 18:45
it is a DATETIME, not sure what you mean by indexes
– John Karasev
Mar 25 at 18:45
no I dont have indexes
– John Karasev
Mar 25 at 18:46
no I dont have indexes
– John Karasev
Mar 25 at 18:46
which MySQL / MariaDB version you are using
– Bernd Buffen
Mar 25 at 18:51
which MySQL / MariaDB version you are using
– Bernd Buffen
Mar 25 at 18:51
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
– John Karasev
Mar 25 at 19:01
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
– John Karasev
Mar 25 at 19:01
|
show 5 more comments
2 Answers
2
active
oldest
votes
Try this:
SELECT
aggregate_power_demands.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as pdx
ON aggregate_power_demands.time = pdx.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as sea
ON aggregate_power_demands.time = sea.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as boi
ON aggregate_power_demands.time = boi.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as geg
ON aggregate_power_demands.time = geg.time;
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
which column you have null value @JohnKarasev ?pdx.temperature
orsea.temperature
orboi.temperature
orgeg.temperature
?
– tcadidot0
Mar 27 at 1:15
@JohnKarasev ,GROUP BY
is to make sure that you only want one result per eachlocation_name
andtime
. You can remove it if you want but only if eachlocation_name
have differenttime
and no values where onelocation_name
have twotime
value that is similar.
– tcadidot0
Mar 27 at 1:20
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
|
show 1 more comment
You can also try this. it has only one join:
SELECT
agd.time
, agd.demand
, MAX( IF( w.location_name = 'PDX', w.temperature, NULL)) as pdx_temperature
, MAX( IF( w.location_name = 'SEA', w.temperature, NULL)) as sea_temperature
, MAX( IF( w.location_name = 'BOI', w.temperature, NULL)) as boi_temperature
, MAX( IF( w.location_name = 'GEG', w.temperature, NULL)) as geg_temperature
FROM capstone.aggregate_power_demands agd
LEFT JOIN capstone.weather w ON agd.time = w.time
WHERE w.location_name IN ( 'PDX' , 'SEA' , 'BOI' , 'GEG' )
AND agd.time BETWEEN '1993-01-01 00:00:00' AND '2018-12-31 23:59:59'
GROUP BY w.location_name;
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
andMAX
must haveGROUP BY
.
– tcadidot0
Mar 27 at 2:46
1
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344483%2finner-join-is-very-slow-how-can-i-optimize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
SELECT
aggregate_power_demands.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as pdx
ON aggregate_power_demands.time = pdx.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as sea
ON aggregate_power_demands.time = sea.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as boi
ON aggregate_power_demands.time = boi.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as geg
ON aggregate_power_demands.time = geg.time;
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
which column you have null value @JohnKarasev ?pdx.temperature
orsea.temperature
orboi.temperature
orgeg.temperature
?
– tcadidot0
Mar 27 at 1:15
@JohnKarasev ,GROUP BY
is to make sure that you only want one result per eachlocation_name
andtime
. You can remove it if you want but only if eachlocation_name
have differenttime
and no values where onelocation_name
have twotime
value that is similar.
– tcadidot0
Mar 27 at 1:20
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
|
show 1 more comment
Try this:
SELECT
aggregate_power_demands.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as pdx
ON aggregate_power_demands.time = pdx.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as sea
ON aggregate_power_demands.time = sea.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as boi
ON aggregate_power_demands.time = boi.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as geg
ON aggregate_power_demands.time = geg.time;
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
which column you have null value @JohnKarasev ?pdx.temperature
orsea.temperature
orboi.temperature
orgeg.temperature
?
– tcadidot0
Mar 27 at 1:15
@JohnKarasev ,GROUP BY
is to make sure that you only want one result per eachlocation_name
andtime
. You can remove it if you want but only if eachlocation_name
have differenttime
and no values where onelocation_name
have twotime
value that is similar.
– tcadidot0
Mar 27 at 1:20
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
|
show 1 more comment
Try this:
SELECT
aggregate_power_demands.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as pdx
ON aggregate_power_demands.time = pdx.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as sea
ON aggregate_power_demands.time = sea.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as boi
ON aggregate_power_demands.time = boi.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as geg
ON aggregate_power_demands.time = geg.time;
Try this:
SELECT
aggregate_power_demands.time,
demand,
pdx.temperature,
sea.temperature,
boi.temperature,
geg.temperature
FROM capstone.aggregate_power_demands
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='PDX' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as pdx
ON aggregate_power_demands.time = pdx.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='SEA' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as sea
ON aggregate_power_demands.time = sea.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='BOI' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as boi
ON aggregate_power_demands.time = boi.time
LEFT JOIN
(SELECT time, temperature
FROM capstone.weather
WHERE location_name='GEG' AND YEAR(time) BETWEEN '1993' AND '2018'
GROUP BY location_name,time) as geg
ON aggregate_power_demands.time = geg.time;
answered Mar 26 at 2:25
tcadidot0tcadidot0
9901 gold badge2 silver badges14 bronze badges
9901 gold badge2 silver badges14 bronze badges
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
which column you have null value @JohnKarasev ?pdx.temperature
orsea.temperature
orboi.temperature
orgeg.temperature
?
– tcadidot0
Mar 27 at 1:15
@JohnKarasev ,GROUP BY
is to make sure that you only want one result per eachlocation_name
andtime
. You can remove it if you want but only if eachlocation_name
have differenttime
and no values where onelocation_name
have twotime
value that is similar.
– tcadidot0
Mar 27 at 1:20
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
|
show 1 more comment
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
which column you have null value @JohnKarasev ?pdx.temperature
orsea.temperature
orboi.temperature
orgeg.temperature
?
– tcadidot0
Mar 27 at 1:15
@JohnKarasev ,GROUP BY
is to make sure that you only want one result per eachlocation_name
andtime
. You can remove it if you want but only if eachlocation_name
have differenttime
and no values where onelocation_name
have twotime
value that is similar.
– tcadidot0
Mar 27 at 1:20
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
I got some null values in the temperatures, is there a way to replace each null with the average in that location?
– John Karasev
Mar 26 at 22:56
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
Also, what does the GROUP BY do?
– John Karasev
Mar 26 at 23:13
which column you have null value @JohnKarasev ?
pdx.temperature
or sea.temperature
or boi.temperature
or geg.temperature
?– tcadidot0
Mar 27 at 1:15
which column you have null value @JohnKarasev ?
pdx.temperature
or sea.temperature
or boi.temperature
or geg.temperature
?– tcadidot0
Mar 27 at 1:15
@JohnKarasev ,
GROUP BY
is to make sure that you only want one result per each location_name
and time
. You can remove it if you want but only if each location_name
have different time
and no values where one location_name
have two time
value that is similar.– tcadidot0
Mar 27 at 1:20
@JohnKarasev ,
GROUP BY
is to make sure that you only want one result per each location_name
and time
. You can remove it if you want but only if each location_name
have different time
and no values where one location_name
have two time
value that is similar.– tcadidot0
Mar 27 at 1:20
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
boi.temperature has null,
– John Karasev
Mar 27 at 5:06
|
show 1 more comment
You can also try this. it has only one join:
SELECT
agd.time
, agd.demand
, MAX( IF( w.location_name = 'PDX', w.temperature, NULL)) as pdx_temperature
, MAX( IF( w.location_name = 'SEA', w.temperature, NULL)) as sea_temperature
, MAX( IF( w.location_name = 'BOI', w.temperature, NULL)) as boi_temperature
, MAX( IF( w.location_name = 'GEG', w.temperature, NULL)) as geg_temperature
FROM capstone.aggregate_power_demands agd
LEFT JOIN capstone.weather w ON agd.time = w.time
WHERE w.location_name IN ( 'PDX' , 'SEA' , 'BOI' , 'GEG' )
AND agd.time BETWEEN '1993-01-01 00:00:00' AND '2018-12-31 23:59:59'
GROUP BY w.location_name;
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
andMAX
must haveGROUP BY
.
– tcadidot0
Mar 27 at 2:46
1
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
add a comment |
You can also try this. it has only one join:
SELECT
agd.time
, agd.demand
, MAX( IF( w.location_name = 'PDX', w.temperature, NULL)) as pdx_temperature
, MAX( IF( w.location_name = 'SEA', w.temperature, NULL)) as sea_temperature
, MAX( IF( w.location_name = 'BOI', w.temperature, NULL)) as boi_temperature
, MAX( IF( w.location_name = 'GEG', w.temperature, NULL)) as geg_temperature
FROM capstone.aggregate_power_demands agd
LEFT JOIN capstone.weather w ON agd.time = w.time
WHERE w.location_name IN ( 'PDX' , 'SEA' , 'BOI' , 'GEG' )
AND agd.time BETWEEN '1993-01-01 00:00:00' AND '2018-12-31 23:59:59'
GROUP BY w.location_name;
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
andMAX
must haveGROUP BY
.
– tcadidot0
Mar 27 at 2:46
1
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
add a comment |
You can also try this. it has only one join:
SELECT
agd.time
, agd.demand
, MAX( IF( w.location_name = 'PDX', w.temperature, NULL)) as pdx_temperature
, MAX( IF( w.location_name = 'SEA', w.temperature, NULL)) as sea_temperature
, MAX( IF( w.location_name = 'BOI', w.temperature, NULL)) as boi_temperature
, MAX( IF( w.location_name = 'GEG', w.temperature, NULL)) as geg_temperature
FROM capstone.aggregate_power_demands agd
LEFT JOIN capstone.weather w ON agd.time = w.time
WHERE w.location_name IN ( 'PDX' , 'SEA' , 'BOI' , 'GEG' )
AND agd.time BETWEEN '1993-01-01 00:00:00' AND '2018-12-31 23:59:59'
GROUP BY w.location_name;
You can also try this. it has only one join:
SELECT
agd.time
, agd.demand
, MAX( IF( w.location_name = 'PDX', w.temperature, NULL)) as pdx_temperature
, MAX( IF( w.location_name = 'SEA', w.temperature, NULL)) as sea_temperature
, MAX( IF( w.location_name = 'BOI', w.temperature, NULL)) as boi_temperature
, MAX( IF( w.location_name = 'GEG', w.temperature, NULL)) as geg_temperature
FROM capstone.aggregate_power_demands agd
LEFT JOIN capstone.weather w ON agd.time = w.time
WHERE w.location_name IN ( 'PDX' , 'SEA' , 'BOI' , 'GEG' )
AND agd.time BETWEEN '1993-01-01 00:00:00' AND '2018-12-31 23:59:59'
GROUP BY w.location_name;
edited Mar 27 at 5:33
answered Mar 26 at 18:01
Bernd BuffenBernd Buffen
10.6k2 gold badges15 silver badges24 bronze badges
10.6k2 gold badges15 silver badges24 bronze badges
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
andMAX
must haveGROUP BY
.
– tcadidot0
Mar 27 at 2:46
1
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
add a comment |
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
andMAX
must haveGROUP BY
.
– tcadidot0
Mar 27 at 2:46
1
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
missing a closing parenthesis
– John Karasev
Mar 26 at 22:58
and
MAX
must have GROUP BY
.– tcadidot0
Mar 27 at 2:46
and
MAX
must have GROUP BY
.– tcadidot0
Mar 27 at 2:46
1
1
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
I have add change the answer to correct the errors. Thx @John Karasev and @ tcadidot0
– Bernd Buffen
Mar 27 at 5:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344483%2finner-join-is-very-slow-how-can-i-optimize%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is type of a time column on which you are making join? Do you have indexes in all tables on this field?
– Pustovalov Dmitry
Mar 25 at 18:43
it is a DATETIME, not sure what you mean by indexes
– John Karasev
Mar 25 at 18:45
no I dont have indexes
– John Karasev
Mar 25 at 18:46
which MySQL / MariaDB version you are using
– Bernd Buffen
Mar 25 at 18:51
/usr/local/mysql/bin/mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
– John Karasev
Mar 25 at 19:01