简体   繁体   English

什么更快; 包括另一个文件或在PHP中查询MySQL数据库?

[英]What's quicker; including another file or querying a MySQL database in PHP?

In PHP, which is quicker; 在PHP中,它更快; using include('somefile.php') or querying a MySQL database with a simple SELECT query to get the same information? 使用include('somefile.php')或使用简单的SELECT查询查询MySQL数据库以获取相同的信息?

For example, say you had a JavaScript autocomplete search field which needed 3,000 terms to match against. 例如,假设您有一个JavaScript自动填充搜索字段,需要3,000个术语才能匹配。 Is it quicker to read those terms in from another file using include or to read them from a MySQL database using a simple SELECT query? 使用include从另一个文件读取这些术语是否更快或使用简单的SELECT查询从MySQL数据库中读取它们?

Edit: This is assuming that the database and the file I want to include are on the same local machine as my code. 编辑:这假设我想要包含的数据库和文件与我的代码在同一台本地机器上。

It depends. 这取决于。 If your file is stored locally in your server and the database is installed in another machine, then the faster is to include the file. 如果您的文件存储在服务器本地,并且数据库安装在另一台计算机上,则包含该文件的速度越快。

Buuuuut, because it depends on your system it could be not true. Buuuuut,因为它取决于你的系统,它可能不是真的。 I suggest to you to make a PHP test script and run it 100 times from the command line, and repeat the test through HTTP (using cURL) 我建议你制作一个PHP测试脚本并从命令行运行100次,然后通过HTTP重复测试(使用cURL)

Example: 例:

use_include.php use_include.php

<?php

  start = microtime(true);

  include( 'somefile.php' );

  echo microtime(true)-start;

?>

use_myphp.php use_myphp.php

<?php

  start = microtime(true);

  __put_here_your_mysql_statements_to_retrieve_the_file__

  echo microtime(true)-start;

?>

Including a file should almost always be quicker. 包含文件应该几乎总是更快。 If your database is on another machine (eg in shared hosting) or in a multi-server setup the lookup will have to make an extra hop. 如果您的数据库位于另一台计算机上(例如,在共享主机中)或多服务器设置中,则查找将需要进行额外的跃点。

However, in practice the difference is probably not going to matter. 然而,在实践中,差异可能并不重要。 If the list is dynamic then storing it in MySQL will make your life easier. 如果列表是动态的,那么将其存储在MySQL中将使您的生活更轻松。 Static lists (eg countries or states) can be stored in a PHP include. 静态列表(例如国家或州)可以存储在PHP包含中。 If the list is quite short (a few hundred entries) and often used, you could load it straight into JavaScript and do away with AJAX. 如果列表很短(几百个条目)并且经常使用,你可以将它直接加载到JavaScript中并取消AJAX。

If you are going the MySQL route and are worried about speed then use caching. 如果您要使用MySQL路由并担心速度,请使用缓存。

$query = $_GET['query'];
$key = 'query' . $query;
if (!$results = apc_fetch($key))
{ 
    $statement = $db->prepare("SELECT name FROM list WHERE name LIKE :query");
    $statement->bindValue(':query', "$query%");
    $statement->execute();
    $results = $statement->fetchAll();
    apc_store($key, $results);
}

echo json_encode($results);

The difference in time is more down to the system design than the underlying technique I'd dare say. 时间的差异更多地取决于系统设计,而不是我敢说的基础技术。 Both a MySQL result and a file can be cached in memory, and the performance difference there would be so small it is neglectable. MySQL结果和文件都可以缓存在内存中,而那里的性能差异很小,可以忽略不计。

Instead I would ask myself what the difference in maintenance would be. 相反,我会问自己维护的差异是什么。 Are you likely to ever change the data? 您是否可能更改数据? If not, just pop it in a plain file. 如果没有,只需将其弹出一个普通文件即可。 Are you likely to change bits of the content ever so often? 您是否可能经常更改内容的位? If so, a database is way easier to manipulate. 如果是这样,数据库更容易操作。 Same thing for the structure of the data, if it needs "restructuring", maybe it is more efficient to put it in a database? 对于数据结构也是如此,如果需要“重组”,将数据放入数据库可能更有效率吗?

So: Do what you feel is most convenient for you and the future maintainer of the code and data. 所以:做你认为最方便的代码和数据的未来维护者。 :-) :-)

It's very hard/impossible to give an exact answer, as there are too many unknown variables - what if the filesystem is mounted on an NFS that resides on the other side of the world? 很难/不可能给出一个确切的答案,因为有太多的未知变量 - 如果文件系统安装在位于世界另一端的NFS上会怎么样? Or you have the whole MySQL database in memory. 或者你有整个MySQL数据库在内存中。 The size of the database should be factored in too. 还应考虑数据库的大小。

But, on a more answer-y note, a safe guess would be that MySQL is faster , given good indexes, good database structure/normalization and not too fancy/complex queries. 但是,在一个更回答的问题上, 一个安全的猜测是MySQL会更快 ,给出好的索引,良好的数据库结构/规范化,而不是太花哨/复杂的查询。 I/O operations are always expensive (read: slow), while, as previously mentioned, the whole dataset is already cached in memory by MySQL. I / O操作总是很昂贵(读取:慢),而如前所述,整个数据集已经被MySQL缓存在内存中。

Besides, I imagine you thought of doing further string manipulation with those included files, which makes things even more troublesome - I'm convinced MySQL's string searching algorithms are much better optimized than what you could come up with in PHP. 此外,我想你想用这些包含的文件进行进一步的字符串操作,这会让事情变得更加麻烦 - 我确信MySQL的字符串搜索算法比你在PHP中提出的要好得多。

如果这是你定期获取的东西,那么预取数据(从磁盘或数据库,无关紧要)可能是值得的,并让你的脚本从像RAMcached这样的RAM缓存中提取它。

肯定包括只要文件不是太大而你最终使用太多内存,在这种情况下建议使用数据库

Reading in raw data to a script from a file will generally be faster than from a database. 从文件中读取原始数据到脚本通常比从数据库中读取更快。

However it sounds like you are wanting to query that data in order to find a match to return to the javascript. 但是,听起来您想要查询该数据以便找到匹配以返回到javascript。 You may find in that case that MySQL will be faster for the actual querying/searching of the data (especially if correctly indexed etc.) as this is something a database is good at. 在这种情况下,您可能会发现MySQL对于数据的实际查询/搜索会更快(特别是如果正确索引等),因为这是数据库擅长的。

Reading in a big file is also less scalable as you will be using lots of server memory while the script executes. 读取大文件的可伸缩性也较低,因为在脚本执行时将使用大量服务器内存。

Why not do it both ways and see which is faster? 为什么不两种方式做,看哪哪个更快? Both solutions are pretty trivial. 这两种解决方案都非常简单。

如果您希望术语的数量在以后变得更大,那么最好将MySQL与全文搜索字段一起使用。

I recently had this issue. 我最近有这个问题。 I had some data in mysql that I was querying on every page request. 我在mysql中有一些数据,我在每个页面请求时查询。 For my data set, it was faster to write a fixed record length file than to use MySQL. 对于我的数据集,编写固定记录长度文件比使用MySQL更快。

There were a few different factors that made a file faster than MySQL for me: 有几个不同的因素使我的文件比MySQL更快:

  1. File size was small -- under 100kb of text data 文件大小很小 - 低于100kb的文本数据
  2. I was randomly picking and not searching -- indexes made no difference 我随机挑选而不是搜索 - 索引没有任何区别
  3. Connection time -- opening the file and reading it in was faster than connecting to the database when the server load was high. 连接时间 - 打开文件并将其读入比在服务器负载较高时连接到数据库更快。 This was especially true since the OS cached the file in memory 由于操作系统将文件缓存在内存中,因此尤其如此

Bottom line was that I benchmarked it and compared results. 最重要的是我对它进行了基准测试并比较了结果。 For my workload, the file system was faster. 对于我的工作负载,文件系统更快。 I suspect if my data set ever grows, that will change. 我怀疑如果我的数据集增长,那将会改变。 I'm going to be keeping an eye on performance and I'm ready to change how it works in the future. 我将密切关注性能,并且我已准备好改变其未来的工作方式。

If you use a PHP bytecode cache like APC or Xcache, including the file is likely to be faster. 如果你使用像APC或Xcache这样的PHP字节码缓存,包括文件可能会更快。 If you're using PHP and you want performance, a bytecode cache is absolutely a requirement. 如果您正在使用PHP并且想要性能,则绝​​对需要字节码缓存。

It sounds like you're considering keeping static data around in a PHP script that you include, to avoid hitting the database. 听起来您正在考虑在您包含的PHP脚本中保留静态数据,以避免命中数据库。 You're basically doing a rudimentary cache. 你基本上做了一个基本的缓存。 This can work okay, as long as you have some way to refresh that file if/when the data does change. 这可以正常工作,只要您有一些方法来刷新该文件,如果/当数据确实发生变化时。 You might also look want to learn about the MySQL Query Cache to make SQL queries against static data faster. 您可能还希望了解MySQL查询缓存,以便更快地对静态数据进行SQL查询。 Or Memcached for keeping static data in memory. 或者Memcached用于将静态数据保存在内存中。

I exactly don't know, but in my opinio using MySQL, even if can be slower, sould be used if the content is dynamic. 我完全不知道,但在我的意见中使用MySQL,即使速度较慢,如果内容是动态的,也可以使用。 But I'm pretty sure it is faster, for big contents, using include. 但是我很确定它对于大内容来说更快,使用include。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM