Home Database Mysql Tutorial 开源数据库MySQL优化秘籍(1)

开源数据库MySQL优化秘籍(1)

Jun 07, 2016 pm 04:07 PM
mysql optimization Open source database Cheats

数据库优化是一项很复杂的工作,因为这最终需要对系统优化的很好理解才行。尽管对系统或应用系统的了解不多的情况下优化效果还不错,但是如果想优化的效果更好,那么就需要对它了解更多才行。 1、优化概述 让系统运行得快得最重要因素是数据库基本的设计。并

数据库优化是一项很复杂的工作,因为这最终需要对系统优化的很好理解才行。尽管对系统或应用系统的了解不多的情况下优化效果还不错,但是如果想优化的效果更好,那么就需要对它了解更多才行。

1、优化概述

让系统运行得快得最重要因素是数据库基本的设计。并且还必须清楚您的系统要用来做什么,以及存在的瓶颈。

最常见的系统瓶颈有以下几种:

磁盘搜索。它慢慢地在磁盘中搜索数据块。对现代磁盘来说,平时的搜索时间基本上小于10毫秒,因此理论上每秒钟可以做100次磁盘搜索。这个时间对于全新的新磁盘来说提高的不多,并且对于只有一个表的情况也是如此。加快搜索时间的方法是将数据分开存放到多个磁盘中。

磁盘读/写。当磁盘在正确的位置上时,就需要读取数据。对现代磁盘来说,磁盘吞吐量至少是10-20MB/秒。这比磁盘搜索的优化更容易,因为可以从多个媒介中并行地读取数据。

CPU周期。数据存储在主内存中(或者它已经在主内存中了),这就需要处理这些数据以得到想要的结果。

内存带宽。当CPU要将更多的数据存放在CPU缓存中时,主内存的带宽就是瓶颈了。在大多数系统中,这不是常见的瓶颈,不过也是要注意的一个因素。

1.1 MySQL 设计的局限性

当使用MyISAM存储引擎时,MySQL会使用一个快速数据表锁以允许同时多个读取和一个写入。这种存储引擎的最大问题是发生在一个单一的表上同时做稳定的更新操作及慢速查询。如果这种情况在某个表中存在,可以使用另一种表类型。

MySQL可以同时在事务及非事务表下工作。为了能够平滑的使用非事务表(发生错误时不能回滚),有以下几条规则:

所有的字段都有默认值

如果字段中插入了一个"错误"的值,比如在数字类型字段中插入过大数值,那么MySQL会将该字段值置为"最可能的值"而不是给出一个错误。数字类型的值是0,最小或者最大的可能值。字符串类型,不是空字符串就是字段所能存储的最大长度。

所有的计算表达式都会返回一个值而报告条件错误,例如 1/0 返回 NULL。

这些规则隐含的意思是,不能使用MySQL来检查字段内容。相反地,必须在存储到数据库前在应用程序中来检查。

1.2 应用设计的可移植性

由于各种不同的数据库实现了各自的SQL标准,这就需要我们尽量使用可移植的SQL应用。查询和插入操作很容易就能做到可移植,不过由于更多的约束条件的要求就越发困难。想要让一个应用在各种数据库系统上快速运行,就变得更困难了。

为了能让一个复杂的应用做到可移植,就要先看这个应用运行于哪种数据库系统之上,然后看这些数据库系统都支持哪些特性。每个数据库系统都有某些不足。也就是说,由于设计上的一些妥协,导致了性能上的差异。

可以用MySQL的 crash-me 程序来看选定的数据库服务器上可以使用的函数,类型,限制等。crash-me 不会检查各种可能存在的特性,不过这仍然是合乎情理的理解,大约做了450次测试。一个crash-me 的信息类型的例子就是,它会告诉您如果想使用Informix 或 DB2的话,就不能使字段名长度超过18个字符。

crash-me 程序和MySQL基准使每个准数据库都实现了的。可以通过阅读这些基准程序是怎么写的,自己就大概有怎样做才能让程序独立于各种数据库这方面的想法了。这些程序可以在MySQL源代码的 `sql-bench' 目录下找到。他们大部分都是用Perl写的,并且使用DBI接口。由于它提供了独立于数据库的各种访问方式,因此用DBI来解决各种移植性的问题。

如果您想努力做到独立于数据库,这就需要对各种SQL服务器的瓶颈都有一些很好的想法。例如,MySQL对于 MyISAM 类型的表在检索以及更新记录时非常快,但是在有并发的慢速读取及写入记录时却有一定的问题。作为Oracle来说,它在访问刚刚被更新的记录时有很大的问题(直到结果被刷新到磁盘中)。事务数据库一般地在从日志表中生成摘要表这方面的表现不怎么好,因为在这种情况下,行记录锁几乎没用。

为了能让应用程序真正的做到独立于数据库,就必须把操作数据的接口定义的简单且可扩展。由于C++在很多系统上都可以使用,因此使用C++作为数据库的基类结果很合适。

如果使用了某些数据库独有的特定功能(比如 REPLACE 语句就只在MySQL中独有),这就需要通过编写替代方法来在其他数据库中实现这个功能。尽管这些替代方法可能会比较慢,但是它能让其他数据库实现同样的功能。

在MySQL中,可以在查询语句中使用 /*! */ 语法来增加MySQL特有的关键字。然而在很多其他数据库中,/**/ 却被当成了注释(并且被忽略)。

如果有时候更高的性能比数据结果的精确更重要,就像在一些Web应用中那样,这可以使用一个应用层来缓存结果,这可能会有更高的性能。通过让旧数据在一定时间后过期,来合理的更新缓存。这是处理负载高峰期时的一种方法,这种情况下,可以通过加大缓存容量和过期时间直到负载趋于正常。

这种情况下,建表信息中就要包含了初始化缓存的容量以及正常刷新数据表的频率。一个实现应用层缓存的可选方案是使用MySQL的查询缓存(query cache)。启用查询缓存后,数据库就会根据一些详情来决定哪些结果可以被重用。它大大简化了应用程序。

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
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

Compare and contrast MySQL and MariaDB. Compare and contrast MySQL and MariaDB. Apr 26, 2025 am 12:08 AM

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

See all articles