MySQL SQL优化的索引问题详解
以下的文章主要介绍的是MySQL SQL优化的索引问题,我们大家都知道在一般的数据中,很多人喜欢用相关索引来对MySQL数据库进行优化。我们通过相关索引在一般的情况下,是帮助我们解决大多数的MySQL SQL性能问题。 1. 索引的存储分类 MyISAM存储引擎的表的数据
以下的文章主要介绍的是MySQL SQL优化的索引问题,我们大家都知道在一般的数据中,很多人喜欢用相关索引来对MySQL数据库进行优化。我们通过相关索引在一般的情况下,是帮助我们解决大多数的MySQL SQL性能问题。
1. 索引的存储分类
MyISAM存储引擎的表的数据和索引时自动分开存储的,各自是独立的一个文件;InnoDB存储引擎的表的数据和索引时存储在同一表空间里面,但可以有多个文件组成。
MySQL中索引的存储类型目前只有两种(BTREE和HASH),具体和表的存储引擎相关;MyISAM和InnoDB存储引擎都只支持BTREE索引;MEMORY/HEAP存储引擎可以支持HASH和BTREE索引。
MySQL目前不支持函数索引,但是能对列的前面某一部分进行索引,例如name字段,可以以只取name的前4个字符进行索引,这个特征可以大大缩小索引文件的大小。在设计表结构的时候也可以对文本列根据此特性进行灵活设计。例如
引用
<ol class="dp-xml"><li class="alt"><span><span>create index ind_company2_name on company2(name(4)) </span></span></li></ol>
2. MySQL如何使用索引
索引用于快速找出在某个列中有一特定值的行。对相关列使用索引时提高SELECT操作性能的最佳途径。
查询要使用索引最主要的条件是查询条件中需要使用索引关键字,如果是多列MySQL SQL优化索引,那么只有查询条件使用了多列关键字最左边的前缀时,才可以使用索引,否则将不能使用索引。
1. 使用索引
在MySQL中,下列几种情况下可能使用索引。
对于创建的多列索引,只要查询的条件中用到了最左边的列,索引一般就会使用。
例如:
引用
我们首先按company_id ,Moneys的顺序创建一个复合索引
<ol class="dp-xml"><li class="alt"><span><span>create index ind_sales2_companyid_moneys on sales2(company_id,moneys) </span></span></li></ol>
如果按company_id进行表查询
引用
使用explain来分析下
<ol class="dp-xml"> <li class="alt"><span><span>explain select * from sales2 where </span><span class="attribute">company_id</span><span> =</span><span class="attribute-value">2000</span><span> \G; </span></span></li> <li> <span>explain select * from sales2 where </span><span class="attribute">moneys</span><span> = </span><span class="attribute-value">1</span><span>\G; </span> </li> </ol>
通过上面你可以发现即便where条件中不是用company_id 和 moneys的组合条件,MySQL SQL优化之索引仍然能用到,这就是索引的前缀特性。但是如果只按照moneys条件查询表,那么索引就不会被用到。
对于使用like的查询,后面如果是常量并且只有%号不在第一字符,索引才能会被使用例如
引用
<ol class="dp-xml"> <li class="alt"><span><span>explain select * from company2 where name like "%3"\G; </span></span></li> <li><span>explain select * from company2 where name like "3%"\G; </span></li> </ol>
以上两句你可以认为是一样的。其实是不一样的。第一句其实没有用到索引,而第二句才能够利用到索引。另外如果like后面跟的是一个列的名字,那么索引也不会被使用。
如果对大是文本进行搜索,使用全文索引而不用使用like"%..%" 如果列名是索引,使用column_name is null 将使用MySQL SQL优化之索引如 查询name为nll的记录就用到了索引
引用
<ol class="dp-xml"><li class="alt"><span><span>explain select * from company2 where name is null \G; </span></span></li></ol>
2. 下面一些情况存在索引但不使用索引,你可能认为它会用,但是实际上它就是没用。
引用
1. 如果Mysql估计使用索引比全表扫描更慢,则不使用索引。
例如列key_part1均匀分布在1和100之间,下列查询中使用索引就不是很好
<ol class="dp-xml"><li class="alt"><span><span>select * from table_name where key_part1 </span><span class="tag">></span><span> 1 and key_part1 </span><span class="tag"><span> </span><span class="tag-name">90</span><span>; </span></span></span></li></ol>
2. 如果使用MEMORY/HEAP表并且where条件中不使用"="进行索引列,那么不会用到索引。heap表只有在" ="的条件下才会使用索引
3. 用or分割开的条件,如果or前的条件中的列有索引,而后面的列中没用MySQL SQL优化之索引,那么涉及的索引都不会被用到
4. 如果不是索引列的第一部分,那么也不会使用。
5. 如果like是以"%"开始
6. 如果列类型是字符串,那么一定记得在where条件中把字符常量值用引号引起来,否则即便这个列上有索引,Mysql也不会使用。因为MYSQL默认把输入的常量值进行转换以后才进行检索。
最后查看索引使用情况
如果索引正在工作,Handler_read_key的值将很高,这个值代表了一个行被索引值读的次数,很低的值表明增加索引得到的性能改善不高,因为索引经常不被使用到。Handler_read_rnd_next的值高则说明查询运行低效,并且应该建立索引补救。这个值的含义是在数据文件中读取下一行的请求数。如果正进行大量的表扫描,Handler_read_rnd_next的值较高,则通常说明表索引不正确或者写入的查询没有利用MySQL SQL优化之索引。
还记得怎么看Handler_read_rnd_next 吗? 使用
<ol class="dp-xml"><li class="alt"><span><span>show statuts like 'Handler_read_%'; </span></span></li></ol>
以上的相关内容就是对MySQL SQL优化的索引问题的介绍,望你能有所收获。

MySQLoffersvariousstorageengines,eachsuitedfordifferentusecases:1)InnoDBisidealforapplicationsneedingACIDcomplianceandhighconcurrency,supportingtransactionsandforeignkeys.2)MyISAMisbestforread-heavyworkloads,lackingtransactionsupport.3)Memoryengineis

Common security vulnerabilities in MySQL include SQL injection, weak passwords, improper permission configuration, and unupdated software. 1. SQL injection can be prevented by using preprocessing statements. 2. Weak passwords can be avoided by forcibly using strong password strategies. 3. Improper permission configuration can be resolved through regular review and adjustment of user permissions. 4. Unupdated software can be patched by regularly checking and updating the MySQL version.

Identifying slow queries in MySQL can be achieved by enabling slow query logs and setting thresholds. 1. Enable slow query logs and set thresholds. 2. View and analyze slow query log files, and use tools such as mysqldumpslow or pt-query-digest for in-depth analysis. 3. Optimizing slow queries can be achieved through index optimization, query rewriting and avoiding the use of SELECT*.

To monitor the health and performance of MySQL servers, you should pay attention to system health, performance metrics and query execution. 1) Monitor system health: Use top, htop or SHOWGLOBALSTATUS commands to view CPU, memory, disk I/O and network activities. 2) Track performance indicators: monitor key indicators such as query number per second, average query time and cache hit rate. 3) Ensure query execution optimization: Enable slow query logs, record and optimize queries whose execution time exceeds the set threshold.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

MySQL uses a GPL license. 1) The GPL license allows the free use, modification and distribution of MySQL, but the modified distribution must comply with GPL. 2) Commercial licenses can avoid public modifications and are suitable for commercial applications that require confidentiality.

The situations when choosing InnoDB instead of MyISAM include: 1) transaction support, 2) high concurrency environment, 3) high data consistency; conversely, the situation when choosing MyISAM includes: 1) mainly read operations, 2) no transaction support is required. InnoDB is suitable for applications that require high data consistency and transaction processing, such as e-commerce platforms, while MyISAM is suitable for read-intensive and transaction-free applications such as blog systems.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
