The mysqlor condition can be indexed to avoid full table scan _ MySQL

Source: Internet
Author: User
The mysqlor condition can be indexed to avoid full table scan of bitsCN.com.

In some cases, the or condition can avoid full table scan.

1. if the where statement contains the or condition, the myisam table can use indexes, but innodb does not.

1) myisam 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 = 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 | 5 | Using where |

+ ---- + ------------- + ------- + ------ + --------------- + ------ + --------- + ------ + ------------- +

1 row in set (0.00 sec)

2. all or conditions must be independent indexes:

+ ------- + Response ----------------------------------------------------------------------------------------------------------------------

| Table | Create Table

+ ------- + Response ----------------------------------------------------------------------------------------------------------------------

| 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 |

+ ------- + Response ----------------------------------------------------------------------------------------------------------------------

1 row in set (0.00 sec)

To view the description, follow these steps:

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 | 5 | Using where |

+ ---- + ------------- + ------- + ------ + --------------- + ------ + --------- + ------ + ------------- +

1 row in set (0.00 sec)

The entire table is scanned.

From the hguisu column

BitsCN.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.