Home Database Mysql Tutorial 给DBA支招:让数据库变快的10个建议_MySQL

给DBA支招:让数据库变快的10个建议_MySQL

Jun 01, 2016 pm 01:49 PM
Skill database article user

bitsCN.com

  大多数网站的内容都存在数据库里,用户通过请求来访问内容。数据库非常的快,有许多技巧能让你优化数据库的速度,使你不浪费服务器的资源。在这篇文章中,收录了十个优化数据库速度的技巧。

  0. 小心设计数据库

  第一个技巧也许看来理所当然,但事实上大部分数据库的问题都来自于设计不好的数据库结构。

  譬如我曾经遇见过将客户端信息和支付信息储存在同一个数据库列中的例子。对于系统和用数据库的开发者来说,这很糟糕。

  新建数据库时,应当将信息储存在不同的表里,采用标准的命名方式,并采用主键。

  1. 清楚你需要优化的地方

  如果你想优化某个查询语句,清楚的知道这个语句的结果是非常有帮助的。采用EXPLAIN语句,你将获得很多有用的信息,下面来看个例子:

  EXPLAIN SELECT * FROM ref_table,other_table WHERE ref_table.key_column=other_table.column;

  2. 最快的查询语句…是那些你没发送的语句

  每次你向数据库发送一条语句,你都会用掉很多服务器资源。所以在很高流量的网站中,最好的方法是将你的查询语句缓存起来。

  有许多种缓存语句的方法,下面列出了几个:

  AdoDB: AdoDB是一个PHP的数据库简化库。使用它,你可以选用不同的数据库系统(MySQL, PostGreSQL, Interbase等等),而且它就是为了速度而设计的。AdoDB提供了简单但强大的缓存系统。还有,AdoDB拥有BSD许可,你可以在你的项目中免费使用它。对于商业化的项目,它也有LGPL许可。

  Memcached:Memcached是一种分布式内存缓存系统,它可以减轻数据库的负载,来加速基于动态数据库的网站。

  CSQL Cache: CSQL缓存是一个开源的数据缓存架构。我没有试过它,但它看起来非常的棒。

  3. 不要select你不需要的

  获取想要的数据,一种非常常见的方式就是采用*字符,这会列出所有的列。

  SELECT * FROM wp_posts;

  然而,你应该仅列出你需要的列,如下所示。如果在一个非常小型的网站,譬如,一分钟一个用户访问,可能没有什么分别。然而如果像Cats Who Code这样大流量的网站,这就为数据库省了很多事。

  SELECT title, excerpt, author FROM wp_posts;

  4. 采用LIMIT

  仅获得某个特定行数的数据是非常常见的。譬如博客每页只显示十篇文章。这时,你应该使用LIMIT,来限定你想选定的数据的行数。

  如果没有LIMIT,表有100,000行数据,你将会遍历所有的行数,这对于服务器来说是不必要的负担。

  SELECT title, excerpt, author FROM wp_posts LIMIT 10;

  5. 避免循环中的查询

  当在PHP中使用SQL时,可以将SQL放在循环语句中。但这么做给你的数据库增加了负担。

  下面的例子说明了“在循环语句中嵌套查询语句”的问题:

  foreach ($display_order as $id => $ordinal) {

  $sql = "UPDATE categories SET display_order = $ordinal WHERE id = $id";

  mysql_query($sql);

  }

  你可以这么做:

  UPDATE categories

  SET display_order = CASE id

  WHEN 1 THEN 3

  WHEN 2 THEN 4

  WHEN 3 THEN 5

  END

  WHERE id IN (1,2,3)

  6. 采用join来替换子查询

  程序员可能会喜欢用子查询,甚至滥用。下面的子查询非常有用:

  SELECT a.id,

  (SELECT MAX(created)

  FROM posts

  WHERE author_id = a.id)

  AS latest_post FROM authors a

  虽然子查询很有用,但join语句可以替换它,join语句执行起来更快。

  SELECT a.id, MAX(p.created) AS latest_post

  FROM authors a

  INNER JOIN posts p

  ON (a.id = p.author_id)

  GROUP BY a.id

  7. 小心使用通配符

  通配符非常好用,在搜索数据的时候可以用通配符来代替一个或多个字符。我不是说不能用,而是,应该小心使用,并且不要使用全词通配符(full wildcard),前缀通配符或后置通配符可以完成相同的任务。

  事实上,在百万数量级的数据上采用全词通配符来搜索会让你的数据库当机。

  #Full wildcard

  SELECT * FROM TABLE WHERE COLUMN LIKE '%hello%';

  #Postfix wildcard

  SELECT * FROM TABLE WHERE COLUMN LIKE 'hello%';

  #Prefix wildcard

  SELECT * FROM TABLE WHERE COLUMN LIKE '%hello';

  8. 采用UNION来代替OR

  下面的例子采用OR语句来:

  SELECT * FROM a, b WHERE a.p = b.q or a.x = b.y;

  UNION语句,你可以将2个或更多select语句的结果拼在一起。下面的例子返回的结果同上面的一样,但是速度要快些:

  SELECT * FROM a, b WHERE a.p = b.q UNION

  SELECT * FROM a, b WHERE a.x = b.y

  9. 使用索引

  数据库索引和你在图书馆中见到的索引类似:能让你更快速的获取想要的信息,正如图书馆中的索引能让读者更快的找到想要的书一样。

  可以在一个列上创建索引,也可以在多个列上创建。索引是一种数据结构,它将表中的一列或多列的值以特定的顺序组织起来。

  下面的语句在Product表的Model列上创建索引。这个索引的名字叫作idxModel

  CREATE INDEX idxModel ON Product (Model);

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

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
1243
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

PHP connections to different databases: MySQL, PostgreSQL, Oracle and more PHP connections to different databases: MySQL, PostgreSQL, Oracle and more Jun 01, 2024 pm 03:02 PM

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles