Home Database Mysql Tutorial 读写文件与读写数据库的效率比较_MySQL

读写文件与读写数据库的效率比较_MySQL

Jun 01, 2016 pm 01:29 PM
database

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>';
Copy after login

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

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

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

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.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

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.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

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())

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

See all articles