mysql or condition can use index and avoid full table

        In some cases, the or condition can avoid a full table scan. Although my tests on MariaDB10.0.10 and MySQL5.7.14 were unsuccessful, it is still necessary to record them.

mysql> show variables like 'version';
+---------------+---------------------+
| Variable_name | Value               |
+---------------+---------------------+
| version       | 10.0.10-MariaDB-log |
+---------------+---------------------+
1 row in set (0.00 sec)
mysql> show variables like 'version';
+---------------+------------+
| Variable_name | Value      |
+---------------+------------+
| version       | 5.7.14-log |
+---------------+------------+
1 row in set, 1 warning (0.00 sec)

1. If there is an or condition in the where statement, the index can be used in the myisam table, but not in innodb

1.myisam表

CREATE TABLE IF NOT EXISTS `a` (
  `id` int(1) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `aNum` char(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;
mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
| id | select_type | table | type        | possible_keys | key         | key_len | ref  | rows | Extra                                 |
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
|  1 | SIMPLE      | a     | index_merge | PRIMARY,uid   | PRIMARY,uid | 4,4     | NULL |    2 | Using union(PRIMARY,uid); Using where |
+----+-------------+-------+-------------+---------------+-------------+---------+------+------+---------------------------------------+
1 row in set (0.00 sec)

  

2.innodb table

CREATE TABLE IF NOT EXISTS `a` (
  `id` int(1) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `aNum` char(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `uid` (`uid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;
mysql>  explain select * from a where id=1 or uid =2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | a     | ALL  | PRIMARY,uid   | NULL | NULL    | NULL |    5 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

 

2. All or conditions must be independent indexes

+-------+----------------------------------------------------------------------------------------------------------------------
| Table | Create Table
+-------+----------------------------------------------------------------------------------------------------------------------
| a     | CREATE TABLE `a` (
  `id` int(1) NOT NULL AUTO_INCREMENT,
  `uid` int(11) NOT NULL,
  `aNum` char(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------
1 row in set (0.00 sec)

explain view:

mysql> explain select * from a where id=1 or uid =2;
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | SIMPLE      | a     | ALL  | PRIMARY       | NULL | NULL    | NULL |    5 | Using where |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
1 row in set (0.00 sec)

        Full table scan.

 

3. Replace OR with UNION (for indexed columns)

        Normally, replacing OR in the WHERE clause with UNION will work well, and using OR on indexed columns will result in a full table scan.

        Note that the above rules are only valid for multiple indexed columns. If there are columns that are not indexed, the query efficiency may be reduced because you do not select OR.

        In the example below, both LOC_ID and REGION are indexed.

Efficient:

select loc_id , loc_desc , region from location where loc_id = 10   
union   
select loc_id , loc_desc , region  from location where region = "melbourne"

Inefficient:

select loc_id , loc desc , region from location where loc_id = 10 or region = "melbourne"

        If you insist on using OR, you need to write the index column with the fewest returned records first.

 

4. Use in to replace or  

        This is a simple and easy-to-remember rule, but the actual execution effect needs to be tested. Under oracle8i, the execution paths of the two seem to be the same. 

Inefficient:

select * from location where loc_id = 10 or loc_id = 20 or loc_id = 30

Efficient:

select * from location where loc_in  in (10,20,30);

  

PS: How to check the mysql version

1. Enter "mysql --version" on the command line and press "Enter", as shown in the following figure:


2. Use the command line to view the mysql version - mysql variable view

        Enter "mysql" on the command line, press "Enter" to enter the mysql command line mode, and enter "show variables like 'version';", as shown below:


 

Article source: http://blog.csdn.net/hguisu/article/details/7106159

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326705629&siteId=291194637
Recommended