Home Database Mysql Tutorial mysql or条件可以使用索引而避免全表扫描_MySQL

mysql or条件可以使用索引而避免全表扫描_MySQL

Jun 01, 2016 pm 01:43 PM
scanning surface

bitsCN.com

 

在某些情况下,or条件可以避免全表扫描的。

 

1 .where 语句里面如果带有or条件, myisam表能用到索引,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表:

 

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 .必须所有的or条件都必须是独立索引:

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

| 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查看:

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)

 

全表扫描了。

 

摘自 hguisu的专栏

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Windows Defender to scan folders in Win10. How to use Windows Defender to scan folders in Win10. Jul 10, 2023 pm 11:17 PM

Windows 10 has a free antivirus program called Windows Defender, which provides real-time protection and can scan your computer. This also allows you to perform customized scans, whereby you can specify specific folders or drives to scan for malware. Because you only need to scan this folder, the scan time will be much faster than scanning the entire machine. As shown below, we offer two ways to customize the scan for your specific folders. How to use Windows Defender to scan folders for malware in Win10. To scan an individual folder and its subfolders, the easiest way is to right-click the folder and select Scan with Windows Defender

How does NetEase Cloud Music scan local music_NetEase Cloud Music scans local music tutorial How does NetEase Cloud Music scan local music_NetEase Cloud Music scans local music tutorial Mar 25, 2024 pm 10:21 PM

1. Open NetEase Cloud Music, click My, then click Local Music. 2. Click the three dots in the upper right corner. 3. Click Scan local music. 4. Click Scan Settings below. 5. Swipe left to filter audio files shorter than 60 seconds. 6. Go back and click Full Scan to scan all local music.

How to scan from HP printer into a pdf How to scan from HP printer into a pdf Feb 19, 2024 am 10:06 AM

After using HP printers to scan documents, many users want to scan them directly into a PDF file, but they don't know how to do it successfully. They just need to use a scanner program on their computer. How to scan an HP printer into a PDF: 1. First open the scanner program on your computer. 2. Then select "Save PDF" in the page settings. 3. Then press "Scan" in the lower right corner to start scanning the first file. 4. After completion, click the "+" icon in the lower left corner to add a new scan page. 5. You will see a new scan box next to the original file. 7. When finished, select "Save" to save these PDF files.

How to scan Quark QR code How to scan Quark QR code Feb 27, 2024 pm 04:10 PM

In Quark software, a variety of functions bring convenience and fun to users, among which the scanning function is particularly popular. Through the scanning function, users can easily scan the QR code, whether it is to quickly log in to the website, add friends, or download applications, all can be done with one click. So, how to use the scanning function of Quark QR code? Players who still don’t know how to use it must not miss it. Come and follow the article brought by the editor of this website to learn more about it. How to scan the Quark QR code and answer: [Quark]-[Three horizontal icons]-[Take photo and scan]. Specific steps: 1. First open the Quark software. After entering the homepage, we click the [three horizontal icons] in the lower right corner; 2. Then slide up on the My Page to the bottom of the page to find [Photo Scan]

How to deal with blurred scanned documents How to deal with blurred scanned documents Jan 05, 2021 pm 03:09 PM

Processing method: 1. Open the scanned image to be processed in the PS software; 2. Click "Filter" - "Sharpening" - "Smart Sharpening" on the top toolbar; 3. In the pop-up window, select according to your needs Sharpen the radius and click "OK"; 4. Click "File" - "Save As" and save it as a file.

C# Development Notes: Security Vulnerability Scanning and Repair C# Development Notes: Security Vulnerability Scanning and Repair Nov 23, 2023 am 08:26 AM

In C# development, with the continuous development of network technology, security issues have become more and more serious. To ensure the security of applications, developers need to pay attention to scanning and repairing security vulnerabilities. This article will introduce precautions from the following aspects. 1. Pay attention to data transmission security In applications, data transmission security is very important. Especially when using the network to transmit data, secure protocols and encryption algorithms should be used wherever possible. In order to ensure data integrity and confidentiality, it is recommended to use Secure Socket Layer (SSL) or Transport Layer Security (TLS), etc.

How to use QR code scanning in PHP? How to use QR code scanning in PHP? Aug 19, 2023 pm 07:49 PM

How to use QR code scanning in PHP? QR code scanning has become very common in modern life. Whether it is Alipay, WeChat Pay or various other applications, you can quickly pay or transfer information by scanning the QR code. In PHP, we can also easily implement the QR code scanning function. This article will introduce how to use PHP for QR code scanning and provide corresponding code examples. First, we need a PHP QR code generation library. Here we choose to use the "PHPQRCode" library.

Scan printed and handwritten notes in the Notes app for iPhone Scan printed and handwritten notes in the Notes app for iPhone Nov 29, 2023 pm 11:19 PM

In 2022, Apple added a new feature to the Notes app on iPhone and iPad that allows you to quickly scan printed or handwritten text and save it in a digital text format. Read on to learn how it works. On earlier versions of iOS and iPadOS, scanning text into Apple's Notes app required tapping the note's text field and then tapping the "Live Text" option in the pop-up menu. However, Apple is making it easier to digitize real-world notes in 2022. The following steps show you how to do this on a device running iOS 15.4 or iPadOS 15.4 and above. On your iPhone or iPad, open "

See all articles