search
HomeDatabaseMysql Tutorial记录MySQL的一些知识点(2013.04.13)

我非DBA,知识这两天路过学习了一点MySQL的知识,并且进行了一些SQL操作,为了方便记忆以及今后查阅,简单做下记录(有点零乱的)。 SQL命令(语句)的分类:DDL, DML, DCL, TCL Data Definition Language (DDL) statements are used to define the database

我非DBA,知识这两天路过学习了一点MySQL的知识,并且进行了一些SQL操作,为了方便记忆以及今后查阅,简单做下记录(有点零乱的)。

SQL命令(语句)的分类:DDL, DML, DCL, TCL
Data Definition Language (DDL) statements are used to define the database structure or schema. DDL示例:CREATE, ALTER, DROP, TRUNCATE, RENAME
Data Manipulation Language (DML) statements are used for managing data within schema objects. DML示例:SELECT, INSERT, UPDATE, DELETE, CALL, EXPLAIN PLAN, LOCK TABLES
Data Control Language (DCL) statements. DCL示例:GRANT, REVOKE
Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions. TCL示例:COMMIT, SAVEPOINT, ROLLBACK, SET TRANSACTION
来自:http://www.orafaq.com/faq/what_are_the_difference_between_ddl_dml_and_dcl_commands

MyISAM和Innodb存储引擎中的索引的区别:
1. MyISAM默认使用B-tree索引只把索引载入内存,数据缓存依赖于操作系统,InnoDB使用聚集索引实际上是在同样的结构中保存了B-tree索引和数据行,把索引和索引的数据都载入内存缓冲 。
2. MyISAM数据库中的数据是按照插入的顺序保存,在每个索引节点中保存对应的数据行的地址,理论上说主键索引和其他索引是一样的;InnoDB数据库中的数据和主键节点保存在一起,所有其他索引节点中保存的是主键索引的值。
3. 在InnoDB表中插入数据一定要尽可能按照主键增加的顺序,AUTO_INCREMENT最好,这样插入的速度最快,如果没有按照主键顺序插入数据,在插入后最好使用OPTIMIZE TABLE重新组织表。
4. 因为InnoDB索引节点中保存的是主键的值,所以主键的值越简单越好。
5. 对于InnoDB表,在查询的时候如果只需要查找索引列,就不要加入其它列,这样速度最快。
6. MyISAM和Innodb的B-tree索引支持前缀索引,有最左匹配原则。
参考:http://www.cyrec.org/posts/myisam-innodb-index
提高mysql插入数据的速度,可以参考这里提出的一些Tips: http://hi.baidu.com/jackbillow/item/d40d1eedb1eecb0d64db0027


MySQL使用分区表的好处:

1,可以把一些归类的数据放在一个分区中,可以减少服务器检查数据的数量加快查询。
2,方便维护,通过删除分区来删除老的数据。
3,分区数据可以被分布到不同的物理位置,可以做分布式有效利用多个硬盘驱动器。
MySQL可以建立四种分区类型的分区:
1. RANGE 分区:基于属于一个给定连续区间的列值,把多行分配给分区。 (这种分区类型,很久之前在实际业务中也遇到的过的,那时是Oracle)
2. LIST 分区:类似于按RANGE分区,区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择。
3. HASH分区:基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的这些行的列值进行计算。这个函数可以包含MySQL 中有效的、产生非负整数值的任何表达式。
4. KEY 分区:类似于按HASH分区,区别在于KEY分区只支持计算一列或多列,且MySQL 服务器提供其自身的哈希函数。必须有一列或多列包含整数值。
一般用得多的是range分区和list分区。
参考:http://www.cyrec.org/posts/mysql-partition-tables

MySQL事务示例
InnoDB存储引擎支持事务,使用事务的SQL语句示例如下:

START TRANSACTION;
SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=@A WHERE type=1;
COMMIT;

START TRANSACTION or BEGIN start a new transaction.
COMMIT commits the current transaction, making its changes permanent.
ROLLBACK rolls back the current transaction, canceling its changes.
SET autocommit disables or enables the default autocommit mode for the current session.
来自:http://dev.mysql.com/doc/refman/5.6/en/commit.html

关于MySQL中内存的分配、使用、调优等基础知识,见:http://mysql.rjweb.org/doc.php/memory

http://dev.mysql.com/doc/refman/5.6/en/memory-use.html

MySQL的variables和status是管理维护的利器,就类似Oracle的spfile和v$表。
MySQL通过系统变量记录很多配置信息,比如最大连接数max_connections:
mysql> show variables like ‘%connect%’;
+————————–+——————-+
| Variable_name | Value |
+————————–+——————-+
| character_set_connection | latin1 |
| collation_connection | latin1_swedish_ci |
| connect_timeout | 10 |
| init_connect | |
| max_connect_errors | 10 |
| max_connections | 151 |
| max_user_connections | 0 |
+————————–+——————-+
7 rows in set (0.00 sec)
可以在global或session范围内修改这个参数:
mysql> set global max_connections=151;
status命令查询当前MySQL数据库的状态:
mysql> status
MySQL为每个连接分配线程来处理,可以通过threads_connected参数查看当前分配的线程数量:
mysql> show status like ‘%thread%’;
参考:http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html

http://dev.mysql.com/doc/refman/5.6/en/server-status-variables.html

查看当前连接状态信息
有些时候,我们需要查看MYSQL当前有哪些连接,比如IP、用户等信息。执行如下SQL命令即可。
命令: show processlist;
如果是root帐号,你能看到所有用户的当前连接。如果是其它普通帐号,只能看到自己占用的连接。
列出当前连接和状态:
mysql> show processlist; (默认只列出前100条)
mysql> show full processlist; (列出所有连接)
mysql> show full processlist;
+—-+——+———————–+——+———+——+——-+———————–+
| Id | User | Host | db | Command | Time | State | Info |
+—-+——+———————–+——+———+——+——-+———————–+
| 2 | root | localhost | test | Query | 0 | NULL | show full processlist |
| 4 | test | 192.168.199.112:44751 | test | Sleep | 827 | | NULL |
+—-+——+———————–+——+———+——+——-+———————–+
2 rows in set (0.00 sec)
对于影响系统运行的thread,可以用 kill connection|query threadid 的命令杀掉它。

explain查看执行计划:
mysql> explain select * from mytable;
+—-+————-+———+——+—————+——+———+——+——+——-+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+—-+————-+———+——+—————+——+———+——+——+——-+
| 1 | SIMPLE | mytable | ALL | NULL | NULL | NULL | NULL | 2 | |
+—-+————-+———+——+—————+——+———+——+——+——-+
1 row in set (0.00 sec)
参考:http://dev.mysql.com/doc/refman/5.6/en/explain.html

MySQL用户和权限管理
mysql> CREATE USER ‘test’@'%’ IDENTIFIED BY ’123456′;
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL ON *.* TO ‘test’@'%’;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
参考:http://dev.mysql.com/doc/refman/5.6/en/grant.html

MySQL的临时表
A TEMPORARY table is visible only to the current connection, and is dropped automatically when the connection is closed. This means that two different connections can use the same temporary table name without conflicting with each other or with an existing non-TEMPORARY table of the same name.
参考:http://dev.mysql.com/doc/refman/5.6/en/internal-temporary-tables.html

http://www.tutorialspoint.com/mysql/mysql-temporary-tables.htm

http://dev.mysql.com/doc/refman/5.1/en/create-table.html

binary log (binlog)
A file containing a record of all statements that attempt to change table data. These statements can be replayed to bring slave servers up to date in a replication scenario, or to bring a database up to date after restoring table data from a backup.
redo log
A disk-based data structure used during crash recovery, to correct data written by incomplete transactions. During normal operation, it encodes requests to change InnoDB table data, which result from SQL statements or low-level API calls through NoSQL interfaces. Modifications that did not finish updating the data files before an unexpected shutdown are replayed automatically.
The redo log is physically represented as a set of files, typically named ib_logfile0 and ib_logfile1. The data in the redo log is encoded in terms of records affected; this data is collectively referred to as redo. The passage of data through the redo logs is represented by the ever-increasing LSN value. The original 4GB limit on maximum size for the redo log is raised to 512GB in MySQL 5.6.
MySQL术语词汇表:http://dev.mysql.com/doc/refman/5.6/en/glossary.html

Original article: 记录MySQL的一些知识点(2013.04.13)

©2013 笑遍世界. All Rights Reserved.

Statement
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
MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL: Not a Programming Language, But...MySQL: Not a Programming Language, But...Apr 13, 2025 am 12:03 AM

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

MySQL: An Introduction to the World's Most Popular DatabaseMySQL: An Introduction to the World's Most Popular DatabaseApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

The Importance of MySQL: Data Storage and ManagementThe Importance of MySQL: Data Storage and ManagementApr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

Why Use MySQL? Benefits and AdvantagesWhy Use MySQL? Benefits and AdvantagesApr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Describe InnoDB locking mechanisms (shared locks, exclusive locks, intention locks, record locks, gap locks, next-key locks).Apr 12, 2025 am 12:16 AM

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA

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

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use