Comments on: 7 ways to convince MySQL to use the right index https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index Blog by Shlomi Noach Thu, 06 Dec 2018 03:28:00 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: Chun Yin Lo https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-545268 Thu, 06 Dec 2018 03:28:00 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-545268 Please rename `t` to `data`. It is confusing at the first place

]]>
By: Vinay Jaiswal https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-415818 Fri, 26 Jan 2018 07:28:00 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-415818 Facing issue Facing slow query on master even same query is running more faster on slave . need your inputs to resolve this issue .

Key Points :
Mysql version 5.6.33 on the both ( master & slave ) version are same .
cross validated show status o/p is same on both server .
But getting different execution plan on master & slave .
Slave is not using sorting & temporary while master is using for same query .
schema & index are same on both servers even also validated ordered .
Already run on three table analyse & optimise on master server but still planner is different than slave .

Query : explain extended select this_.solution_type as y0_, this_.lead_id as y1_, this_.created_at as y2_, this_.cust_id as y3_, this_.mobile_number as y4_, node2_.sub_stage as y5_, node2_.stage as y6_ from user_business_mapping this_ inner join workflow_status wfstatus1_ on this_.id=wfstatus1_.user_business_mapping_id inner join workflow_node node2_ on wfstatus1_.workflow_node_id=node2_.id where this_.created_at>=’2018-01-19′ and this_.created_at<='2018-01-25' and this_.solution_type='bc_merchant' and wfstatus1_.is_active=1 order by this_.id asc limit 5

Master server Execution Plan :

*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: node2_
type: index
possible_keys: PRIMARY
key: idx_wfn_stage_substage_is_active
key_len: 305
ref: NULL
rows: 43
filtered: 100.00
Extra: Using index; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: wfstatus1_
type: ref
possible_keys: FK_ws_rbsm,FK_wfs_wf_nodeId_idx
key: FK_wfs_wf_nodeId_idx
key_len: 9
ref: onboarding_engine.node2_.id
rows: 927
filtered: 100.00
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: this_
type: eq_ref
possible_keys: PRIMARY,created_at_idx
key: PRIMARY
key_len: 8
ref: onboarding_engine.wfstatus1_.user_business_mapping_id
rows: 1
filtered: 100.00
Extra: Using where
3 rows in set, 1 warning (0.00 sec)

Slave Server Execution plan :

*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: this_
type: index
possible_keys: PRIMARY,created_at_idx
key: PRIMARY
key_len: 8
ref: NULL
rows: 5
filtered: 4705840.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: wfstatus1_
type: ref
possible_keys: FK_ws_rbsm,FK_wfs_wf_nodeId_idx
key: FK_ws_rbsm
key_len: 8
ref: onboarding_engine.this_.id
rows: 4
filtered: 100.00
Extra: Using where
*************************** 3. row ***************************
id: 1
select_type: SIMPLE
table: node2_
type: eq_ref
possible_keys: PRIMARY
key: PRIMARY
key_len: 8
ref: onboarding_engine.wfstatus1_.workflow_node_id
rows: 1
filtered: 100.00
Extra: NULL
3 rows in set, 1 warning (0.00 sec)

]]>
By: İlter Öcal https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-388909 Wed, 26 Apr 2017 07:40:00 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-388909 Faced this issue where mysql uses the wrong index and resulting with a simple query executed in 5 seconds.

I am using php laravel5 framework whose database library (Eloquent ORM) and query builder does not allow to specify use index/force index in the queries. But it allows to write raw order by clauses so i just added order by id + 0 as in SOLUTION #6 to prevent it using the wrong index. Now my query executes in 0.002 seconds.

Thanks for this great analysis and clever solution coming from 2009!

]]>
By: Jonathon https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-378127 Wed, 09 Nov 2016 23:36:00 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-378127 Excellent analysis and still relevant with latest stable version of MySQL. Had similar issue with a new client. Sure enough, ordering by the primary key only was resulting in perfectly good indices not being used where they should be. Came across this following a similar process to the above.

]]>
By: Luke https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-339382 Wed, 16 Dec 2015 23:08:02 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-339382 Great read. Ran into same thought process with DOCTRINE and found you searching for ways NOT to USE or IGNORE indices.

Our solution in the end was to run one query to simply get the queries from the ordered query without any of the other fields in the select. Then run a WHERE IN query using those IDs.

]]>
By: Rick James https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-309410 Sat, 14 Feb 2015 05:14:23 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-309410 I would start by recommending INDEX(type, level) or INDEX(type, level, id). (With InnoDB, those are essentially identical.) That would also get you “Using index”, which is an additional performance boost.

]]>
By: SacTiw https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-243889 Fri, 06 Jun 2014 13:25:21 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-243889 Regarding solution:

SELECT id from (SELECT id FROM data
WHERE type=12345 AND level > 3)
ORDER BY id

This would work fast only if for type=12345 you just have few hundred row (say 3

I am not sure what would be the best solution for that but as of now these two comes to my mind:

SELECT id from data IGNORE INDEX (index_on_type_column)
WHERE type = 12345 AND level > 3
ORDER BY id LIMIT 1000;

SELECT id from data
WHERE id NOT IN (SELECT id FROM data
WHERE type != 12345) AND level > 3
ORDER BY id LIMIT 1000;

]]>
By: shlomi https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-241674 Fri, 23 May 2014 05:35:56 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-241674 @Lokesh,

thanks – first time I encounter this variable. Interesting.

]]>
By: Lokesh https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-241521 Thu, 22 May 2014 06:17:54 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-241521 May be you can also try setting the MySQL variable ‘SET max_seeks_for_key=100’ if you are sure your index will bring it down to 100 examinations…

]]>
By: Jayadev Swain https://shlomi-noach.github.io/blog/mysql/7-ways-to-convince-mysql-to-use-the-right-index/comment-page-1#comment-222300 Thu, 31 Oct 2013 13:39:12 +0000 https://shlomi-noach.github.io/blog/?p=695#comment-222300 Well documented article Shlomi!

What could have also helped is if you had mentioned the Exec Time for each of the Solutions#.

It would have helped with the comparisons. But nevertheless, this gives us techniques which can put to practice.

]]>