Description:
I'm seeing this in mysql 5.6.33
The table looks like:
```
mysql> mysql> show create table users \G
*************************** 1. row ***************************
Table: users
Create Table: CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`active` tinyint(1) NOT NULL DEFAULT '1',
`system_account` tinyint(1) NOT NULL DEFAULT '0',
`email` varchar(255) NOT NULL,
`user_type` varchar(20) NOT NULL,
`created_at` datetime DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`sermo_employee` tinyint(1) DEFAULT '0',
`eula_date` datetime DEFAULT NULL,
`eula_rejected_date` datetime DEFAULT NULL,
`reset_password_token` varchar(255) DEFAULT NULL,
`reset_password_sent_at` datetime DEFAULT NULL,
`remember_created_at` datetime DEFAULT NULL,
`sign_in_count` int(11) DEFAULT '0',
`current_sign_in_at` datetime DEFAULT NULL,
`last_sign_in_at` datetime DEFAULT NULL,
`current_sign_in_ip` varchar(255) DEFAULT NULL,
`last_sign_in_ip` varchar(255) DEFAULT NULL,
`failed_attempts` int(11) DEFAULT '0',
`unlock_token` varchar(255) DEFAULT NULL,
`locked_at` datetime DEFAULT NULL,
`login_token` varchar(255) DEFAULT NULL,
`dea_attempt` int(11) DEFAULT '0',
`primary_role` varchar(255) DEFAULT NULL,
`uuid` varchar(255) DEFAULT NULL,
`client_identifier` varchar(255) DEFAULT NULL,
`otp_secret_key` varchar(255) DEFAULT NULL,
`second_factor_attempts_count` int(11) DEFAULT '0',
`mobile_phone` varchar(15) DEFAULT NULL,
`master_group_staff_master_group_id` int(11) DEFAULT NULL,
`call_center` varchar(255) DEFAULT NULL,
`pat_enabled` tinyint(1) NOT NULL DEFAULT '1',
`about_me` text,
`avatar_file_name` varchar(255) DEFAULT NULL,
`avatar_content_type` varchar(255) DEFAULT NULL,
`avatar_file_size` int(11) DEFAULT NULL,
`avatar_updated_at` datetime DEFAULT NULL,
`browser_notifications_enabled` tinyint(1) DEFAULT NULL,
`profile_setup_completed_at` datetime DEFAULT NULL,
`profile_setup_skip_count` int(11) DEFAULT '0',
`profile_setup_last_skipped_at` datetime DEFAULT NULL,
`last_synced_at` datetime DEFAULT NULL,
`activity_based_on_transition_in_cares` tinyint(1) NOT NULL DEFAULT '0',
`county` varchar(255) DEFAULT NULL,
`state` varchar(2) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `index_users_on_email` (`email`),
UNIQUE KEY `index_users_on_reset_password_token` (`reset_password_token`),
UNIQUE KEY `index_users_on_unlock_token` (`unlock_token`),
UNIQUE KEY `index_users_on_uuid` (`uuid`),
UNIQUE KEY `index_users_on_otp_secret_key` (`otp_secret_key`),
KEY `index_users_on_active` (`active`),
KEY `index_users_on_created_at` (`created_at`),
KEY `index_users_on_first_name` (`first_name`),
KEY `index_users_on_last_name` (`last_name`),
KEY `index_users_on_sermo_employee` (`sermo_employee`),
KEY `index_users_on_user_type` (`user_type`),
KEY `index_users_on_client_identifier` (`client_identifier`),
KEY `users_master_group_staff_master_group_id_fk` (`master_group_staff_master_group_id`),
KEY `index_users_on_last_synced_at` (`last_synced_at`) USING BTREE,
FULLTEXT KEY `index_users_on_first_name_and_last_name` (`first_name`,`last_name`),
CONSTRAINT `users_master_group_staff_master_group_id_fk` FOREIGN KEY (`master_group_staff_master_group_id`) REFERENCES `master_groups` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=38498 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
```
```
mysql> SET SESSION optimizer_switch="index_merge_intersection=on";
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT SQL_NO_CACHE count(`users`.`id`) FROM `users` WHERE `users`.`user_type` = 'master_group_staff' AND `users`.`master_group_staff_master_group_id` = 35;
+---------------------+
| count(`users`.`id`) |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.00 sec)
mysql> explain SELECT SQL_NO_CACHE count(`users`.`id`) FROM `users` WHERE `users`.`user_type` = 'master_group_staff' AND `users`.`master_group_staff_master_group_id` = 35;
+----+-------------+-------+-------------+----------------------------------------------------------------------+----------------------------------------------------------------------+---------+------+------+-----------------------------------------------------------------------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------------+----------------------------------------------------------------------+----------------------------------------------------------------------+---------+------+------+-----------------------------------------------------------------------------------------------------------------+
| 1 | SIMPLE | users | index_merge | index_users_on_user_type,users_master_group_staff_master_group_id_fk | users_master_group_staff_master_group_id_fk,index_users_on_user_type | 5,62 | NULL | 5 | Using intersect(users_master_group_staff_master_group_id_fk,index_users_on_user_type); Using where; Using index |
+----+-------------+-------+-------------+----------------------------------------------------------------------+----------------------------------------------------------------------+---------+------+------+-----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> SET SESSION optimizer_switch="index_merge_intersection=off";
Query OK, 0 rows affected (0.00 sec)
mysql> explain SELECT SQL_NO_CACHE count(`users`.`id`) FROM `users` WHERE `users`.`user_type` = 'master_group_staff' AND `users`.`master_group_staff_master_group_id` = 35;
+----+-------------+-------+------+----------------------------------------------------------------------+---------------------------------------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+----------------------------------------------------------------------+---------------------------------------------+---------+-------+------+-------------+
| 1 | SIMPLE | users | ref | index_users_on_user_type,users_master_group_staff_master_group_id_fk | users_master_group_staff_master_group_id_fk | 5 | const | 38 | Using where |
+----+-------------+-------+------+----------------------------------------------------------------------+---------------------------------------------+---------+-------+------+-------------+
1 row in set (0.00 sec)
```
https://bugs.mysql.com/bug.php?id=80789 is probably the same bug but I can't open it.
https://bugs.mysql.com/bug.php?id=79675 doesn't appear to be related because there is no subquery.
How to repeat:
I can't figure out a way to repeat it. It happens randomly. If I restart mysql, it works for some period of time - weeks?. It can happen on master but the replica is fine or the opposite scenario.
Suggested fix:
Temporary fixes:
Restarting mysql will fix it.
`analyze table users` will fix it.
Disabling index_merge_intersection will fix it.
SET SESSION optimizer_switch="index_merge_intersection=on"