Is PHP fast to read the database in template traversal or to read html from files?

Source: Internet
Author: User
Previously, I was working on a mall website. one module was to customize the homepage, so I could extract the html content of the page and use php to write it into the file. when I read the homepage, I read the file and output it to the template. Reading the database of a question refers to reading custom data, not html content. I am not in a hurry now. I want to know... previously, I was working on a mall website. one module was to customize the homepage, so I could extract the html content of the page and use php to write it into the file. when I read the homepage, I read the file and output it to the template. Reading the database of a question refers to reading custom data, not html content.

I am not in a hurry. I want to know:

  1. If multiple users have high access concurrency, is reading files faster? Or is it faster to read data from the database?

  2. If many files are written, is the reading time slow when the files are large?

  3. The TP framework I used is directly in the template. Will it be better?

  4. How can I test the concurrency? What keywords are to be searched?

Ps: I just got out of work. please give me some advice.

Reply content:

Previously, I was working on a mall website. one module was to customize the homepage, so I could extract the html content of the page and use php to write it into the file. when I read the homepage, I read the file and output it to the template. Reading the database of a question refers to reading custom data, not html content.

I am not in a hurry. I want to know:

  1. If multiple users have high access concurrency, is reading files faster? Or is it faster to read data from the database?

  2. If many files are written, is the reading time slow when the files are large?

  3. The TP framework I used is directly in the template. Will it be better?

  4. How can I test the concurrency? What keywords are to be searched?

Ps: I just got out of work. please give me some advice.

  1. Database data also exists in files, without considering the memory cache of the database, reading files alone is certainly faster than reading the database, because the database must go through the query process and other processing procedures. However, if there are too many files, you also need to consider the file system query speed. this speed is slower than the database query speed when there are too many files.

  2. The file read time depends on the file size, but if all the content in the file is what you want, this time is naturally indispensable. If you only want to read part of the file content, you can use seek to move the file pointer.

  3. The template of the TP framework is compiled. that is to say, in actual execution, the template file you wrote is not used, but the compiled template is used, so you can safely use include, do not worry about performance issues.

  4. Concurrency testing should be called stress testing. you can simply search for a stress testing solution.

For your problem,

  1. In the case of high concurrency, direct databases will certainly be slow. at least there is a Cache layer on the database, and the Cache layer efficiency:

    File <Memory (memcache, redis) <Cache array file is also a type of Cache

    It seems that you still need to generate static files. Here are some of your keywords:

    • ob_start

    • Pseudo-static

    • CDN

  2. If many files are written, there is no way to do this. we generally use Cache and so on to do the overall architecture scheme, rather than simply writing this simple.

    If the read File (especially the php file) is large, enable OpCache to increase the speed.

  3. You include a template file, that is, executed by PHP.include, Equivalent to PHPinclude.
    If all the static files on the Accessed page need to be embedded into the sub-template, SSI (Apache,nginX) Will be better than PHP'sincludeFaster

    Therefore, the answer to your question is: HTML is the fastest, and PHP is not required, but must be generated in advance.

  4. Concurrency testing, from keywordsapache benchmarkFirst, you will find a lot of content you want.

First, they have little difference in speed, and files are faster, but if they are all stored in the database, it is better to manage them.

Second, who is the most important? These CMS-type websites can be optimized through static. after static, the page generation time is irrelevant.

First, memory> File
Second, the database also stores data in files (of course, the database has a query cache)
Non-relational data, of course, saves files fast
However, a large number of files cannot be stored in the same folder (addressing is slow). you can use the file name directory to separate the files, for example:
File name: dsaferdfsasxfsfsdf. dat
Take the first two characters to create a level-1 directory and store it
Ds/dsaferdfsasxfsfsdf. dat
Lokljljoiomlkml. dat> lo/lokljljoiomlkml. dat

The simple cache method S () provided by TP is the file driver by default. if Memcahced or Redis is used as the cache driver, it is faster than the file, provided that Memached or Redis server is on the local machine, or in a local area network over 1 gigabit

For concurrent testing, the Linux and Darwin (OS x) kernel systems can use the AB command, such:
AB-k-n 1000-c 100 "http://www.baidu.com"
// Initiate 1000 requests with a concurrency of 100 each time (this value has an upper limit. the default value is 256 depending on the system settings)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.