Large php site performance and concurrent access optimization

Website performance optimization is very important for large sites, visit a website opening speed affect the user experience, site visits slow will cause a high bounce rate, a good solution to small sites, that for a large section of the site due to the many photographs and images They are relatively large, then how to optimize the overall performance of it? This article provides a large php site performance and concurrent access optimization solution for you.

 

First, the large sites improve performance strategies:

Large sites, such as portals, in the face of a large number of user access, high concurrent requests, the basic solution focused on several aspects of this: the use of high-performance servers, high-performance database, efficient programming language, as well as high Web container properties. These Solutions mean greater investment to a certain extent.

the container is a web service program, there is a port on the server provide a service program, and this program is to process requests from a client, such as the JAVA Tomcat container, ASP is IIS or PWS such containers are . A plurality of containers can be a server.

1, HTML static

In fact, we all know, the highest efficiency, consumption is the smallest of pure static html page, so we try to make the pages on our site using static pages to realize that the easiest way is actually the most effective method.

2, image server separation

As you know, a Web server, whether it is Apache, IIS or other containers, the picture is the most resource-consuming, so we need to be separated from the picture page, which is basically a large site will adopt a strategy, they have independent, even many image server platform. This architecture provides a server system can reduce the pressure of the page access request, and can ensure that the system will not collapse picture problems.

On the application server and image server, different configurations can be optimized, for example, can be as little as apache configuration ContentType of support at the time, as little as possible of LoadModule, to ensure higher system consumption and efficiency.

3, database clustering, database table hash

Large sites are complex applications, these applications must use the database, then in the face of heavily accessed, the database bottlenecks will be revealed soon, then a database will soon be unable to meet the application, so we need to use the database cluster hash table or library.

In terms of database cluster, many databases have their own solutions, Master / Slave popular MySQL also provide a similar program.

Clusters typically use CDN and GSBL with DNS load balancing technology, a group of front-end servers in each region group, for example: Netease, Baidu uses DNS load balancing technology, each channel is a set of front-end servers, a search using the DNS load technology, All channels share a foreground cluster of servers.

Library table hash is commonly used and most effective solution.

We installed the application and business application or database to separate functional modules, different modules corresponding to different databases or tables, and then according to certain strategies smaller hash database on a page or function, such as user tables, carried out in accordance with a hash table user ID, so that the performance of the lift system at low cost and has good scalability.

sohu the forum is to use such a structure, the user forums, settings, and other information in a database separate posts, and then posts the user to hash database and tables in accordance with the plate and ID, ultimately a simple configuration in a configuration file it allows the system at any time adding a low-cost supplement comes in database system performance.

4, Cache

The term techno cache are exposed to a lot of places to use the cache. Website architecture and web development in the cache is also very important. Here to tell the two basic cache. Advanced and distributed cache in the back about.

Cache architecture, to the Apache people can know more familiar with Apache provides its own cache module, you can use the additional Squid module cache, either way can effectively improve Apache's access responsiveness.

Web application development aspects of the cache, available on Linux Memory Cache is a common cache interface, can be used in web development, such as in Java when you can call MemoryCache some data caching and communication sharing, some large community use this architecture. In addition, when using the web language development, basic language variety has its own cache modules and method, PHP Pear has a Cache module, Java even more, .net is not very familiar with, I believe it is definitely there.

5, mirror

Mirroring is a way to improve safety and performance data often used in large sites, mirroring technologies can address user access speed differences and geographical bring network access providers, such as the difference between ChinaNet and EduNet it prompted a lot of sites in set up mirror sites, the data within the education network timing update or live update.

6, load balancing

Load balancing is a large site will address the high load and high-end access a large number of concurrent requests using the solution.

Load balancing technology developed over the years, there are many professional service providers and products to choose from, I personally contacted a number of solutions, two of which can give you a reference architecture

Two, PHP coding optimization:

1. echo much faster than print.

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

2. include_once more time-consuming than include.

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

3. For a long string paragraph must use single quotes instead of double quotes.

Because the double quotation marks to search for a string variable. For example: echo 'This is long string' $ name than the echo "This is long string $ name" is much faster.

4. Do not use nested for recycle loop  

5. If the function can be defined as static

Then do not become a member function is defined, static function 33% faster than member functions.  

6. Regular expressions can solve the problem if you can not pass

Then do not use regular. Regular expressions than PHP native function slower.

For example, str_replace substituted preg_replae. 

7. Do not use relative paths to include files

Find the relative file path will now find the current directory, and then turn again to find. Find this file is very slow. The best is to define such constants WEB_ROOT, then use this constant to include files.  

8. congruent faster than symbol equal == ===

And if (1 == '1') returns true, if (0 == ") also returns true, and when you use congruent symbols if (1 === '1'), and if (0 = == ") returns false. So when you need to detect a number of Boolean variables in the program when it is best to use congruent symbols.  

Third, there are several ways for thinkphp

1. Turn off the debug mode

Because after closing the debug mode, the system automatically generates a project to compile and close the log buffer is written, thus reducing the overhead of a lot of IO load and log writes.

2. Turn the page output compression

3.1 version, adds OUTPUT_ENCODE configuration parameters for controlling page output compression.

3. Turn Cache

In the APC Web site deployment environment installation or Xcache cache can effectively improve operational performance and memory footprint website

XCache is an open source opcode cache / optimizer, which means that he can improve the performance of PHP on your server. He passed the buffered data compiled PHP shared memory in order to avoid duplication of the compilation process, it has been able to directly use the buffer thereby increasing the speed of the compiled code. typically can improve your page generation rate of 2 to 5 times, reduce server load.

Alternative PHP Cache (APC) is an effective open for PHP cache storage means, it is possible to cache the opcode php midamble.

4. Cache Fields

By default, the cache is automatically generated field, after completion of the development, substantially changes the database becomes relatively small, and therefore may be considered to merge field buffer corresponding model class, so that each reading can be reduced overhead field buffer IO . The combined method is to find the corresponding fields in the cache file following Runtime / Data / _fields

Fourth, database optimization

1, select the correct storage engine

To MySQL, for example, there are two storage engines including MyISAM and InnoDB, both advantages and disadvantages for each engine.

MyISAM suitable for some applications that require a large number of queries. InnoDB trend would be a very complicated storage engine, for some small applications, it will be slower than MyISAM. But it supports "row lock" to support the transaction.

2, to optimize the data type of the field

As a rule, the smaller the column faster. For most database engines, disk operation is probably the most significant bottleneck. So, your data will become compact and this is very helpful, because it reduces the access to the hard disk.

If you only have a few columns of a table Bale (such as dictionary table, the configuration table), then we have no reason to call the shots using the INT key, use MEDIUMINT, SMALLINT TINYINT or less will be more economical. If you do not record time, using DATETIME better than DATE. Of course, you also need to leave enough enough room for expansion.

3, add an index for the search field

The index is not necessarily a primary key or unique field. If your table, you always have to have a field often used to do a search, then it is better indexing it, unless you want to search the fields are large text fields, it should be full-text indexed.

4, avoid the use of Select * read more data from the database,

Then the query becomes slower. And, if your database server and WEB server are two separate servers, then it will increase the load on the network. Even if all the fields you want to query the data table, but also try not to use the * wildcard, use the built-in field provides troubleshooting definitions may give more convenience.

5, instead of using ENUM VARCHAR

ENUM type is very fast and compact. In fact, its preservation is TINYINT, but its appearance on the show as a string. As a result, use this field to do some of the options list becomes quite perfect. For example, the values ​​of these fields like gender, ethnicity, and state departments of limited and fixed, then you should use ENUM instead of VARCHAR.

6, as much as possible to use NOT NULL

Unless you have a very special reason to use a NULL value, you should always keep your field holds NOT NULL. NULL actually need extra space, and when you compare, your program will be more complex. Of course, this does not mean that you can not use NULL, the reality is very complex, still in some cases, you need to use a NULL value.

7, the fixed length table will be faster

If all the fields in the table are "fixed length", the entire table is considered a "static" or "fixed-length". For example, the following table is no type of field: VARCHAR, TEXT, BLOB. As long as you include one of these fields, then the table is not a "fixed-length static table", so, MySQL engine will use another method to deal with.

Fixed length table will improve performance, because MySQL search will get faster, because the fixed length is easy to calculate the next offset in the data, it will naturally be read quickly. And if the field is not a fixed length, so every time to find the next one, then the program needs to find the primary key.

And, fixed length tables are also more likely to be cached and reconstruction. However, the only side effect is that fixed-length fields will waste some space, because the fixed-length field whether you use do not, he is to be allocated so much space.

Use "vertical split" technique, you can split into two your table is a fixed length is a variable length.

8, the vertical split

"Vertical split" is a table in the database by the columns of the table into several methods, so that the number and complexity of the fields of the table can be reduced, so as to achieve optimization purposes.

For example: There is a table in the User field is the home address, the field is optional, since in comparison, and in addition to your personal information, you do not need to read or rewrite this field often when database operations. So why not put him in another table in it? So make your table have a better performance, we think is not a lot when I watch it for the user, only the user ID, user name, password, user roles are often used. Smaller tables always have a good performance.

In addition, you need to note that these tables are divided out form fields, you do not regularly go Join them, otherwise, this performance will be worse than not divided, and that will be the number of poles order reduction.

9, EXPLAIN Your SELECT query;

Use EXPLAIN keyword can let you know how MySQL is handling your SQL statement. This can help you analyze performance bottlenecks in your query or table structure. EXPLAIN query results will also tell you your index primary key is how to use, how your table is searched and sorted ...... and so on, and so on.

Usually we can compare complex particularly with regard to multi-table SELECT statement, the keyword EXPLAIN added to the front. You can use phpmyadmin to do this thing. Details are at Annex explain.doc.

Fifth, the front-end optimization

After optimization and database after the end, we immediately have to do is optimize your resource file for the front page and page output, including optimization of images, JS and style files.

We recommend the following web performance testing tool for testing and analysis, optimization will give relevant recommendations:

Google PageSpeed ​​tools developed

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

yslow YSlow

It can be analyzed pages on the site, and tell you in order to improve site performance, how certain rules-based and optimized.

This article is reproduced in: php Chinese network

 

Guess you like

Origin www.cnblogs.com/lazb/p/11712262.html