计算大型数据库的结果数量

发布于 2024-12-26 00:16:35 字数 770 浏览 0 评论 0原文

我有两个数据库表,其中一个有 165 个条目。对于其中的每一个,我都必须浏览 100 万个条目表,看看这 165 个条目中每一个被提及了多少次。

“odds_provider”有 165 个条目,“bettingoffer”有 100 万个条目。

    $SQL = "SELECT
    odds_provider.id AS id,
    odds_provider.name AS name,
    COUNT(bettingoffer.odds_providerFK) AS betcount
    FROM odds_provider
    INNER JOIN
    bettingoffer
    ON bettingoffer.odds_providerFK = odds_provider.id
    WHERE 
    odds_provider.active = 'yes'
    GROUP BY
    odds_provider.id,
    odds_provider.name
    ORDER BY betcount DESC";

    $result = mysql_query($SQL);
    while ($db_field = mysql_fetch_assoc($result)) {
echo $db_field['id'] , " " , $db_field['name'] , " " , $db_field['betcount'] , "</BR>";
    }           

它实现了预期的目的,但需要很长时间。有更快的方法吗?

I got a two database tables, one has 165 entries. For each of those, I got to browse the 1-million-entry table and see how many times is each of those 165 entries mentioned.

"odds_provider" has 165 entries, "bettingoffer" has a million entries.

    $SQL = "SELECT
    odds_provider.id AS id,
    odds_provider.name AS name,
    COUNT(bettingoffer.odds_providerFK) AS betcount
    FROM odds_provider
    INNER JOIN
    bettingoffer
    ON bettingoffer.odds_providerFK = odds_provider.id
    WHERE 
    odds_provider.active = 'yes'
    GROUP BY
    odds_provider.id,
    odds_provider.name
    ORDER BY betcount DESC";

    $result = mysql_query($SQL);
    while ($db_field = mysql_fetch_assoc($result)) {
echo $db_field['id'] , " " , $db_field['name'] , " " , $db_field['betcount'] , "</BR>";
    }           

It does what's intended but it takes forever. Is there a faster way?

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

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

发布评论

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

评论(3

归途 2025-01-02 00:16:35

这个应该会更快,尤其是在没有很多非活动 ID 的情况下。

SELECT id, name, betcount
FROM (
  SELECT
    odds_providerFK as id,
    COUNT(*) as betcount
  FROM bettingoffer
  WHERE active = 'yes'
  GROUP BY odds_providerFK
  ORDER BY betcount DESC) as counts
USING (id);

This one should be faster, especially if there not many inactive ids.

SELECT id, name, betcount
FROM (
  SELECT
    odds_providerFK as id,
    COUNT(*) as betcount
  FROM bettingoffer
  WHERE active = 'yes'
  GROUP BY odds_providerFK
  ORDER BY betcount DESC) as counts
USING (id);
残龙傲雪 2025-01-02 00:16:35

您应该能够使用 SQL 连接两个表。不过最好不要执行 COUNT(*)。您可以通过指定要计数的特定列来获得更好的性能。

SELECT
  op.id,
  op.name,
  COUNT(bo.odds_providerFK) AS bet_offering_count
FROM
 odds_provider op
 INNER JOIN
 bettingoffer bo
  ON bo.odds_providerFK = op.id
 WHERE 
  op.active = 'yes'
GROUP BY
  op.id,
  op.name

You should be able to join the two tables using SQL. It would be best to not do a COUNT(*) though. You can get better performance by specifying the specific column you would want to count.

SELECT
  op.id,
  op.name,
  COUNT(bo.odds_providerFK) AS bet_offering_count
FROM
 odds_provider op
 INNER JOIN
 bettingoffer bo
  ON bo.odds_providerFK = op.id
 WHERE 
  op.active = 'yes'
GROUP BY
  op.id,
  op.name
少年亿悲伤 2025-01-02 00:16:35

您可以使用 SQL Join 查询以更好的方式完成此操作。您不必在第二个表中搜索第一个表中的每条记录。您可以在表之间建立 JOIN 关系。

如果您粘贴两个表的架构定义,将会更有帮助,但一般来说,这就是 JOIN 查询的工作方式。

参考这些教程了解 JOIN 查询的工作原理。

You can do it in much better way using SQL Join queries. You don't have to search the 2nd table for each individual record in 1st table. You can establish a JOIN relationship between the tables.

It would be more helpful if you would have pasted the schema definition of both of your tables, but in general this is how JOIN queries works.

Kindly refer to these tutorials about how JOIN queries work.

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