Home Database Mysql Tutorial MySQL的优化方法总结_MySQL

MySQL的优化方法总结_MySQL

Jun 01, 2016 pm 01:50 PM
Database optimization

bitsCN.com

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

  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)。启用查询缓存后,数据库就会根据一些详情来决定哪些结果可以被重用。它大大简化了应用程序。

  1.3 我们都用MySQL来做什么

  在MySQL最开始的开发过程中,MySQL本来是要准备给大客户用的,他们是瑞典的2个最大的零售商,他们用于货物存储数据管理。

  我们每周从所有的商店中得到交易利润累计结果,以此给商店的老板提供有用的信息,帮助他们分析如果更好的打广告以影响他们的客户。

  数据量相当的大(每个月的交易累计结果大概有7百万),而且还需要显示4-10年间的数据。我们每周都得到客户的需求,他们要求能‘瞬间’地得到数据的最新报表。

  我们把每个月的全部信息存储在一个压缩的‘交易’表中以解决这个问题。我们有一些简单的宏指令集,它们能根据不同的标准从存储的‘交易’表中根据字段分组(产品组、客户id、商店等等)取得结果。我们用一个小Perl脚本动态的生成Web页面形式的报表。这个脚本解析Web页面,执行SQL语句,并且插入结果。我们还可以用PHP或者mod_perl来做这个工作,不过当时还没有这2个工具。

bitsCN.com
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How does Hibernate optimize database query performance? How does Hibernate optimize database query performance? Apr 17, 2024 pm 03:00 PM

Tips for optimizing Hibernate query performance include: using lazy loading to defer loading of collections and associated objects; using batch processing to combine update, delete, or insert operations; using second-level cache to store frequently queried objects in memory; using HQL outer connections , retrieve entities and their related entities; optimize query parameters to avoid SELECTN+1 query mode; use cursors to retrieve massive data in blocks; use indexes to improve the performance of specific queries.

How to improve the access speed of Python website through database optimization? How to improve the access speed of Python website through database optimization? Aug 07, 2023 am 11:29 AM

How to improve the access speed of Python website through database optimization? Summary When building a Python website, a database is a critical component. If the database access speed is slow, it will directly affect the performance and user experience of the website. This article will discuss some ways to optimize your database to improve the access speed of your Python website, along with some sample code. Introduction For most Python websites, the database is a key part of storing and retrieving data. If not optimized, the database can become a performance bottleneck. Book

Spring Boot performance optimization tips: create applications as fast as the wind Spring Boot performance optimization tips: create applications as fast as the wind Feb 25, 2024 pm 01:01 PM

SpringBoot is a popular Java framework known for its ease of use and rapid development. However, as the complexity of the application increases, performance issues can become a bottleneck. In order to help you create a springBoot application as fast as the wind, this article will share some practical performance optimization tips. Optimize startup time Application startup time is one of the key factors of user experience. SpringBoot provides several ways to optimize startup time, such as using caching, reducing log output, and optimizing classpath scanning. You can do this by setting spring.main.lazy-initialization in the application.properties file

How to improve MySQL performance by using composite indexes How to improve MySQL performance by using composite indexes May 11, 2023 am 11:10 AM

In the MySQL database, indexing is a very important means of performance optimization. When the amount of data in the table increases, inappropriate indexes can cause queries to slow down or even cause database crashes. In order to improve database performance, indexes need to be used rationally when designing table structures and query statements. Composite index is a more advanced indexing technology that improves query efficiency by combining multiple fields as indexes. In this article, we will detail how to improve MySQL performance by using composite indexes. What is composite index composite

Java Spring Boot Security performance optimization: make your system fly Java Spring Boot Security performance optimization: make your system fly Feb 19, 2024 pm 05:27 PM

1. Code optimization to avoid using too many security annotations: In Controller and Service, try to reduce the use of @PreAuthorize and @PostAuthorize and other annotations. These annotations will increase the execution time of the code. Optimize query statements: When using springDataJPA, optimizing query statements can reduce database query time, thereby improving system performance. Caching security information: Caching some commonly used security information can reduce the number of database accesses and improve the system's response speed. 2. Use indexes for database optimization: Creating indexes on tables that are frequently queried can significantly improve the query speed of the database. Clean logs and temporary tables regularly: Clean logs and temporary tables regularly

From a technical perspective, why can Oracle beat MySQL? From a technical perspective, why can Oracle beat MySQL? Sep 08, 2023 pm 04:15 PM

From a technical perspective, why can Oracle beat MySQL? In recent years, database management systems (DBMS) have played a vital role in data storage and processing. Oracle and MySQL, two popular DBMSs, have always attracted much attention. However, from a technical perspective, Oracle is more powerful than MySQL in some aspects, so Oracle is able to defeat MySQL. First, Oracle excels at handling large-scale data. Oracl

Common database problems in Linux systems and their solutions Common database problems in Linux systems and their solutions Jun 18, 2023 pm 03:36 PM

With the continuous development of computer technology and the continuous growth of data scale, database has become a vital technology. However, there are some common problems encountered when using databases in Linux systems. This article will introduce some common database problems in Linux systems and their solutions. Database connection problems When using a database, problems such as connection failure or connection timeout sometimes occur. These problems may be caused by database configuration errors or insufficient access rights. Solution: Check the database configuration file to make sure

How to solve database update performance problems in Java development How to solve database update performance problems in Java development Jun 29, 2023 pm 01:00 PM

How to solve database update performance issues in Java development Summary: With the increase in data volume and business changes, database update performance issues have become a major challenge in Java development. This article will introduce some common methods and techniques to solve database update performance problems. Keywords: Java development, database, update performance issues, solutions Introduction: In most Java applications, the database plays an important role. The performance of the database directly affects the response speed and stability of the application. In actual development, the number

See all articles