Home php教程 php手册 加快php mysql必须掌握10条经验

加快php mysql必须掌握10条经验

Jun 06, 2016 pm 07:56 PM
mysql php accelerate master experience

大多数网站的内容都存在数据库里,用户通过请求来访问内容。数据库非常的快,有许多技巧能让你优化数据库的速度,使你不浪费服务器的资源。在这篇文章中,我收录了十个优化数据库速度的技巧。 1、小心设计数据库 第一个技巧也许看来理所当然,但事实上大部分

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

    1、小心设计数据库

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

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

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

    2、清楚你需要优化的地方

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

    3、最快的查询语句…是那些你没发送的语句

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

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

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

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

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

    4、不要select你不需要的

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

    SELECT * FROM wp_posts;

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

    SELECT title, excerpt, author FROM wp_posts;

    5、采用LIMIT

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

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

    SELECT title, excerpt, author FROM wp_posts LIMIT 10;

    6、避免循环中的查询

    当在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)

    7、采用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

    8、小心使用通配符

    通配符非常好用,在搜索数据的时候可以用通配符来代替一个或多个字符。我不是说不能用,而是,应该小心使用,并且不要使用全词通配符(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';

    9、采用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

    10、使用索引

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

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

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

加快php mysql必须掌握10条经验

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 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
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)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

Understand ACID properties: The pillars of a reliable database Understand ACID properties: The pillars of a reliable database Apr 08, 2025 pm 06:33 PM

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

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.

How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

See all articles