This simple mysql query is taking 2-3 secondsUnexplained InnoDB timeoutsAre two indexes needed?Identical...
How do researchers send unsolicited emails asking for feedback on their works?
Determine voltage drop over 10G resistors with cheap multimeter
Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?
Exit shell with shortcut (not typing exit) that closes session properly
How to test the sharpness of a knife?
What is it called when someone votes for an option that's not their first choice?
PTIJ: Where did Achashverosh's years wander off to?
is this saw blade faulty?
Was World War I a war of liberals against authoritarians?
UK Tourist Visa- Enquiry
Why didn’t Eve recognize the little cockroach as a living organism?
Is VPN a layer 3 concept?
How old is Nick Fury?
Turning a hard to access nut?
What (if any) is the reason to buy in small local stores?
Do I need to convey a moral for each of my blog post?
What is the tangent at a sharp point on a curve?
How can a new country break out from a developed country without war?
Do native speakers use "ultima" and "proxima" frequently in spoken English?
Knife as defense against stray dogs
How do you justify more code being written by following clean code practices?
How to balance a monster modification (zombie)?
Writing in a Christian voice
What kind of footwear is suitable for walking in micro gravity environment?
This simple mysql query is taking 2-3 seconds
Unexplained InnoDB timeoutsAre two indexes needed?Identical query, tables, but different EXPLAIN and performanceWhy does IN (subquery) perform bad when = (subquery) is blazing fast?Deciding which MySQL execution plan is betterOptimizing a simple query on a large tableNeed help improving sql query performanceMySQL query taking too longselect MAX() from MySQL view (2x INNER JOIN) is slowPerformance of mysql equi-join observed in HDD and SSD
I'm using sqlalchemy and trying to do a query on an m2m table. I have a tag and want find all the events that match that tag:
SELECT * FROM events
WHERE EXISTS ( SELECT 1 FROM events_tags, tags
WHERE events.id = events_tags.event_id
AND tags.id = events_tags.tag_id
AND tags.id = 617)
LIMIT 50;
This is taking 2-3 seconds on my web server. On my laptop, it's faster(though my laptop is more powerful and has fewer rows.) The web server has about 300,000 rows in this table.
Here is an explain:
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| 1 | PRIMARY | events | ALL | NULL | NULL | NULL | NULL | 310172 | Using where |
| 2 | DEPENDENT SUBQUERY | tags | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
| 2 | DEPENDENT SUBQUERY | events_tags | eq_ref | PRIMARY,tag_id | PRIMARY | 16 | timeline.events.id,const | 1 | Using index |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
I have a feeling this is simple, but my google-fu is failing me.
Create Table Syntax:
| events | CREATE TABLE `events` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`context` varchar(255) DEFAULT NULL,
`text` mediumtext,
`wikidata_id` bigint(20) DEFAULT NULL,
`start_day` smallint(6) DEFAULT NULL,
`start_month` smallint(6) DEFAULT NULL,
`start_year` bigint(20) DEFAULT NULL,
`end_day` smallint(6) DEFAULT NULL,
`end_month` smallint(6) DEFAULT NULL,
`end_year` bigint(20) DEFAULT NULL,
`is_number` tinyint(1) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`number` bigint(20) DEFAULT NULL,
`start_name` varchar(255) DEFAULT NULL,
`end_name` varchar(255) DEFAULT NULL,
`subject_title` varchar(255) DEFAULT NULL,
`object_title` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=327132 DEFAULT CHARSET=utf8 |
edited: my bad, it was missing the "from events" I guess I'm wondering where I would want an index, I would think on the "id" column, which should have an index.
mysql query-performance
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
|
show 2 more comments
I'm using sqlalchemy and trying to do a query on an m2m table. I have a tag and want find all the events that match that tag:
SELECT * FROM events
WHERE EXISTS ( SELECT 1 FROM events_tags, tags
WHERE events.id = events_tags.event_id
AND tags.id = events_tags.tag_id
AND tags.id = 617)
LIMIT 50;
This is taking 2-3 seconds on my web server. On my laptop, it's faster(though my laptop is more powerful and has fewer rows.) The web server has about 300,000 rows in this table.
Here is an explain:
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| 1 | PRIMARY | events | ALL | NULL | NULL | NULL | NULL | 310172 | Using where |
| 2 | DEPENDENT SUBQUERY | tags | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
| 2 | DEPENDENT SUBQUERY | events_tags | eq_ref | PRIMARY,tag_id | PRIMARY | 16 | timeline.events.id,const | 1 | Using index |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
I have a feeling this is simple, but my google-fu is failing me.
Create Table Syntax:
| events | CREATE TABLE `events` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`context` varchar(255) DEFAULT NULL,
`text` mediumtext,
`wikidata_id` bigint(20) DEFAULT NULL,
`start_day` smallint(6) DEFAULT NULL,
`start_month` smallint(6) DEFAULT NULL,
`start_year` bigint(20) DEFAULT NULL,
`end_day` smallint(6) DEFAULT NULL,
`end_month` smallint(6) DEFAULT NULL,
`end_year` bigint(20) DEFAULT NULL,
`is_number` tinyint(1) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`number` bigint(20) DEFAULT NULL,
`start_name` varchar(255) DEFAULT NULL,
`end_name` varchar(255) DEFAULT NULL,
`subject_title` varchar(255) DEFAULT NULL,
`object_title` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=327132 DEFAULT CHARSET=utf8 |
edited: my bad, it was missing the "from events" I guess I'm wondering where I would want an index, I would think on the "id" column, which should have an index.
mysql query-performance
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
please edit the query for proper - this query will never work, and good to add table structure for all tables from query
– a_vlad
Nov 29 '16 at 5:12
I guess the query you have mentioned has some syntax errors, Please Check, also Join might help to make query faster try them
– Priyanka Kariya
Nov 29 '16 at 5:58
1
JOIN plus missed index I think, for this need to check table structure
– a_vlad
Nov 29 '16 at 6:50
As @a_vlad said, edit your question with the real query, along with create table statements for the tables.
– Philᵀᴹ
Nov 29 '16 at 6:59
Add an index on(tag_id, event_id)
and remove the join totags
from the subquery completely.
– ypercubeᵀᴹ
Nov 29 '16 at 16:08
|
show 2 more comments
I'm using sqlalchemy and trying to do a query on an m2m table. I have a tag and want find all the events that match that tag:
SELECT * FROM events
WHERE EXISTS ( SELECT 1 FROM events_tags, tags
WHERE events.id = events_tags.event_id
AND tags.id = events_tags.tag_id
AND tags.id = 617)
LIMIT 50;
This is taking 2-3 seconds on my web server. On my laptop, it's faster(though my laptop is more powerful and has fewer rows.) The web server has about 300,000 rows in this table.
Here is an explain:
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| 1 | PRIMARY | events | ALL | NULL | NULL | NULL | NULL | 310172 | Using where |
| 2 | DEPENDENT SUBQUERY | tags | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
| 2 | DEPENDENT SUBQUERY | events_tags | eq_ref | PRIMARY,tag_id | PRIMARY | 16 | timeline.events.id,const | 1 | Using index |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
I have a feeling this is simple, but my google-fu is failing me.
Create Table Syntax:
| events | CREATE TABLE `events` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`context` varchar(255) DEFAULT NULL,
`text` mediumtext,
`wikidata_id` bigint(20) DEFAULT NULL,
`start_day` smallint(6) DEFAULT NULL,
`start_month` smallint(6) DEFAULT NULL,
`start_year` bigint(20) DEFAULT NULL,
`end_day` smallint(6) DEFAULT NULL,
`end_month` smallint(6) DEFAULT NULL,
`end_year` bigint(20) DEFAULT NULL,
`is_number` tinyint(1) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`number` bigint(20) DEFAULT NULL,
`start_name` varchar(255) DEFAULT NULL,
`end_name` varchar(255) DEFAULT NULL,
`subject_title` varchar(255) DEFAULT NULL,
`object_title` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=327132 DEFAULT CHARSET=utf8 |
edited: my bad, it was missing the "from events" I guess I'm wondering where I would want an index, I would think on the "id" column, which should have an index.
mysql query-performance
I'm using sqlalchemy and trying to do a query on an m2m table. I have a tag and want find all the events that match that tag:
SELECT * FROM events
WHERE EXISTS ( SELECT 1 FROM events_tags, tags
WHERE events.id = events_tags.event_id
AND tags.id = events_tags.tag_id
AND tags.id = 617)
LIMIT 50;
This is taking 2-3 seconds on my web server. On my laptop, it's faster(though my laptop is more powerful and has fewer rows.) The web server has about 300,000 rows in this table.
Here is an explain:
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
| 1 | PRIMARY | events | ALL | NULL | NULL | NULL | NULL | 310172 | Using where |
| 2 | DEPENDENT SUBQUERY | tags | const | PRIMARY | PRIMARY | 8 | const | 1 | Using index |
| 2 | DEPENDENT SUBQUERY | events_tags | eq_ref | PRIMARY,tag_id | PRIMARY | 16 | timeline.events.id,const | 1 | Using index |
+------+--------------------+-------------+--------+----------------+---------+---------+--------------------------+--------+-------------+
I have a feeling this is simple, but my google-fu is failing me.
Create Table Syntax:
| events | CREATE TABLE `events` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
`context` varchar(255) DEFAULT NULL,
`text` mediumtext,
`wikidata_id` bigint(20) DEFAULT NULL,
`start_day` smallint(6) DEFAULT NULL,
`start_month` smallint(6) DEFAULT NULL,
`start_year` bigint(20) DEFAULT NULL,
`end_day` smallint(6) DEFAULT NULL,
`end_month` smallint(6) DEFAULT NULL,
`end_year` bigint(20) DEFAULT NULL,
`is_number` tinyint(1) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
`number` bigint(20) DEFAULT NULL,
`start_name` varchar(255) DEFAULT NULL,
`end_name` varchar(255) DEFAULT NULL,
`subject_title` varchar(255) DEFAULT NULL,
`object_title` varchar(255) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=327132 DEFAULT CHARSET=utf8 |
edited: my bad, it was missing the "from events" I guess I'm wondering where I would want an index, I would think on the "id" column, which should have an index.
mysql query-performance
mysql query-performance
edited Nov 29 '16 at 16:11
paparazzo
4,6621230
4,6621230
asked Nov 29 '16 at 5:07
mikkergpmikkergp
11
11
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 8 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
please edit the query for proper - this query will never work, and good to add table structure for all tables from query
– a_vlad
Nov 29 '16 at 5:12
I guess the query you have mentioned has some syntax errors, Please Check, also Join might help to make query faster try them
– Priyanka Kariya
Nov 29 '16 at 5:58
1
JOIN plus missed index I think, for this need to check table structure
– a_vlad
Nov 29 '16 at 6:50
As @a_vlad said, edit your question with the real query, along with create table statements for the tables.
– Philᵀᴹ
Nov 29 '16 at 6:59
Add an index on(tag_id, event_id)
and remove the join totags
from the subquery completely.
– ypercubeᵀᴹ
Nov 29 '16 at 16:08
|
show 2 more comments
please edit the query for proper - this query will never work, and good to add table structure for all tables from query
– a_vlad
Nov 29 '16 at 5:12
I guess the query you have mentioned has some syntax errors, Please Check, also Join might help to make query faster try them
– Priyanka Kariya
Nov 29 '16 at 5:58
1
JOIN plus missed index I think, for this need to check table structure
– a_vlad
Nov 29 '16 at 6:50
As @a_vlad said, edit your question with the real query, along with create table statements for the tables.
– Philᵀᴹ
Nov 29 '16 at 6:59
Add an index on(tag_id, event_id)
and remove the join totags
from the subquery completely.
– ypercubeᵀᴹ
Nov 29 '16 at 16:08
please edit the query for proper - this query will never work, and good to add table structure for all tables from query
– a_vlad
Nov 29 '16 at 5:12
please edit the query for proper - this query will never work, and good to add table structure for all tables from query
– a_vlad
Nov 29 '16 at 5:12
I guess the query you have mentioned has some syntax errors, Please Check, also Join might help to make query faster try them
– Priyanka Kariya
Nov 29 '16 at 5:58
I guess the query you have mentioned has some syntax errors, Please Check, also Join might help to make query faster try them
– Priyanka Kariya
Nov 29 '16 at 5:58
1
1
JOIN plus missed index I think, for this need to check table structure
– a_vlad
Nov 29 '16 at 6:50
JOIN plus missed index I think, for this need to check table structure
– a_vlad
Nov 29 '16 at 6:50
As @a_vlad said, edit your question with the real query, along with create table statements for the tables.
– Philᵀᴹ
Nov 29 '16 at 6:59
As @a_vlad said, edit your question with the real query, along with create table statements for the tables.
– Philᵀᴹ
Nov 29 '16 at 6:59
Add an index on
(tag_id, event_id)
and remove the join to tags
from the subquery completely.– ypercubeᵀᴹ
Nov 29 '16 at 16:08
Add an index on
(tag_id, event_id)
and remove the join to tags
from the subquery completely.– ypercubeᵀᴹ
Nov 29 '16 at 16:08
|
show 2 more comments
2 Answers
2
active
oldest
votes
Lots of simplification and speedup:
SELECT e.*
FROM ( SELECT DISTINCT event_id
FROM events_tags
WHERE tag_id = 617 ) AS et
JOIN events AS e ON e.id = et.event_id
ORDER BY ??? -- else get random 50??
LIMIT 50;
events_tags
smells like a many:many mapping table. It can possible be improved by following these tips.
add a comment |
just as one more variant, for common case when need extract not only events, but for example tag name:
SELECT
events.*,
tags.tag_name
FROM
events FORCE INDEX (PRIMARY)
INNER JOIN events_tags ON events.id = events_tags.event_id
INNER JOIN tags ON tags.id = events_tags.tag_id
WHERE tags.id = 617
LIMIT 50;
In some cases MySQL do not want use index, so we little help him (if we sure we request small part of whole table):
FROM
events FORCE INDEX (PRIMARY)
and plan will be:
1 SIMPLE tags const PRIMARY PRIMARY 4 const 1 100.00
1 SIMPLE events_tags ref event_id,tag_id tag_id 5 const 3 100.00 Using where
1 SIMPLE events eq_ref PRIMARY PRIMARY 4 test_db.events_tags.event_id 1 100.00
indexes - must be, all comparable columns must be same type, such as events.id
bigint(20) == events_tags.event_id
bigint(20)
Yeah, this query is really weird. I ended up test out a different subquery:SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy
– mikkergp
Nov 30 '16 at 5:01
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdba.stackexchange.com%2fquestions%2f156618%2fthis-simple-mysql-query-is-taking-2-3-seconds%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
Lots of simplification and speedup:
SELECT e.*
FROM ( SELECT DISTINCT event_id
FROM events_tags
WHERE tag_id = 617 ) AS et
JOIN events AS e ON e.id = et.event_id
ORDER BY ??? -- else get random 50??
LIMIT 50;
events_tags
smells like a many:many mapping table. It can possible be improved by following these tips.
add a comment |
Lots of simplification and speedup:
SELECT e.*
FROM ( SELECT DISTINCT event_id
FROM events_tags
WHERE tag_id = 617 ) AS et
JOIN events AS e ON e.id = et.event_id
ORDER BY ??? -- else get random 50??
LIMIT 50;
events_tags
smells like a many:many mapping table. It can possible be improved by following these tips.
add a comment |
Lots of simplification and speedup:
SELECT e.*
FROM ( SELECT DISTINCT event_id
FROM events_tags
WHERE tag_id = 617 ) AS et
JOIN events AS e ON e.id = et.event_id
ORDER BY ??? -- else get random 50??
LIMIT 50;
events_tags
smells like a many:many mapping table. It can possible be improved by following these tips.
Lots of simplification and speedup:
SELECT e.*
FROM ( SELECT DISTINCT event_id
FROM events_tags
WHERE tag_id = 617 ) AS et
JOIN events AS e ON e.id = et.event_id
ORDER BY ??? -- else get random 50??
LIMIT 50;
events_tags
smells like a many:many mapping table. It can possible be improved by following these tips.
answered Nov 29 '16 at 18:30
Rick JamesRick James
43.6k22259
43.6k22259
add a comment |
add a comment |
just as one more variant, for common case when need extract not only events, but for example tag name:
SELECT
events.*,
tags.tag_name
FROM
events FORCE INDEX (PRIMARY)
INNER JOIN events_tags ON events.id = events_tags.event_id
INNER JOIN tags ON tags.id = events_tags.tag_id
WHERE tags.id = 617
LIMIT 50;
In some cases MySQL do not want use index, so we little help him (if we sure we request small part of whole table):
FROM
events FORCE INDEX (PRIMARY)
and plan will be:
1 SIMPLE tags const PRIMARY PRIMARY 4 const 1 100.00
1 SIMPLE events_tags ref event_id,tag_id tag_id 5 const 3 100.00 Using where
1 SIMPLE events eq_ref PRIMARY PRIMARY 4 test_db.events_tags.event_id 1 100.00
indexes - must be, all comparable columns must be same type, such as events.id
bigint(20) == events_tags.event_id
bigint(20)
Yeah, this query is really weird. I ended up test out a different subquery:SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy
– mikkergp
Nov 30 '16 at 5:01
add a comment |
just as one more variant, for common case when need extract not only events, but for example tag name:
SELECT
events.*,
tags.tag_name
FROM
events FORCE INDEX (PRIMARY)
INNER JOIN events_tags ON events.id = events_tags.event_id
INNER JOIN tags ON tags.id = events_tags.tag_id
WHERE tags.id = 617
LIMIT 50;
In some cases MySQL do not want use index, so we little help him (if we sure we request small part of whole table):
FROM
events FORCE INDEX (PRIMARY)
and plan will be:
1 SIMPLE tags const PRIMARY PRIMARY 4 const 1 100.00
1 SIMPLE events_tags ref event_id,tag_id tag_id 5 const 3 100.00 Using where
1 SIMPLE events eq_ref PRIMARY PRIMARY 4 test_db.events_tags.event_id 1 100.00
indexes - must be, all comparable columns must be same type, such as events.id
bigint(20) == events_tags.event_id
bigint(20)
Yeah, this query is really weird. I ended up test out a different subquery:SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy
– mikkergp
Nov 30 '16 at 5:01
add a comment |
just as one more variant, for common case when need extract not only events, but for example tag name:
SELECT
events.*,
tags.tag_name
FROM
events FORCE INDEX (PRIMARY)
INNER JOIN events_tags ON events.id = events_tags.event_id
INNER JOIN tags ON tags.id = events_tags.tag_id
WHERE tags.id = 617
LIMIT 50;
In some cases MySQL do not want use index, so we little help him (if we sure we request small part of whole table):
FROM
events FORCE INDEX (PRIMARY)
and plan will be:
1 SIMPLE tags const PRIMARY PRIMARY 4 const 1 100.00
1 SIMPLE events_tags ref event_id,tag_id tag_id 5 const 3 100.00 Using where
1 SIMPLE events eq_ref PRIMARY PRIMARY 4 test_db.events_tags.event_id 1 100.00
indexes - must be, all comparable columns must be same type, such as events.id
bigint(20) == events_tags.event_id
bigint(20)
just as one more variant, for common case when need extract not only events, but for example tag name:
SELECT
events.*,
tags.tag_name
FROM
events FORCE INDEX (PRIMARY)
INNER JOIN events_tags ON events.id = events_tags.event_id
INNER JOIN tags ON tags.id = events_tags.tag_id
WHERE tags.id = 617
LIMIT 50;
In some cases MySQL do not want use index, so we little help him (if we sure we request small part of whole table):
FROM
events FORCE INDEX (PRIMARY)
and plan will be:
1 SIMPLE tags const PRIMARY PRIMARY 4 const 1 100.00
1 SIMPLE events_tags ref event_id,tag_id tag_id 5 const 3 100.00 Using where
1 SIMPLE events eq_ref PRIMARY PRIMARY 4 test_db.events_tags.event_id 1 100.00
indexes - must be, all comparable columns must be same type, such as events.id
bigint(20) == events_tags.event_id
bigint(20)
answered Nov 30 '16 at 2:09
a_vlada_vlad
2,9821716
2,9821716
Yeah, this query is really weird. I ended up test out a different subquery:SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy
– mikkergp
Nov 30 '16 at 5:01
add a comment |
Yeah, this query is really weird. I ended up test out a different subquery:SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy
– mikkergp
Nov 30 '16 at 5:01
Yeah, this query is really weird. I ended up test out a different subquery:
SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy– mikkergp
Nov 30 '16 at 5:01
Yeah, this query is really weird. I ended up test out a different subquery:
SELECT * FROM events WHERE events.id IN (SELECT event_id FROM events_tags, tags WHERE events.id = events_tags.event_id AND tags.id = events_tags.tag_id AND tags.id = 617) LIMIT 50;
and that works in .02 seconds. The challenge here is I'm using SQLAlchemy. The query I included came from the SQLAlchemy query log. I guess the next step is either to use a manual query or see if I can file a bug report with SQLAlchemy– mikkergp
Nov 30 '16 at 5:01
add a comment |
Thanks for contributing an answer to Database Administrators Stack Exchange!
- 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%2fdba.stackexchange.com%2fquestions%2f156618%2fthis-simple-mysql-query-is-taking-2-3-seconds%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
please edit the query for proper - this query will never work, and good to add table structure for all tables from query
– a_vlad
Nov 29 '16 at 5:12
I guess the query you have mentioned has some syntax errors, Please Check, also Join might help to make query faster try them
– Priyanka Kariya
Nov 29 '16 at 5:58
1
JOIN plus missed index I think, for this need to check table structure
– a_vlad
Nov 29 '16 at 6:50
As @a_vlad said, edit your question with the real query, along with create table statements for the tables.
– Philᵀᴹ
Nov 29 '16 at 6:59
Add an index on
(tag_id, event_id)
and remove the join totags
from the subquery completely.– ypercubeᵀᴹ
Nov 29 '16 at 16:08