I have executed the below query but it takes 72sec to completed
SELECT ci.id, ci.city_real, co.country_name FROM cities ci
LEFT JOIN countries co ON(ci.country_id=co.country_id)
WHERE city_real in ('Delhi','Bangalore','Mumbai') ORDER BY population DESC
Then I have created indexes as per explain
stat plan but it is no different which means it takes 72sec.
Then I have changed the query using use index
SELECT ci.id, ci.city_real, co.country_name FROM cities ci
LEFT JOIN countries co use index (idx_name) ON(ci.country_id=co.country_id)
WHERE city_real in ('Delhi','Bangalore','Mumbai') ORDER BY population DESC
this time also I am disappointed because this also takes 68 to 73 secs to complete.
Finally, I have used force index
in my query
SELECT ci.id, ci.city_real, co.country_name FROM cities ci
LEFT JOIN countries co force index (idx_name) ON(ci.country_id=co.country_id)
WHERE city_real in ('Delhi','Bangalore','Mumbai') ORDER BY population DESC
Now I can see the result in just 7secs.
From this example my questions are:
use index
didn't work?force index
any side-effects will be occurred or not?The number of rows in the table countries:- 1200000 rows The number of rows in the table cities:- 45000000 rows
Thanks in Advance...
USE INDEX you give mysql the chance to select another index if it calculates the other is better,
With FORCE INDEX, you force mysql to use it and it will do that.
side effects the query could be slower, than the chosen one by mysql, but when you want another because it choose wrong, you only can get slower
Did you run each test twice? I strongly suspect that the difference (10x) is not due to the use of an index hint, but from the caching of data.
You ran the USE
with nothing in the buffer_pool. So there was a lot of I/O to fetch the 45M rows from one table and 1.2M rows from the other.
Then you ran with FORCE
, which worked the same way. But this time, it found all the data in RAM. So it ran 10 times as fast.
Further evidence can be found from EXPLAIN SELECT
. This will say which index it did use.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.