计算大型数据库的结果数量
我有两个数据库表,其中一个有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个应该会更快,尤其是在没有很多非活动 ID 的情况下。
This one should be faster, especially if there not many inactive ids.
您应该能够使用 SQL 连接两个表。不过最好不要执行 COUNT(*)。您可以通过指定要计数的特定列来获得更好的性能。
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.
您可以使用 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.