数据库命令在我的 PHP 脚本中运行缓慢,在 phpMyAdmin 中运行得很快

发布于 2024-07-27 14:09:40 字数 200 浏览 5 评论 0原文

我有一个非常大的 MySQL 数据库(大约 100 万个项目),并且正在该数据库上运行相当复杂的查询。 当我在 phpMyAdmin 中输入 SQL 命令时大约需要 2 秒,但使用 PHP 代码生成相同的输出大约需要 1-2 分钟。

为什么会有这么大的时差呢? 如果这是一个执行得很差的查询,那么结果会不会也需要很长时间才能出现在 phpMyAdmin 中?

I have an extremely large MySQL database (about 1 million items) and am running a fairly complex query on that database. It takes about 2 seconds when I enter the SQL command into phpMyAdmin but it takes around 1-2 minutes to generate that same output using PHP code.

Why would there be such a time difference? If it was a poorly executed query then wouldn't it take a long time for the results to appear in phpMyAdmin as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流年里的时光 2024-08-03 14:09:40

您确定得到完全相同的输出吗?

如果数据库中有大量数据,您的查询会返回多少行?

当您使用 phpMyAdmin 启动查询时,它会自动添加分页。

因此,对于这个初始查询:

select *
from test

phpMyAdmin 实际上执行这个查询:

select *
from test
limit 0, 30

如果您的查询中没有该限制(当从 PHP 脚本执行它时),它可以解释其中的差异:获取 30 行并获取数百/数千/更多线条花费的时间不同(渲染这些的时间相同)。

如果您在查询中使用限制和/或不尝试获取大量行,您可以发布您的代码吗?

(另一个可能的原因是查询缓存:第一次运行查询时,需要花费大量时间;下一次,MySQL 将结果保留在缓存中)

Are you sure you are getting exactly the same output ?

If you have tons on data in your database, how many lines does your query return ?

When you launch a query with phpMyAdmin, it automatically add pagination.

So, with this initial query :

select *
from test

phpMyAdmin actually executes this one :

select *
from test
limit 0, 30

If you don't have that limit in your query (when executing it from your PHP script), it could explain the difference : fetching 30 lines and fetching hundreds/thousands/more lines does not take the same amount of time (same thing for rendering those).

If you are using limit in your query and/or don't try to get lots of lines, can you post your code ?

(Another possibile reason would be query cache : first time the query is run, it takes lots of time ; next times, MySQL has kept the results in cache)

怪异←思 2024-08-03 14:09:40

除了简单地执行查询之外,PHP 脚本很可能还执行其他操作(例如来回整理大量数据)。您能否向我们展示执行 SQL 的 PHP 代码?

请注意,如果您没有打开输出缓冲,并将数据逐段输出到客户端,这会减慢您的速度很多,因为它发送了如此多的数据向客户提供小块,而不是一大块。 查看PHP 中的输出缓冲

最简单的形式:

ob_start();
// Output lots of data here.
ob_end_flush();

phpMyAdmin 经过优化,可以处理大量数据。 看一下它的内部代码,看看它如何从查询中获取数据,这可能会有一些帮助。

另外,我假设您的 phpMyAdmin 与您的脚本位于同一服务器上?

It's very possible that the PHP script does something in addition to simply executing the query (such as shuffling tremendous amounts of data back and forth.) Could you show us the PHP code that executes your SQL?

Note that if you haven't got output buffering on, and output the data piece by piece to the client, that'll slow you down a lot since it sends so many small chunks to the client, instead of one big chunk. Have a look at output buffering in PHP.

In its simplest form:

ob_start();
// Output lots of data here.
ob_end_flush();

phpMyAdmin is optimized to handle large amounts of data. Have a look at its internal code to see how it fetches data from a query, it could be of some help.

Also, I'm assuming you've got phpMyAdmin on the same server as your script?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文