Comments on: SQL mini hack of the day, inverted IN clause https://shlomi-noach.github.io/blog/mysql/sql-mini-hack-of-the-day-inverted-in-clause Blog by Shlomi Noach Sun, 15 Nov 2015 14:27:49 +0000 hourly 1 https://wordpress.org/?v=5.3.3 By: shlomi https://shlomi-noach.github.io/blog/mysql/sql-mini-hack-of-the-day-inverted-in-clause/comment-page-1#comment-334314 Sun, 15 Nov 2015 14:27:49 +0000 https://shlomi-noach.github.io/blog/?p=7451#comment-334314 As I suggested within the post, this negates the use of index:

explain select hostname from database_instance where cluster_name='our.cluster.name:3306';
+----+-------------+-------------------+------+------------------+------------------+---------+-------+------+-------------+
| id | select_type | table             | type | possible_keys    | key              | key_len | ref   | rows | Extra       |
+----+-------------+-------------------+------+------------------+------------------+---------+-------+------+-------------+
|  1 | SIMPLE      | database_instance | ref  | cluster_name_idx | cluster_name_idx | 130     | const |    4 | Using where |
+----+-------------+-------------------+------+------------------+------------------+---------+-------+------+-------------+

explain select hostname from database_instance where 'our.cluster.name:3306' in(cluster_name, '') ;
+----+-------------+-------------------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------------------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | database_instance | ALL  | NULL          | NULL | NULL    | NULL | X000 | Using where |
+----+-------------+-------------------+------+---------------+------+---------+------+------+-------------+

This may, at first glance, make you want to avoid this trick altogether, but it really becomes more viable in your common website “filter by multiple options” type of queries (where multiple columns are involved, and there’s no way for you to index any combination of them), or when you know you’re already filtering by some other column that is indexed.

X000 above is to obscure production number of servers.

]]>
By: Jean-François Gagné https://shlomi-noach.github.io/blog/mysql/sql-mini-hack-of-the-day-inverted-in-clause/comment-page-1#comment-334279 Sun, 15 Nov 2015 11:19:31 +0000 https://shlomi-noach.github.io/blog/?p=7451#comment-334279 Very interesting.
One question: how does the optimizer react to this type of SQL statements (index on cluster_name) ?
Thanks for sharing.

]]>