mysql索引与优化
简要:
一、索引是什么
二、索引类型及使用语法
三、全文索引说明
一、索引是什么
1、以书的目录为例,通过查看目录,再找到对应的内容。因此,索引就是给数据加上了’目录’,便于快速找到数据
2、索引的作用:
好处: 加快了查询速度
坏处: a、降低了增删改的速度
b、增大了表的文件大小(索引文件甚至可能比数据文件还大)
案例: 设有某个表15列,存在10列上有索引,共500w行数据,如何快速导入?
答: 1、把空表的索引全部删除 2、导入数据 3、数据导入完毕后再建立索引
3、索引算法
设有N条随机记录,不用索引,平均查找N/2次,用了索引呢?
3.1、二叉树索引对应次数为log2N次
说明例子,数据1,2,3,4,5,6,7,以中间值4为分界点
|
查找3需要多少次?
由于32,因此在以2为根节点的右边。结果需要2次
3.2、哈希索引,理论上为1次
说明例子,数据1,2,3,4,5,6,7
|
查找3需要多少次?
先hash下,得到005,这样就找到了。刚好1次。
hash的不足:
a、浪费空间,因为hash的值不连续。
b、hash要求高,确保每个值的hash值不同
4、索引的使用原则
a、不过度索引
b、索引条件列(where后面最频繁的条件比较适宜索引)
c、索引散列值,过于集中的值不要索引(如: 性别)
5、如何看表结构
5.1、存储引擎为myisam
frm为表结构、MYD为数据文件、MYI为索引文件
5.2、存储引擎为innodb
frm为表结构、ibd为数据文件和索引文件
二、索引类型及使用语法
1、类型
a、普通索引(index): 仅仅是加快查询速度
b、唯一索引(unique index): 行上的值不能重复
c、主键索引(primary key): 不能重复
d、全文索引(fulltext index):
唯一索引和主键索引的关系:
主键必唯一,但是唯一索引不一定是主键;一张表上只能有一个主键,但是可以有一个或多个唯一索引
2、如何查看表中的索引
3、建立索引
3.1、对已经存在的表建立索引
语法: alter table 表名 add index/uniqueindex/fulltext index/primary key [索引名](列名) (备注:索引名可选,不指定则与列名相同)
表结构:
create table m( id int, emailvarchar(30),tel char(11), intro text)engine=myisam charset=utf8;
a、给tel列建立普通索引
(备注: 指定索引名与列名相同)
b、给email列加上唯一索引
c、给intro列加上全文索引
d、给id列加上主键索引
e、加上多列组合索引
(备注: 这个普通索引m作用在列email和tel列上)
错误点:
错误原因: 没有指定该索引应用在那个列上。
3.2、建立新表时,指定索引
create table m(id int primary keyauto_increment, email varchar(30), tel char(11), intro text, index(tel), uniqueindex(email), fulltext index(intro) )engine=myisam charset=utf8;
4、删除索引
4.1、删除普通索引/唯一索引/全文索引
4.2、删除主键索引
如果主键列本身就是自增的,则删除时会报错
这个情况,应该先修改列的自增属性。
三、全文索引
全文索引在mysql的默认情况下,对于中文意义不大
因为英文有空格,标点符号来拆成单词,进而对单词进行索引。
而对于中文,没有空格来隔开单词。mysql无法识别每个中文词。
用法: match(全文索引名) against(‘keyword’);

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

WebStorm Mac version
Useful JavaScript development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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

Atom editor mac version download
The most popular open source editor
