search
HomeDatabaseMysql Tutorial让PHP网站运转如飞MySQL的优化_MySQL

让PHP网站运转如飞MySQL的优化_MySQL

Jun 01, 2016 pm 01:54 PM
php websiteoptimizationDatabase Tablehardware

一、我们可以且应该优化什么?
  硬件
  
  操作系统/软件库
  
  SQL服务器(设置和查询)
  
  应用编程接口(API)
  
  应用程序
  
  二、优化硬件
  如果你需要庞大的数据库表(>2G),你应该考虑使用64位的硬件结构,像Alpha、Sparc或即将推出的IA64。因为MySQL内部使用大量64位的整数,64位的CPU将提供更好的性能。
  
  对大数据库,优化的次序一般是RAM、快速硬盘、CPU能力。
  
  更多的内存通过将最常用的键码页面存放在内存中可以加速键码的更新。
  
  如果不使用事务安全(transaction-safe)的表或有大表并且想避免长文件检查,一台UPS就能够在电源故障时让系统安全关闭。
  
  对于数据库存放在一个专用服务器的系统,应该考虑1G的以太网。延迟与吞吐量同样重要。
  
  三、优化磁盘
  为系统、程序和临时文件配备一个专用磁盘,如果确是进行很多修改工作,将更新日志和事务日志放在专用磁盘上。
  低寻道时间对数据库磁盘非常重要。对与大表,你可以估计你将需要log(行数)/log(索引块长度/3*2/(键码长度 + 数据指针长度))+1次寻到才能找到一行。对于有500000行的表,索引Mediun int类型的列,需要log(500000) / log(1024/3*2/(3 + 2))+1=4次寻道。上述索引需要500000*7*3/2=5.2M的空间。实际上,大多数块将被缓存,所以大概只需要1-2次寻道。
  然而对于写入(如上),你将需要4次寻道请求来找到在哪里存放新键码,而且一般要2次寻道来更新索引并写入一行。
  
  对于非常大的数据库,你的应用将受到磁盘寻道速度的限制,随着数据量的增加呈N log N数据级递增。
  
  将数据库和表分在不同的磁盘上。在MySQL中,你可以为此而使用符号链接。
  条列磁盘(RAID 0)将提高读和写的吞吐量。
  带镜像的条列(RAID 0+1)将更安全并提高读取的吞吐量。写入的吞吐量将有所降低。
  不要对临时文件或可以很容易地重建的数据所在的磁盘使用镜像或RAID(除了RAID 0)。
  在Linux上,在引导时对磁盘使用命令hdparm -m16 -d1以启用同时读写多个扇区和DMA功能。这可以将响应时间提高5~50%。
  在Linux上,用async (默认)和noatime挂载磁盘(mount)。
  对于某些特定应用,可以对某些特定表使用内存磁盘,但通常不需要。
  
  四、优化操作系统
  不要交换区。如果内存不足,增加更多的内存或配置你的系统使用较少内存。
  不要使用NFS磁盘(会有NFS锁定的问题)。
  增加系统和MySQL服务器的打开文件数量。(在safe_mysqld脚本中加入ulimit -n #)。
  增加系统的进程和线程数量。
  如果你有相对较少的大表,告诉文件系统不要将文件打碎在不同的磁道上(Solaris)。
  使用支持大文件的文件系统(Solaris)。
  选择使用哪种文件系统。在Linux上的Reiserfs对于打开、读写都非常快。文件检查只需几秒种。
  
  五、操作系统移植
  PERL
  可在不同的操作系统和数据库之间移植。
  适宜快速原型。
  应该使用DBI/DBD接口。
  PHP
  比PERL易学。
  使用比PERL少的资源。
  通过升级到PHP4可以获得更快的速度。
  C
  MySQL的原生接口。
  较快并赋予更多的控制。
  低层,所以必须付出更多。
  C++
  较高层次,给你更多的时间来编写应用。
  仍在开发中
  ODBC
  运行在Windows和Unix上。
  几乎可在不同的SQL服务器间移植。
  较慢。MyODBC只是简单的直通驱动程序,比用原生接口慢19%。
  有很多方法做同样的事。很难像很多ODBC驱动程序那样运行,在不同的领域还有不同的错误。
  问题成堆。Microsoft偶尔还会改变接口。
  不明朗的未来。(Microsoft更推崇OLE而非ODBC)
  ODBC
  运行在Windows和Unix上。
  几乎可在不同的SQL服务器间移植。
  较慢。MyODBC只是简单的直通驱动程序,比用原生接口慢19%。
  有很多方法做同样的事。很难像很多ODBC驱动程序那样运行,在不同的领域还有不同的错误。
  问题成堆。Microsoft偶尔还会改变接口。
  不明朗的未来。(Microsoft更推崇OLE而非ODBC)
  JDBC
  理论上可在不同的操作系统何时据库间移植。
  可以运行在web客户端。
  Python和其他
  可能不错,可我们不用它们。
  
  六、优化应用
  应该集中精力解决问题。
  在编写应用时,应该决定什么是最重要的:
  速度
  操作系统间的可移植性
  SQL服务器间的可移植性
  使用持续的连接。.
  缓存应用中的数据以减少SQL服务器的负载。
  不要查询应用中不需要的列。
  不要使用SELECT * FROM table_name...
  测试应用的所有部分,但将大部分精力放在在可能最坏的合理的负载下的测试整体应用。通过以一种模块化的方式进行,你应该能用一个快速“哑模块”替代找到的瓶颈,然后很容易地标出下一个瓶颈。
  如果在一个批处理中进行大量修改,使用LOCK TABLES。例如将多个UPDATES或DELETES集中在一起。
  
  七、应该使用可移植的应用
  Perl DBI/DBD
  ODBC
  JDBC
  Python(或其他有普遍SQL接口的语言)
  你应该只使用存在于所有目的SQL服务器中或可以很容易地用其他构造模拟的SQL构造。www.mysql.com上的Crash-me页可以帮助你。
  为操作系统/SQL服务器编写包装程序来提供缺少的功能。
  
  八、如果你需要更快的速度,你应该:
  找出瓶颈(CPU、磁盘、内存、SQL服务器、操作系统、API或应用)并集中全力解决。
  使用给予你更快速度/灵活性的扩展。
  逐渐了解SQL服务器以便能为你的问题使用可能最快的SQL构造并避免瓶颈。
  优化表布局和查询。
  使用复制以获得更快的选择(select)速度。
  如果你有一个慢速的网络连接数据库,使用压缩客户/服务器协议。
  不要害怕时应用的第一个版本不能完美地移植,在你解决问题时,你总是可以在以后优化它。
  
  九、优化MySQL
  挑选编译器和编译选项。
  位你的系统寻找最好的启动选项。
  通读MySQL参考手册并阅读Paul DuBios的《MySQL》一书。(已有中文版-译注)
  多用EXPLAIN SELECT、SHOW VARIABLES、SHOW STATUS和SHOW PROCESSLIST。
  了解查询优化器的工作原理。
  优化表的格式。
  维护你的表(myisamchk、CHECK TABLE、 OPTIMIZE TABLE)
  使用MySQL的扩展功能以让一切快速完成。
  如果你注意到了你将在很多场合需要某些函数,编写MySQL UDF函数。
  不要使用表级或列级的GRANT,除非你确实需要。
  购买MySQL技术支持以帮助你解决问题:)
  
  十、编译和安装MySQL
  通过位你的系统挑选可能最好的编译器,你通常可以获得10-30%的性能提高。
  在Linux/Intel平台上,用pgcc(gcc的奔腾芯片优化版)编译MySQL。然而,二进制代码将只能运行在Intel奔腾CPU上。
  对于一种特定的平台,使用MySQL参考手册上推荐的优化选项。
  一般地,对特定CPU的原生编译器(如Sparc的Sun Workshop)应该比gcc提供更好的性能,但不总是这样。
  用你将使用的字符集编译MySQL。
  静态编译生成mysqld的执行文件(用--with-mysqld-ldflags=all-static)并用strip sql/mysqld整理最终的执行文件。
  注意,既然MySQL不使用C++扩展,不带扩展支持编译MySQL将赢得巨大的性能提高。
  如果操作系统支持原生线程,使用原生线程(而不用mit-pthreads)。
  用MySQL基准测试来测试最终的二进制代码。
  
  十一、维护
  如果可能,偶尔运行一下OPTIMIZE table,这对大量更新的变长行非常重要。
  偶尔用myisamchk -a更新一下表中的键码分布统计。记住在做之前关掉MySQL。
  如果有碎片文件,可能值得将所有文件复制到另一个磁盘上,清除原来的磁盘并拷回文件。
  如果遇到问题,用myisamchk或CHECK table检查表。
  用mysqladmin -i10 precesslist extended-status监控MySQL的状态。
  用MySQL GUI客户程序,你可以在不同的窗口内监控进程列表和状态。
  使用mysqladmin debug获得有关锁定和性能的信息。
  
  十二、优化SQL
  扬SQL之长,其它事情交由应用去做。使用SQL服务器来做:
  
  找出基于WHERE子句的行。
  JOIN表
  GROUP BY
  ORDER BY
  DISTINCT
  不要使用SQL来做:
  
  检验数据(如日期)
  成为一只计算器
  技巧:
  
  明智地使用键码。
  键码适合搜索,但不适合索引列的插入/更新。
  保持数据为数据库第三范式,但不要担心冗余信息或这如果你需要更快的速度,创建总结表。
  在大表上不做GROUP BY,相反创建大表的总结表并查询它。
  UPDATE table set count=count+1 where key_column=constant非常快。
  对于大表,或许最好偶尔生成总结表而不是一直保持总结表。
  充分利用INSERT的默认值。
  
  十三、不同SQL服务器的速度差别(以秒计)
  通过键码读取2000000行: NT Linux
  mysql 367 249
  mysql_odbc 464
  db2_odbc 1206
  informix_odbc 121126
  ms-sql_odbc 1634
  oracle_odbc 20800
  solid_odbc 877
  sybase_odbc 17614
  
  插入350768行: NT Linux
  mysql 381 206
  mysql_odbc 6

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
What are stored procedures in MySQL?What are stored procedures in MySQL?May 01, 2025 am 12:27 AM

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

How does query caching work in MySQL?How does query caching work in MySQL?May 01, 2025 am 12:26 AM

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

What are the advantages of using MySQL over other relational databases?What are the advantages of using MySQL over other relational databases?May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

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

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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