bitsCN.com
mysql数据库优化语句 数据库语句:
Ddl(数据定义语言) alter create drop Dml(数据操作语言) inset delete update Dtl(数据事务语言) conmmit rollback savepoint Select Dcl(数据控制语句) grant赋权限 revoke回收 Mysql数据库优化:
1、 数据库表 要设计合理(符合3NF,有时候也需要适当的逆范式)2、 Sql语句的优化(索引,常用小技巧)3、 数据库的配置4、 适当的硬件配置和操作系统5、 读写分离 问:什么是数据库3范式? 1NF: 就是具有原子性,不可分割(只要使用的是关系型数据库,就会自动符合) 2NF: 在满足1NF的基础上,我们考虑是否满足2NF,只要表的记录满足唯一性,也就是说,你的同一张表中不可能出现完全相同的记录,一般说我们在表中设计一个主键即可。 3NF: 在满足2NF: 的基础上,我们考虑是否满足3NF,既我们的字段信息可以通过关联的关系,派生即可(通常我们通过外键来处理)使用外键数据库的存储引擎必须是innoDB 问2:数据库参数配置 对于innodb存储引擎最重要的就是内存,所以下面的两个参数调的很大 Innodb_additional_mem_pool_size = 64M Innodb_buffer_pool_size = 1G 缓冲池大小 对于myisam,需要调整key_buffer_size 用show status 语句可以看到当前状态,以决定调整那些参数 一、显示你使用过多少次insert , update , delete 等Sql: show status like “Com”; //在命令窗口中不关闭的时候查询会准确,如果关闭就会从新开始统计 Show sessionstatus like “Com_update”; //就算关闭窗口也会将全部的你执行过的次数统计出来 Show globalstatus like “Com_insert”; Example: session 假如已经使用了6次update1、 用session统计 会是6次如果关闭后命令窗口后在执行Show session statuslike “Com_update”; 就为0了2、 但是如果用Show global status like “Com_insert”;就是6次 二、显示试图连接Mysql服务器的次数 Show status like “Connections”; 数据库启动多长时间了 Show status like “uptime”; 显示慢查询多少次(默认是10秒) Show status like “Slow_queries”; 四、如何在一个项目中,找到慢查询的select,mysql数据库支持把慢查询的语句记录到日志中,供程序员来分析 步骤:1、 启动mysql(特殊的启动方式)a) 在mysql的安装目录下的bin目录下启动mysqld.exe –slow-queryb) Netstat –an 查看3306端口是否启动c) 查询慢查询的次数 show status like “Slow_queries”;d) 设置慢查询的时间 set long_query_time=1; 索引优化: 比如说增加主键索引 Alter table user add primary key(id); 删除主键索引 Alter table user drop primary key删除索引 Alter table user drop index 索引名显示索引 Show index(es) from 表名 Show keys from 表名 Desc 表名 增加索引致使查询会变快好多,其原理就像一本书如果没有目录的话那么如果你想找一个知识点会很难找到,只能一点一点的翻着找,如果有目录的话会很快的定位到这个知识点在那个章节中大概什么位置这样查询起来自然就会快了啊,但是有利必有弊,索引会对查询带来好处,但是对add update delete 来说自然就很麻烦了,比如说你添加一个知识点,你不许还有在目录中添加他是属于那章那节中的那个知识点,同样在修改和删除的时候也会随之改变,来保持信息的准确性。 一个自动分析是否需要使用索引的命令:explainExample: explain select * from emp where id = 9;
索引的分类: 主键索引(primary key) 唯一键索引(unique) Index(普通索引) 全文索引(fulltext) 复合索引(多列和在一起) 在那些列上添加索引比较合适:1、比较频繁的作为查询条件的字段应该加上索引2、 唯一性比较差的字段不适合单独创建索引,及时频繁作为查询条件3、 更新非常频繁的字段不适合创建索引4、 不会出现在where子句中的字段不该创建索引 查询一个表中的所有索引: show indexes from table(表名) 索引的使用: 查询要使用索引最重要的条件是查询条件中需要使用索引 以下几种情况可能会使用到索引1、 对于创建的多列索引,只要查询条件使用了最左边的列,索引一般就会被使用2、 对于使用like的查询,查询如果是‘%aaa’不会使用到索引‘aaa%’会使用到索引 以下的表中将不使用索引1、 如果条件中有or,即使其中有条件带索引也不会使用2、 对于多列索引,不是使用的第一部分,则不会使用索引3、 Like查询是以%开头4、 如果列类型是字符串,那么一定要在条件中将数据使用引号引起来,否则不使用索引。5、 如果mysql估计使用全表扫描要比使用索引快,则不使用索引。 查看索引的使用情况 Show status like ‘handler_read%’;
只有handler_read_key 越大越好 Handler_read_rnd_next 越小越好 数据库类型: MyISAM 不支持事务和外键,一张表由三个文件组成,.frm .myi .myd innoDB 支持事务和外键 对于MyISAM来说查询快,不过删除字段时空间是不会释放的,必须使用手动释放 optimize table table_name 数据库分表:1、 水平分表 2、 垂直分表: Stu表: id Name Pass PhotoMark表 Id Sid Question answer垂直分表针对于关联类型的表,比如说,有一个学生的个人信息(有头像)表,一个考试信息(考试题和答案)表,这时我想查一个学生的考试分数和个人信息,那么mysql会将学生的个人信息和考试信息表关联,速度回降低很多,所以要将考试信息中的答案和题目分开在创建一个表,如果在提高还可以将头像分开为一个单独的表(如百度单独的图片服务器) 读写分离:
作者 web8 bitsCN.com

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

The relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


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

WebStorm Mac version
Useful JavaScript development tools

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

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.