Home Database Mysql Tutorial 每个 MySQL 开发者都应该了解的 10 个技巧

每个 MySQL 开发者都应该了解的 10 个技巧

Jun 07, 2016 pm 05:31 PM

MySQL是世界上实际最流行的数据库管理系统,是遍布全球编程社区的首

MySQL是世界上实际最流行的数据库管理系统,是遍布全球编程社区的首选。它有一个系列有趣的特性,在很多方面都很擅长。由于其巨大的人气,在网上可以找到许多MySQL的使用技巧。这里有12个最好的技巧和窍门,所有MySQL数据库开发者都应该了解一下。

mysql

避免编辑转储文件 MyISAM 块大小

大多数开发者忘记了这一事实,文件系统往往需要一个大的MyISAM块以保证高效运行。许多开发者不知道块大小的设置。.MYI文件存储在myisam_block_size的设置里,这个设置项可用来修改大的块尺寸。MyISAM块大小的默认值是1K,这不是当前大多数系统的恰当设置。因此,开发者应该考虑指定一个与之相适应的值。

打开 Delay_Key_Write

为避免系统崩溃时数据库损坏delay_key_write默认是关闭的。有人可能会问,如果是这样的话,为什么要把它放在首位打开呢?从防止数据库每次写MyISAM文件时刷新密钥文件方面看这是必要的。通过把它打开,开发者可以节省很多时间。参考MySQL官方手册了解你的版本如何把它打开。

Joins(表连接)

创建索引和使用相同的列类型:join(表连接)操作可以在Mysql中被优化。若应用中有许多join操作,可以通过创建相同的列类型上join来优化。创建索引是加速应用的另一种方法。查询修改有助于你找回期望的查询结果。

优化WHERE从句

即使你只搜索一行MySQL也会查询整个表,因此,建议你当只需要一条结果时将limit设置为1。通过这样做,可以避免系统贯穿搜索整个表,从而可以尽可能快找到与你需求相匹配的记录。

在Select查询上使用Explain关键字

你肯定希望得到与任何特定查询相关的一些帮助。Explain关键词在这方面是非常有帮助的。它在你寻求查询到底做了什么时提供了具体细节。例如,在复杂join查询前键入Explain关键词你会得到很多有用的资料。

使用查询缓存优化查询

MySQL的查询缓存是默认启用的。这主要是因为缓存有助于查询的快速执行,,缓存可以在相同的查询多次运行使用。你在关键字前加入当前日期、CURRDATE等PHP代码使查询缓存它从而启用此功能。

使用堆栈跟踪隔离Bug

各种Bug可以使用stack_trace隔离出来。一个空指针足以毁掉一段特定的代码,任何开发人员都知道它有这样的能力。了解使用堆栈跟踪的细节,从而在你的代码里避免bug。

设置SQL_MODE

枚举类型总是让人感到非常的疑惑。由于字段可能拥有多个可能的值,这些可能的值包括你指定的和null,在编码时将会出现很多问题,你将永远都会得到一个警告说代码不正确。一个简单的解决办法就是设置SQL_MODE。

//Start mysqld with
$–sql-mode=”modes”
//or
$sql-mode=”modes” (my.ini – Windows / my.cnf – Unix)
//Change at runtime, separate multiple modes with a comma
$set [GLOBAL|SESSION] sql_mode=’modes’
//TRADITIONAL is equivalent to the following modes:
STRICT_TRANS_TABLES, STRICT_ALL_TABLES, NO_ZERO_IN_DATE, ERROR_FOR_DIVISION_BY_ZERO, and NO_AUTO_CREATE_USER

修改Root密码

修改root密码对于某些特定设置是必不可少的,修改命令如下:

//Straightforward MySQL 101
$mysqladmin -u root password [Type in selected password]
//Changing users ROOT password
$mysqladmin -u root -p [type old password] newpass [hit enter and type new password. Press enter]
//Use mysql sql command
$mysql -u root -p
//prompt “mysql>” pops up. Enter:
$use mysql;
//Enter user name you want to change password for
$update user set password=PASSWORD (Type new Password Here) where User = ‘username’;
//Don’t forget the previous semicolon, now reload the settings for the users privileges
$flush privileges;
$quit

用MySQL Dump 命令备份数据库

开发者都知道数据库备份的重要性,当系统出现重大故障时能够起到救命的作用。

最简单的备份数据库的方法

1 $mysqldump –user [user name] –password=[password] [database name] > [dump file]

//你也可以用简写"-u","-p"来分别代替"user"和"password"
//将多个数据库导入到一个文件只要在后面添加需要导出数据库的名称:

 

1 mysqldump –user [user name] –password=[password]

2 [first database name] [second database name] > [dump file]

//许多数据库都提供了顺序备份的功能,要备份所有数据库只需要添加--all-databases参数。如果你不喜欢命令行,从Sourceforge上下载automysqlbackup吧。

调整CONFIG的配置

PERL脚本MySQL Tuner是另一个强大的优化数据库性能的工具,它能够帮助你对MySQL配置来进行多处调整和修改。你可以访问该项目的官网来进一步了解它。

推荐阅读:

mysqlslap 压力测试工具

MySQL自带的性能压力测试工具mysqlslap详解

linux

Statement of this Website
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

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

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1242
24
When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

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.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

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.

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Explain the role of InnoDB redo logs and undo logs. Explain the role of InnoDB redo logs and undo logs. Apr 15, 2025 am 12:16 AM

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.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 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.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 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

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 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.

See all articles