Extremely slow query- Using google sql cloud











up vote
1
down vote

favorite












Is there a way I can speed this up? Right now it's taking an unbelievably insane amount of time to query.



SELECT trades.*, trader1.user_name as trader1_name,
trader2.user_name as trader2_name FROM trades
LEFT JOIN logs_players trader1 ON trader1.user_id = trader1_account_id
LEFT JOIN logs_players trader2 ON trader2.user_id = trader2_account_id
ORDER BY time_added
LIMIT 20 OFFSET 0;


I've done as much as I could in terms of searching online for a solution. Or even just trying to get some more information why it's taking so long to execute.





The query takes about 45 seconds or so to complete.



Create statements:



CREATE TABLE `trades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trader1_account_id` int(11) DEFAULT NULL,
`trader2_account_id` int(11) DEFAULT NULL,
`trader1_value` bigint(20) DEFAULT NULL,
`trader2_value` bigint(20) DEFAULT NULL,
`trader1_ip` varchar(16) DEFAULT NULL,
`trader2_ip` varchar(16) DEFAULT NULL,
`world` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`trader1_user` varchar(12) DEFAULT NULL,
`trader2_user` varchar(12) DEFAULT NULL,
`time_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8


CREATE TABLE `logs_players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(20) DEFAULT NULL,
`world_stage` varchar(20) DEFAULT NULL,
`world_type` varchar(20) DEFAULT NULL,
`bank` longtext,
`inventory` longtext,
`equipment` longtext,
`total_wealth` mediumtext,
`total_play_time` mediumtext,
`rights` int(11) DEFAULT NULL,
`icon` int(11) DEFAULT NULL,
`ironmode` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`last_ip` varchar(16) DEFAULT NULL,
`last_online` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`muted_until` timestamp NULL DEFAULT NULL,
`banned_until` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8









share|improve this question
























  • Show your create statements on trades, and logs_players please.
    – FrankerZ
    Nov 11 at 0:49










  • @FrankerZ edited the post with it. Thanks for the reply man.
    – user9097072
    Nov 11 at 1:11















up vote
1
down vote

favorite












Is there a way I can speed this up? Right now it's taking an unbelievably insane amount of time to query.



SELECT trades.*, trader1.user_name as trader1_name,
trader2.user_name as trader2_name FROM trades
LEFT JOIN logs_players trader1 ON trader1.user_id = trader1_account_id
LEFT JOIN logs_players trader2 ON trader2.user_id = trader2_account_id
ORDER BY time_added
LIMIT 20 OFFSET 0;


I've done as much as I could in terms of searching online for a solution. Or even just trying to get some more information why it's taking so long to execute.





The query takes about 45 seconds or so to complete.



Create statements:



CREATE TABLE `trades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trader1_account_id` int(11) DEFAULT NULL,
`trader2_account_id` int(11) DEFAULT NULL,
`trader1_value` bigint(20) DEFAULT NULL,
`trader2_value` bigint(20) DEFAULT NULL,
`trader1_ip` varchar(16) DEFAULT NULL,
`trader2_ip` varchar(16) DEFAULT NULL,
`world` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`trader1_user` varchar(12) DEFAULT NULL,
`trader2_user` varchar(12) DEFAULT NULL,
`time_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8


CREATE TABLE `logs_players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(20) DEFAULT NULL,
`world_stage` varchar(20) DEFAULT NULL,
`world_type` varchar(20) DEFAULT NULL,
`bank` longtext,
`inventory` longtext,
`equipment` longtext,
`total_wealth` mediumtext,
`total_play_time` mediumtext,
`rights` int(11) DEFAULT NULL,
`icon` int(11) DEFAULT NULL,
`ironmode` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`last_ip` varchar(16) DEFAULT NULL,
`last_online` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`muted_until` timestamp NULL DEFAULT NULL,
`banned_until` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8









share|improve this question
























  • Show your create statements on trades, and logs_players please.
    – FrankerZ
    Nov 11 at 0:49










  • @FrankerZ edited the post with it. Thanks for the reply man.
    – user9097072
    Nov 11 at 1:11













up vote
1
down vote

favorite









up vote
1
down vote

favorite











Is there a way I can speed this up? Right now it's taking an unbelievably insane amount of time to query.



SELECT trades.*, trader1.user_name as trader1_name,
trader2.user_name as trader2_name FROM trades
LEFT JOIN logs_players trader1 ON trader1.user_id = trader1_account_id
LEFT JOIN logs_players trader2 ON trader2.user_id = trader2_account_id
ORDER BY time_added
LIMIT 20 OFFSET 0;


I've done as much as I could in terms of searching online for a solution. Or even just trying to get some more information why it's taking so long to execute.





The query takes about 45 seconds or so to complete.



Create statements:



CREATE TABLE `trades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trader1_account_id` int(11) DEFAULT NULL,
`trader2_account_id` int(11) DEFAULT NULL,
`trader1_value` bigint(20) DEFAULT NULL,
`trader2_value` bigint(20) DEFAULT NULL,
`trader1_ip` varchar(16) DEFAULT NULL,
`trader2_ip` varchar(16) DEFAULT NULL,
`world` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`trader1_user` varchar(12) DEFAULT NULL,
`trader2_user` varchar(12) DEFAULT NULL,
`time_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8


CREATE TABLE `logs_players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(20) DEFAULT NULL,
`world_stage` varchar(20) DEFAULT NULL,
`world_type` varchar(20) DEFAULT NULL,
`bank` longtext,
`inventory` longtext,
`equipment` longtext,
`total_wealth` mediumtext,
`total_play_time` mediumtext,
`rights` int(11) DEFAULT NULL,
`icon` int(11) DEFAULT NULL,
`ironmode` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`last_ip` varchar(16) DEFAULT NULL,
`last_online` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`muted_until` timestamp NULL DEFAULT NULL,
`banned_until` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8









share|improve this question















Is there a way I can speed this up? Right now it's taking an unbelievably insane amount of time to query.



SELECT trades.*, trader1.user_name as trader1_name,
trader2.user_name as trader2_name FROM trades
LEFT JOIN logs_players trader1 ON trader1.user_id = trader1_account_id
LEFT JOIN logs_players trader2 ON trader2.user_id = trader2_account_id
ORDER BY time_added
LIMIT 20 OFFSET 0;


I've done as much as I could in terms of searching online for a solution. Or even just trying to get some more information why it's taking so long to execute.





The query takes about 45 seconds or so to complete.



Create statements:



CREATE TABLE `trades` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`trader1_account_id` int(11) DEFAULT NULL,
`trader2_account_id` int(11) DEFAULT NULL,
`trader1_value` bigint(20) DEFAULT NULL,
`trader2_value` bigint(20) DEFAULT NULL,
`trader1_ip` varchar(16) DEFAULT NULL,
`trader2_ip` varchar(16) DEFAULT NULL,
`world` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`trader1_user` varchar(12) DEFAULT NULL,
`trader2_user` varchar(12) DEFAULT NULL,
`time_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8


CREATE TABLE `logs_players` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`user_name` varchar(20) DEFAULT NULL,
`world_stage` varchar(20) DEFAULT NULL,
`world_type` varchar(20) DEFAULT NULL,
`bank` longtext,
`inventory` longtext,
`equipment` longtext,
`total_wealth` mediumtext,
`total_play_time` mediumtext,
`rights` int(11) DEFAULT NULL,
`icon` int(11) DEFAULT NULL,
`ironmode` int(11) DEFAULT NULL,
`x` int(11) DEFAULT NULL,
`z` int(11) DEFAULT NULL,
`level` int(11) DEFAULT NULL,
`last_ip` varchar(16) DEFAULT NULL,
`last_online` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`muted_until` timestamp NULL DEFAULT NULL,
`banned_until` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8






mysql spring google-cloud-platform






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 2:30









FrankerZ

15.4k72859




15.4k72859










asked Nov 11 at 0:26









user9097072

83




83












  • Show your create statements on trades, and logs_players please.
    – FrankerZ
    Nov 11 at 0:49










  • @FrankerZ edited the post with it. Thanks for the reply man.
    – user9097072
    Nov 11 at 1:11


















  • Show your create statements on trades, and logs_players please.
    – FrankerZ
    Nov 11 at 0:49










  • @FrankerZ edited the post with it. Thanks for the reply man.
    – user9097072
    Nov 11 at 1:11
















Show your create statements on trades, and logs_players please.
– FrankerZ
Nov 11 at 0:49




Show your create statements on trades, and logs_players please.
– FrankerZ
Nov 11 at 0:49












@FrankerZ edited the post with it. Thanks for the reply man.
– user9097072
Nov 11 at 1:11




@FrankerZ edited the post with it. Thanks for the reply man.
– user9097072
Nov 11 at 1:11












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I filled a sample database with 10k rows each, and found that a few indexes were what you needed:



ALTER TABLE `logs_players` ADD INDEX(`user_id`);
ALTER TABLE `trades` ADD INDEX(`time_added`);


The main index we need is an index on user_id. Changing that we went from a query time of 20.1390 seconds, to 0.0130 seconds:





We can even get that down further, by adding an index on time_added to make sorting a lot faster, now we ended up with an impressive query time:





Do some research on indexes! A simple EXPLAIN query would show you that you're using filesort (Which is rather bad!):



After indexes, this looks a lot better:






share|improve this answer





















  • Dude you're a legend. Thanks so much for the help. Gonna implement this now.
    – user9097072
    Nov 11 at 2:38










  • @user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
    – FrankerZ
    Nov 11 at 2:41











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244744%2fextremely-slow-query-using-google-sql-cloud%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I filled a sample database with 10k rows each, and found that a few indexes were what you needed:



ALTER TABLE `logs_players` ADD INDEX(`user_id`);
ALTER TABLE `trades` ADD INDEX(`time_added`);


The main index we need is an index on user_id. Changing that we went from a query time of 20.1390 seconds, to 0.0130 seconds:





We can even get that down further, by adding an index on time_added to make sorting a lot faster, now we ended up with an impressive query time:





Do some research on indexes! A simple EXPLAIN query would show you that you're using filesort (Which is rather bad!):



After indexes, this looks a lot better:






share|improve this answer





















  • Dude you're a legend. Thanks so much for the help. Gonna implement this now.
    – user9097072
    Nov 11 at 2:38










  • @user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
    – FrankerZ
    Nov 11 at 2:41















up vote
0
down vote



accepted










I filled a sample database with 10k rows each, and found that a few indexes were what you needed:



ALTER TABLE `logs_players` ADD INDEX(`user_id`);
ALTER TABLE `trades` ADD INDEX(`time_added`);


The main index we need is an index on user_id. Changing that we went from a query time of 20.1390 seconds, to 0.0130 seconds:





We can even get that down further, by adding an index on time_added to make sorting a lot faster, now we ended up with an impressive query time:





Do some research on indexes! A simple EXPLAIN query would show you that you're using filesort (Which is rather bad!):



After indexes, this looks a lot better:






share|improve this answer





















  • Dude you're a legend. Thanks so much for the help. Gonna implement this now.
    – user9097072
    Nov 11 at 2:38










  • @user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
    – FrankerZ
    Nov 11 at 2:41













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I filled a sample database with 10k rows each, and found that a few indexes were what you needed:



ALTER TABLE `logs_players` ADD INDEX(`user_id`);
ALTER TABLE `trades` ADD INDEX(`time_added`);


The main index we need is an index on user_id. Changing that we went from a query time of 20.1390 seconds, to 0.0130 seconds:





We can even get that down further, by adding an index on time_added to make sorting a lot faster, now we ended up with an impressive query time:





Do some research on indexes! A simple EXPLAIN query would show you that you're using filesort (Which is rather bad!):



After indexes, this looks a lot better:






share|improve this answer












I filled a sample database with 10k rows each, and found that a few indexes were what you needed:



ALTER TABLE `logs_players` ADD INDEX(`user_id`);
ALTER TABLE `trades` ADD INDEX(`time_added`);


The main index we need is an index on user_id. Changing that we went from a query time of 20.1390 seconds, to 0.0130 seconds:





We can even get that down further, by adding an index on time_added to make sorting a lot faster, now we ended up with an impressive query time:





Do some research on indexes! A simple EXPLAIN query would show you that you're using filesort (Which is rather bad!):



After indexes, this looks a lot better:







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 2:25









FrankerZ

15.4k72859




15.4k72859












  • Dude you're a legend. Thanks so much for the help. Gonna implement this now.
    – user9097072
    Nov 11 at 2:38










  • @user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
    – FrankerZ
    Nov 11 at 2:41


















  • Dude you're a legend. Thanks so much for the help. Gonna implement this now.
    – user9097072
    Nov 11 at 2:38










  • @user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
    – FrankerZ
    Nov 11 at 2:41
















Dude you're a legend. Thanks so much for the help. Gonna implement this now.
– user9097072
Nov 11 at 2:38




Dude you're a legend. Thanks so much for the help. Gonna implement this now.
– user9097072
Nov 11 at 2:38












@user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
– FrankerZ
Nov 11 at 2:41




@user9097072 If you're into making this the best possible, I highly recommend looking into foreign key indexes, which ensure data integrity when rows get deleted or updated.
– FrankerZ
Nov 11 at 2:41


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244744%2fextremely-slow-query-using-google-sql-cloud%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python