Home > Backend Development > PHP Tutorial > Large php website performance and concurrent access optimization solution

Large php website performance and concurrent access optimization solution

PHPz
Release: 2023-03-06 18:14:01
forward
13616 people have browsed it

Website performance optimization is very important for large websites. The opening speed of a website affects the user experience. Slow website access speed will cause high bounce rate, which is easy to solve for small websites. However, for large websites due to many columns, Pictures and images are relatively large, so how to optimize overall performance? This article provides you with a large-scale PHP website performance and concurrent access optimization plan.

Large php website performance and concurrent access optimization solution1. Large-scale website performance improvement strategies:

Large-scale websites, such as portals When a website faces a large number of user visits and high concurrent requests, the basic solutions focus on the following links: using high-performance servers, high-performance databases, high-efficiency programming languages, and high-performance Web containers. These solutions mean greater investment to a certain extent.

The web container is a service program. There is a program that provides corresponding services on a port of the server, and this program processes requests from the client, such as the Tomcat container in JAVA, ASP's IIS or PWS are all such containers. A server can have multiple containers.

1. HTML static

In fact, everyone knows that the most efficient and least consumed is the purely static HTML page, so we try our best to make our website The pages on are implemented using static pages. This simplest method is actually the most effective method.

2. Image server separation

As we all know, for web servers, whether it is Apache, IIS or other containers, images consume the most resources, so we It is necessary to separate images from pages. This is a strategy basically adopted by large websites. They all have independent or even multiple image servers. Such an architecture can reduce the pressure on the server system that provides page access requests and ensure that the system will not crash due to image problems.

On the application server and image server, different configuration optimizations can be performed. For example, apache can support as few LoadModules as possible when configuring ContentType to ensure higher system consumption and execution efficiency.

3. Database cluster, library table hashing

Large websites have complex applications, and these applications must use databases. When faced with a large number of accesses, The bottleneck of the database will soon appear. At this time, one database will soon be unable to meet the application, so we need to use database clustering or database table hashing.

In terms of database clusters, many databases have their own solutions, and the commonly used Master/Slave provided by MySQL is a similar solution.

Clusters usually use CDN, GSBL and DNS load balancing technology. Each region has a front-end server group. For example: NetEase and Baidu use DNS load balancing technology. Each channel has a group of front-end servers. One search Using DNS load technology, all channels share a front-end server cluster.

Library table hashing is the most common and most effective solution.

We install business and application or functional modules in the application to separate the database. Different modules correspond to different databases or tables, and then according to certain strategies, smaller databases are scattered for a certain page or function. Columns, such as user tables, are hashed according to user IDs, which can improve system performance at low cost and have good scalability.

sohu's forum adopts such a structure, which separates the forum's users, settings, posts and other information into the database, and then hashes the posts and users according to the section and ID in the database and table. Finally, it can be configured Simple configuration in the file allows the system to add a low-cost database at any time to supplement system performance.

4. Caching

Anyone who is involved in technology has come across the word cache, and cache is used in many places. Caching in website architecture and website development is also very important. Here we first talk about the two most basic caches. Advanced and distributed caching are described later.

Architecture caching, anyone familiar with Apache will know that Apache provides its own caching module, and you can also use the additional Squid module for caching. Both methods can effectively improve Apache's access. Responsiveness.

Cache in website program development, Memory Cache provided on Linux is a commonly used cache interface, which can be used in web development. For example, when developing in Java, you can call MemoryCache to cache and communicate with some data. , some large communities use such an architecture. In addition, when using web language development, each language basically has its own cache module and method. PHP has Pear's Cache module, and Java has even more. I am not very familiar with .net, but I believe it must be there.

5. Mirroring

Mirroring is a method often used by large websites to improve performance and data security. Mirroring technology can solve the problem of different network access providers and geographical zones. The difference in user access speeds, such as the difference between ChinaNet and EduNet, has prompted many websites to build mirror sites within the education network, and the data is updated regularly or in real time.

6. Load balancing

Load balancing will be a high-end solution for large websites to solve high-load access and a large number of concurrent requests.

Load balancing technology has been developed for many years, and there are many professional service providers and products to choose from. I have personally come across some solutions, and two of them can be used as a reference.

2. PHP code writing optimization:

1. Echo is much faster than print.

Both methods will print things on the page, but echo does not return any value, and print will return 0 or 1 on success or failure.

2. include_once is more time-consuming than include.

Because it needs to check whether the class you want to include has been included. ​

3. For long paragraph strings, single quotes must be used instead of double quotes.

Because double quotes will search for variables in the string. For example: echo ‘This is long string’.$name is much faster than echo ‘This is long string $name’.

4. Do not use for loops nested within loops

5. If the function can be defined as static

Then don’t define it as a member function. Static functions are 33% faster than member functions.​

6. If you can solve the problem without using regular expressions

then don’t use regular expressions. Regular expressions are slower than PHP's native functions.

For example, use str_replace instead of preg_replae.

7. Try not to use relative paths to include files

If you search for files in a relative path, you will search them in the current directory, and then search again in sequence. This makes finding files very slow. It is best to define a constant like WEB_ROOT first, and then use this constant to include the file.

8. Congruent symbols === are faster than equal ==

And if(1 == '1′) will return true, if(0 = = ”) will also return true, while if(1 ==='1′) and if(0===”) will both return false when you use the congruence symbol. So it is best to use the congruence symbol when you need to detect some Boolean variables in your program.

3. There are the following methods for thinkphp

1. Turn off debugging mode

After turning off debugging mode, The system will automatically generate a project compilation cache and turn off log writing, which can reduce a lot of IO loading and log writing overhead.

2. Enable page compression output

Starting from version 3.1, the OUTPUT_ENCODE configuration parameter is added to control page compression output.

3. Turn on caching

Installing APC or Xcache cache in the website deployment environment can effectively improve website performance and memory usage

XCache is an open source Opcode cache/optimizer, which means that it can improve the performance of PHP on your server. It avoids repeated compilation processes by buffering the compiled PHP data into shared memory, and can directly use the buffered compiled code to Increase speed. Typically increases your page generation rate by 2 to 5 times, reducing server load.

Alternative PHP Cache (APC) is an open source cache tool for PHP that can cache Opcode's PHP intermediate code.

4. Field cache

By default, the field cache is automatically generated. After the development is completed, there are basically fewer changes to the database, so it can Consider merging the field cache into the corresponding model class, which can reduce the IO overhead of reading the field cache each time. The method of merging is to find the corresponding field cache file under Runtime/Data/_fields

4. Database optimization

1. Select the correct storage engine

Take MySQL as an example, including two storage engines MyISAM and InnoDB. Each engine has advantages and disadvantages.

MyISAM is suitable for applications that require a large number of queries. The trend of InnoDB will be a very complex storage engine, and for some small applications, it will be slower than MyISAM. But it supports "row locks" and transactions.

2. Optimize the data type of the field

Remember a principle, the smaller the column, the faster it will be. For most database engines, hard disk operations are probably the most significant bottleneck. So, making your data compact can be very helpful in this situation as it reduces access to the hard drive.

If a table only has a few columns (such as dictionary table, configuration table), then we have no reason to use INT as the primary key. It will be more economical to use MEDIUMINT, SMALLINT or smaller TINYINT. Some. If you don't need to keep track of time, it's much better to use DATE than DATETIME. Of course, you also need to leave enough room for expansion.

3. Add an index to the search field

The index does not necessarily mean the primary key or the only field. If there is a field in your table that you will always use for searching, then it is best to index it. Unless the field you want to search is a large text field, then you should create a full-text index.

4. Avoid using Select * to read more data from the database,

Then the query will become slower. Moreover, if your database server and WEB server are two independent servers, this will also increase the load of network transmission. Even if you want to query all fields in the data table, try not to use the * wildcard character. Making good use of the built-in field exclusion definitions may bring more convenience.

5. Use ENUM instead of VARCHAR

The ENUM type is very fast and compact. In fact, it holds a TINYINT, but it appears as a string. In this way, it becomes quite perfect to use this field to make some choice lists. For example, if the values ​​of fields such as gender, ethnicity, department, and status are limited and fixed, you should use ENUM instead of VARCHAR.

6. Use NOT NULL whenever possible

Unless you have a very specific reason to use NULL values, you should always keep your fields NOT NULL . NULL actually requires extra space, and your program will be more complex when you perform comparisons. Of course, this does not mean that you cannot use NULL. The reality is very complicated, and there will still be situations where you need to use NULL values.

7. Fixed-length tables will be faster

If all fields in the table are "fixed length", the entire table will be considered "static" or "fixed-length". For example, there are no fields of the following types in the table: VARCHAR, TEXT, BLOB. As long as you include one of these fields, the table is no longer a "fixed-length static table" and the MySQL engine will process it in another way.

Fixed length tables will improve performance because MySQL will search faster. Because these fixed lengths make it easy to calculate the offset of the next data, reading will naturally be faster. . And if the field is not of fixed length, then every time you want to find the next one, the program needs to find the primary key.

Also, fixed-length tables are easier to cache and rebuild. However, the only side effect is that fixed-length fields waste some space, because fixed-length fields require so much space regardless of whether you use them or not.

Using the "vertical split" technology, you can split your table into two, one with a fixed length and one with a variable length.

8. Vertical split

"Vertical split" is a method of converting the tables in the database into several tables by columns, which can reduce the complexity of the table Degree and number of fields to achieve optimization purposes.

For example: There is a field in the User table that is home address. This field is optional. In comparison, except for personal information when you operate in the database, you do not need to read or read it frequently. It is to rewrite this field. So why not put him in another table? This will give your table better performance. Think about it, a lot of the time, for the user table, only the user ID, user name, password, user role, etc. will be frequently used. Smaller tables will always have better performance.

In addition, you need to pay attention to the fact that you will not join the tables formed by these separated fields frequently. Otherwise, the performance will be worse than when not divided. Moreover, it will be an extremely low level of decline.

9. EXPLAIN your SELECT query;

Using the EXPLAIN keyword can let you know how MySQL handles your SQL statement. This can help you analyze the performance bottlenecks of your query statements or table structures. The query results of EXPLAIN will also tell you how your index primary key is used, how your data table is searched and sorted...etc., etc.

Usually we can add the keyword EXPLAIN to the front of more complex SELECT statements, especially those involving multiple tables. You can use phpmyadmin to do this. For details, see the attachment explain.doc.

5. Front-end optimization

After optimizing the back-end and database, the next thing we need to do is to optimize the output page. The front-end pages and resource files mainly include the optimization of images, JS and style files.

We recommend using the following web page performance testing tools for detection and analysis, and will give relevant optimization suggestions:

PageSpeed ​​tool developed by Google

Webmasters and web developers can use PageSpeed ​​to evaluate the performance of their web pages and get recommendations on how to improve performance.

yslow YSlow

can analyze the pages of the website and tell you how to optimize based on certain rules in order to improve website performance.

Related articles:
Practice of php website performance optimization: Taobao homepage loading speed optimization practice

The above is the detailed content of Large php website performance and concurrent access optimization solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template