MySQL选择计数(1)语句在桌上太慢,数百万行

发布于 2025-02-06 23:04:16 字数 542 浏览 2 评论 0原文

以下语句需要0.73秒,并产生54,000个结果:

SELECT COUNT(1) FROM `table` WHERE MATCH(tagline, location, country) AGAINST(' +Germany' IN BOOLEAN MODE)

此说明需要28秒,并产生33万秒的结果:

SELECT COUNT(1) FROM `table` WHERE MATCH(tagline, location, country) AGAINST(' +United +States' IN BOOLEAN MODE)

有没有办法通过以某种方式将结果限制为一百万?

这样的事情:

COUNT THIS -->    
SELECT * FROM `table` WHERE MATCH(tagline, location, country) AGAINST(' +United +States' IN BOOLEAN MODE)
LIMIT 1000000  

The following statement takes 0.73 seconds and generates 54,000 results:

SELECT COUNT(1) FROM `table` WHERE MATCH(tagline, location, country) AGAINST(' +Germany' IN BOOLEAN MODE)

This statement takes 28 seconds and generates 3.3 millions results:

SELECT COUNT(1) FROM `table` WHERE MATCH(tagline, location, country) AGAINST(' +United +States' IN BOOLEAN MODE)

Is there a way to limit the query time by somehow capping the results at one million?

Something like this:

COUNT THIS -->    
SELECT * FROM `table` WHERE MATCH(tagline, location, country) AGAINST(' +United +States' IN BOOLEAN MODE)
LIMIT 1000000  

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

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

发布评论

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

评论(2

§普罗旺斯的薰衣草 2025-02-13 23:04:16

回答我自己的问题:

将您的选择查询包裹在此:

SELECT COUNT(1) FROM ( ... ) as someRandomWord

我的选择查询对此进行了更改,以便结果可以受到限制:

SELECT counter FROM `table` WHERE MATCH(tagline, location, experience, country) AGAINST(' +United +States' IN BOOLEAN MODE) limit 1000000

将两个问题放在一起:

SELECT COUNT(1) FROM (
SELECT counter FROM `table` WHERE MATCH(tagline, location, experience, country) AGAINST(' +business' IN BOOLEAN MODE) limit 1000000
) as someRandomWord

“德国” - > 54,000个结果(0.76秒)

“美国” ---> “超过一百万的结果”(2.4秒)

Answering my own question:

Wrap your select query in this:

SELECT COUNT(1) FROM ( ... ) as someRandomWord

My select query is altered to this so that results can be limited:

SELECT counter FROM `table` WHERE MATCH(tagline, location, experience, country) AGAINST(' +United +States' IN BOOLEAN MODE) limit 1000000

Put this two together:

SELECT COUNT(1) FROM (
SELECT counter FROM `table` WHERE MATCH(tagline, location, experience, country) AGAINST(' +business' IN BOOLEAN MODE) limit 1000000
) as someRandomWord

"Germany" --> 54,000 results (0.76 seconds)

"United States" ---> "More than one million results" (2.4 seconds)

无言温柔 2025-02-13 23:04:16

您应该运行解释计划并找到效率低下。您可以在本文中查看如何分析解释平原:

https://dev.mysql.com/doc/refman/8.0/en/execution-plan-information.html
要点:

  1. 您的目标是识别解释计划的各个方面,表明查询非常优化,并学习SQL语法和索引技术,以改善计划,如果您看到某些效率低下的操作。
  2. 在解释的帮助下,您可以在哪里查看在哪里添加索引,以使语句通过使用索引查找行更快地执行,
  3. 还可以使用luxply来检查优化器是否以最佳顺序连接了表。
  4. 说明语句提供了有关MySQL如何执行语句的信息。用选择,删除,插入,替换和更新语句说明作品。

根据您的SQL,您使用MySQL似乎是您的。

最终结果可能是添加索引。本文应有助于创建您的索引:

https://www.tutorialspoint.com/mysql.com/mysql /mysql-indexes.htm
关键点:

  1. 创建索引加快访问数据,但使用磁盘空间来维护索引。这是您可以创建索引的方式:在tutorials_tbl(tutorial_author desc)上创建索引fure_index
  2. desc/asc-您应该根据最有可能访问的方式考虑数据和订单的自然顺序和顺序。它。如果是时间表,并且您最总是想获取最新数据,则使用DESC顺序。

You should run explain plan and find your inefficiencies. You can see how to analyze explain plain at this article:

https://dev.mysql.com/doc/refman/8.0/en/execution-plan-information.html
Key points:

  1. Your goals are to recognize the aspects of the EXPLAIN plan that indicate a query is optimized well, and to learn the SQL syntax and indexing techniques to improve the plan if you see some inefficient operations.
  2. With the help of EXPLAIN, you can see where you should add indexes to tables so that the statement executes faster by using indexes to find rows
  3. You can also use EXPLAIN to check whether the optimizer joins the tables in an optimal order.
  4. The EXPLAIN statement provides information about how MySQL executes statements. EXPLAIN works with SELECT, DELETE, INSERT, REPLACE, and UPDATE statements.

Based on your SQL it appears your using MySQL.

The end result might be to add indexes. This article should help to create your indexes:

https://www.tutorialspoint.com/mysql/mysql-indexes.htm
Key Points:

  1. Creating indexes speed up access data but uses disk space to maintain the index. Here is how you can create an index: CREATE INDEX AUTHOR_INDEX ON tutorials_tbl (tutorial_author DESC)
  2. DESC/ASC - You should think about the natural order of data and order based on the most likely way you want to access it. If it was timeseries and you most always want to the most recent data, the use DESC order.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文