Home  >  Article  >  Backend Development  >  How to change php website performance

How to change php website performance

藏色散人
藏色散人Original
2022-10-25 09:28:271099browse

Methods to change the performance of the PHP website: 1. Staticize the HTML; 2. Separate the image server; 3. Perform database clustering and table hashing; 4. Set up cache; 5. Build a mirror; 6. Use load balancing technology; 7. Optimize code writing, etc.

How to change php website performance

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to change the performance of php website?

Large PHP website performance and concurrent access optimization solution

Website performance optimization is very important for large websites. The access opening speed of a website affects users In terms of experience, slow website access speed will cause a high bounce rate, which is easy to solve for small websites. However, for large websites, due to many columns and large pictures and images, how to optimize the overall performance?

1. Strategies for improving performance of large websites

1. HTML static

In fact, everyone knows that the most efficient and least consuming method is pure static HTML pages, so we try our best to use static pages for the pages on our website. 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 need to separate images from pages. Separation is a strategy that is 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 can ensure that the system does not crash due to image problems. On the application server and image server, different configuration optimizations can be performed. For example, when configuring ContentType, apache can support as few LoadModules as possible 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 quickly increase. It can be seen that one database will soon be unable to satisfy 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 a commonly used 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 disperse a certain page or function into smaller databases according to a certain strategy. 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 works 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. Regarding caching in architecture, 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 response capabilities.

For caching in website program development, Memory Cache provided on Linux is a commonly used caching interface that can be used in web development. For example, when developing in Java, you can call MemoryCache to cache and share 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 differences in user access speeds caused by different network access providers and regions. , for example, the differences between ChinaNet and EduNet have 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.

2. PHP code writing optimization:

1. Echo is much faster than print.

Both methods will print something 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. Be sure to use single quotes instead of double quotes for long paragraph strings.

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 do not 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 a file in a relative path, you will search it 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. The congruent symbol === is faster than the 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 congruent 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 ways for thinkphp

1. Turn off the debugging mode

Because after turning off the debugging mode, the system will automatically generate project compilation Cache and turn off log writing, which can reduce a lot of IO loading and log writing overhead.

2. Turn on page compression output

Starting from version 3.1, the OUTPUT_ENCODE configuration parameter has been 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 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. It can usually increase your page generation rate 2 to 5 times, reducing server load. Alternative PHP Cache (APC) is an open source cache tool effective for PHP, which can cache the PHP intermediate code of opcode.

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 you 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

Taking MySQL as an example, it includes 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 because it reduces access to the hard drive. If a table only has a few columns (such as a 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 a smaller TINYINT. If you don't need to record 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

​ ​ ​ ​ ​ ​ ​ ​ ​ ​

3. Add an index to the search field

### ​ ​ ​ ​ ​ ​ ​ ​ ​ ######3. Indexing 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 * The more data is read from the database, the slower the query will become. ###

And, 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 special 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.

Moreover, fixed-length tables are also 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.

8. Use the "vertical split" technology

You can split your table into two, one with a fixed length and one with a variable length. Vertical splitting "Vertical splitting" is a method of turning a table in a database into several tables by columns, which can reduce the complexity of the table and the number of fields, thereby achieving 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 rewrite this field frequently. So, why not put it in another table? This will make your table have better performance. Think about it, a lot of times, for the user table, I only have the user ID, user name, and password. , user roles, etc. will be used frequently. Smaller tables will always have better performance. In addition, you need to pay attention to the fact that you will not frequently join the tables formed by these separated fields. Otherwise, the performance will be worse than when not divided, and it will be extreme. level decline.

9. EXPLAIN your SELECT query

Using the EXPLAIN keyword can let you know how MySQL processes 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.

5. Front-end optimization

After optimizing the back-end and database, the next thing we need to do is to optimize your front-end page and resource files for the output page. Mainly includes optimization of images, JS and style files. It is recommended to use the following web page performance testing tools for detection and analysis, and relevant optimization suggestions will be given:

PageSpeed ​​Tools developed by Google

Developers can use PageSpeed ​​to evaluate the performance of their web pages and Get suggestions on how to improve performance.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to change php website performance. For more information, please follow other related articles on the PHP Chinese website!

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