Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why using IN(...) when selecting on indexed fields, will kill the performance of SELECT query?

Tags:

mysql

Avoid using IN(...) when selecting on indexed fields, It will kill the performance of SELECT query.

I found this here: https://wikis.oracle.com/pages/viewpage.action?pageId=27263381

Can you explain it? Why that will kill performance? And what should I use instead of IN. "OR" statement maybe?

like image 537
errx Avatar asked Dec 21 '11 09:12

errx


People also ask

Does indexing reduce performance?

Of course, Index can reduce the performance of any query and particularly of SELECT queries without even being that indexed utilized directly in the query. There is a huge misconception that Indexes only reduces the performance of INSERT, UPDATE and DELETE statement.

How the indexing affects the performance in database?

Indexing makes columns faster to query by creating pointers to where data is stored within a database. Imagine you want to find a piece of information that is within a large database. To get this information out of the database the computer will look through every row until it finds it.

Why indexing Cannot always increase the performance of queries?

A wrong index can be an index created on a column that doesn't provide easier data manipulation or an index created on multiple columns which instead of speeding up queries, slows them down. A table without a clustered index can also be considered as a poor indexing practice.

What do you think makes a query slow in case of indexing?

Having two identical indexes makes a negative impact on the performance of SQL queries. It is actually a waste of disk space and also slows down the insertions to the table. Therefore, it is a good practice to avoid duplicate indexes to eliminate these issues. Duplication of indexes can happen in multiple ways.


2 Answers

To tell the truth, that statement contradicts to many hints that I have read in books and articles on MySQL.

Here is an example: http://www.mysqlperformanceblog.com/2010/01/09/getting-around-optimizer-limitations-with-an-in-list/

Moreover, expr IN(value, ...) itself has additional enhancements for dealing with large value lists, since it is supposed to be used as a useful alternative to certain range queries:

If all values are constants, they are evaluated according to the type of expr and sorted. The search for the item then is done using a binary search. This means IN is very quick if the IN value list consists entirely of constants.

Still overusing INs may result in slow queries. Some cases are noted in the article.

like image 70
newtover Avatar answered Oct 19 '22 08:10

newtover


Because MySQL can't optimize it.

Here is an example:

explain select * from keywordmaster where id in (1, 567899);

plan (sorry for external link. Doesn't show correctly here)

here is another query:

   explain 
   select * from table where id = 1
   union
   select * from keywordmaster where id = 567899

plan

As you can see in the second query we get ref as const and type is const instead of range. MySQL can't optimize range scans.

like image 24
ravnur Avatar answered Oct 19 '22 07:10

ravnur