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’);

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

MySQL asynchronous master-slave replication enables data synchronization through binlog, improving read performance and high availability. 1) The master server record changes to binlog; 2) The slave server reads binlog through I/O threads; 3) The server SQL thread applies binlog to synchronize data.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

The installation and basic operations of MySQL include: 1. Download and install MySQL, set the root user password; 2. Use SQL commands to create databases and tables, such as CREATEDATABASE and CREATETABLE; 3. Execute CRUD operations, use INSERT, SELECT, UPDATE, DELETE commands; 4. Create indexes and stored procedures to optimize performance and implement complex logic. With these steps, you can build and manage MySQL databases from scratch.

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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