读写文件与读写数据库的效率比较_MySQL
bitsCN.com
这个问题也是最近才想到的,就是到底读文件更快还是读数据库更快,能快多少,天缘也搜索过,没见有网友就这个问题答复过,也可能是太简单的缘故,我们本文还是来实测一下,由于时间关系,VC还没装,天缘先用PHP测试了一下,下次有时间在C/C++上补充测试到本文来,因为PHP的底层解析应该也是基于C的,所以估计两者环境测试结果差不多,小问题大收获,现在就来看一下测试过程及结果。
测试程序如下:
说明1:由于读数据库语句调用简单的封包函数两次,所以把读文件也改成连续调用两次,数据库记录ID为1就在第一条,并且唯一索引。
说明2:测试两次一次是4K数据,一次是整形数据
1 set_time_limit(0); 2 3 function fnGet($filename) 4 5 { 6 7 $content = file_get_contents($filename); 8 9 return $content; 10 11 } 12 13 function fnGetContent($filename) 14 15 { 16 17 $content = fnGet($filename); 18 19 return $content; 20 21 } 22 23 $times=100000; 24 25 echo '数据库查询结果:<br>'; 26 27 //--------------------------------- 28 29 $begin=fnGetMicroTime(); 30 31 for($i=0;$imydb_query("SELECT log_Content FROM blog WHERE log_ID='1'"); 36 37 $row=$dbcon->mydb_fetch_row($res); 38 39 $content=$row[0]; 40 41 } 42 43 echo 'fetch_row '.$times.' 次时间:<font color="red">'.(fnGetMicroTime()-$begin).'</font>秒<br>'; 44 45 //--------------------------------- 46 47 $begin=fnGetMicroTime(); 48 49 for($i=0;$imydb_query("SELECT log_Content FROM blog WHERE log_ID='1'"); 54 55 $row=$dbcon->mydb_fetch_array($res); 56 57 $content=$row['log_Content']; 58 59 } 60 61 echo 'fetch_array '.$times.' 次时间:<font color="red">'.(fnGetMicroTime()-$begin).'</font>秒<br>'; 62 63 //--------------------------------- 64 65 $begin=fnGetMicroTime(); 66 67 for($i=0;$imydb_query("SELECT log_Content FROM blog WHERE log_ID='1'"); 72 73 $row=$dbcon->mydb_fetch_object($res); 74 75 $content=$row->log_Content; 76 77 } 78 79 echo 'fetch_object '.$times.' 次时间:<font color="red">'.(fnGetMicroTime()-$begin).'</font>秒<br>'; 80 81 //--------------------------------- 82 83 $dbcon->mydb_free_results(); 84 85 $dbcon->mydb_disconnect(); 86 87 fnWriteCache('test.txt',$content); 88 89 echo '直接读文件测试结果:<br>'; 90 91 //--------------------------------- 92 93 $begin=fnGetMicroTime(); 94 95 for($i=0;$i'.(fnGetMicroTime()-$begin).'秒<br>';104 105 //---------------------------------106 107 $begin=fnGetMicroTime();108 109 for($i=0;$i'.(fnGetMicroTime()-$begin).'秒<br>';
4K大小数据的查询结果:
fetch_row 100000 次时间:16.737720012665秒
fetch_array 100000 次时间:16.661195993423秒
fetch_object 100000 次时间:16.775065898895秒
直接读文件测试结果:
file_get_contents直接读100000次时间:5.4631857872009秒
fopen直接读100000次时间:11.463611125946秒
整形ID查询结果:
fetch_row 100000 次时间:12.812072038651秒
fetch_array 100000 次时间:12.667390108109秒
fetch_object 100000 次时间:12.988099098206秒
直接读文件测试结果:
file_get_contents直接读100000次时间:5.6616430282593秒
fopen直接读100000次时间:11.542816877365秒
测试结论:
1、直接读文件相比数据库查询效率更胜一筹,而且文中还没算上连接和断开的时间。
2、一次读取的内容越大,直接读文件的优势会越明显(读文件时间都是小幅增长,这跟文件存储的连续性和簇大小等有关系),这个结果恰恰跟天缘预料的相反,说明MYSQL对更大文件读取可能又附加了某些操作(两次时间增长了近30%),如果只是单纯的赋值转换应该是差异偏小才对。
3、写文件和INSERT几乎不用测试就可以推测出,数据库效率只会更差。
4、很小的配置文件如果不需要使用到数据库特性,更加适合放到独立文件里存取,无需单独创建数据表或记录,很大的文件比如图片、音乐等采用文件存储更为方便,只把路径或缩略图等索引信息放到数据库里更合理一些。
5、PHP上如果只是读文件,file_get_contents比fopen、fclose更有效率,不包括判断存在这个函数时间会少3秒左右。
6、fetch_row和fetch_object应该是从fetch_array转换而来的,我没看过PHP的源码,单从执行上就可以说明fetch_array效率更高,这跟网上的说法似乎相反。
实际上在做这个试验之前,从个人经验判断就有了大概的结果,测试完成后则有种豁然开朗的感觉。假定在程序效率和关键过程相当且不计入缓存等措施的条件下,读写任何类型的数据都没有直接操作文件来的快,不论MSYQL过程如何,最后都要到磁盘上去读这个“文件”(记录存储区等效),所以当然这一切的前提是只读内容,无关任何排序或查找操作。
转载于:PHP程序猿的笔记 http://www.songchaoke.cn
bitsCN.com
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

PHP is a back-end programming language widely used in website development. It has powerful database operation functions and is often used to interact with databases such as MySQL. However, due to the complexity of Chinese character encoding, problems often arise when dealing with Chinese garbled characters in the database. This article will introduce the skills and practices of PHP in handling Chinese garbled characters in databases, including common causes of garbled characters, solutions and specific code examples. Common reasons for garbled characters are incorrect database character set settings: the correct character set needs to be selected when creating the database, such as utf8 or u

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my
