使用 PHP 和 MySQL 优化排名页面

发布于 2024-09-13 14:04:59 字数 966 浏览 5 评论 0原文

我确实需要一些帮助来优化我网站上用于显示排名的表格。我已经阅读了很多有关如何优化查询以及如何正确使用索引的文章,但即使在实施了我认为可行的更改之后,也看不到什么改进。我的快速解决办法是仅使用前 100,000 个排名(每天更新并存储在不同的表中)来提高目前的速度,但我真的不喜欢这个选项。

因此,我有一个存储用户信息的表,如下所示:

表“缓存”:

id    (Primary key)
name
region
country
score

还存储有关用户的其他变量,但我认为它们与此处无关,因为它们未在排名中使用。

用户可以查看 3 个基本排名页面:

世界视图:

SELECT cache name,region,country,score FROM cache ORDER BY score DESC LIMIT 0,26

区域视图:

SELECT name,region,country,score FROM cache WHERE region='Europe' ORDER BY score DESC LIMIT 0,26

和国家视图:

SELECT name,region,country,score FROM cache WHERE region='Europe' AND country='Germany' ORDER BY score DESC LIMIT 0,26

我已经尝试了几乎所有我能想到的索引组合,以帮助减轻数据库的工作,虽然有些似乎有点帮助我找不到一个只返回 26 行的地区和国家查询(只需一个“分数”索引,世界排名就非常快)。

我觉得我可能缺少一些基本的东西,任何帮助将不胜感激!

额外的信息:缓存表目前约为 920 MB,总共略多于 800,000 行。如果您需要更多信息,请告诉我。

I could really use some help optimizing a table on my website that is used to display rankings. I have been reading a lot on how to optimize queries and how to properly use indexes but even after implementing changes I thought would work, little improvement can be seen. My quick fix has been simply to use only the top 100,000 rankings (updated daily and stored in a different table) to improve the speed for now, but I really don't like that option.

So I have a table that stores the information for users that looks something like:

table 'cache':

id    (Primary key)
name
region
country
score

There are other variables being stored about the user, but I don't think they are relevant here as they are not used in the rankings.

There are 3 basic ranking pages that a user can view:

A world view:

SELECT cache name,region,country,score FROM cache ORDER BY score DESC LIMIT 0,26

A region view:

SELECT name,region,country,score FROM cache WHERE region='Europe' ORDER BY score DESC LIMIT 0,26

and a country view:

SELECT name,region,country,score FROM cache WHERE region='Europe' AND country='Germany' ORDER BY score DESC LIMIT 0,26

I have tried almost every combination of indexes I can think of to help alleviate work for the database, and while some seem to help a little bit I can't find one that will only return 26 rows for both the region and country queries(with simply an index on 'score' the world rankings are blazing fast).

I feel like I might be missing something basic, any help would be much appreciated!

Little extra info: the cache table is currently around 920 megabytes with a little more than 800,000 rows total. If you could use any more info just let me know.

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

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

发布评论

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

评论(3

尾戒 2024-09-20 14:04:59

您的世界排名受益于分数索引,因为分数是查询中的唯一标准。它排序的逻辑顺序内置于查询中。所以这样很好。

其他查询将受益于区域索引。然而,与@Matt 指出的类似,地区、国家和分数的综合指数可能是最好的选择。请注意,键的三列应按地区、国家、分数顺序排列。

Your world rankings benefit from the score index because score is the only criteria in the query. The logical sequence it sorts on is built into the query. So that's good.

The other queries will benefit from an index on region. However, similar to what @Matt indicates, a composite index on region, country and score may be the best bet. Note, the three columns for the key should be in region, country, score sequence.

简美 2024-09-20 14:04:59

对国家、分数、地区建立一个索引。太多的索引会减慢你的速度。

put ONE index on country, score, region. Too many indices will slow you down.

千纸鹤 2024-09-20 14:04:59

我们谈论的是多少条记录?

我不是 SQL 专家,但在这种情况下,如果仅仅更改索引并不能解决问题。我会考虑尝试一下表结构,看看我可以获得什么性能增益。

缓存

ID (pk)
LocationId(索引)
姓名
得分

位置

LocationId (pk)
CountryId(索引)也许
RegionId(索引)可能是

国家

CountryId
名称

区域

RegionId
名称

位置

LocationId (主键)
国家/地区 ID
ID 国家

地区

国家 ID
名称

区域

RegionId
Procs 中的名称

临时表将允许您在每种情况下选择位置 ID。它将降低您遇到的问题的整体复杂性:您将解决 1 个查询计划,而不是 3 个查询计划。

工作量会很高,并且在完成之前会得到回报,所以我建议查看索引方法第一的。

祝你好运

How many records are we talking about?

I am no SQL guru, but in this case, if just changes to indexes did not do the trick. I would consider playing around with the table structure to see what perf gains I could get.

Cache

id (pk)
LocationId (index)
name
score

Location

LocationId (pk)
CountryId (index) maybe
RegionId (index) maybe

Country

CountryId
name

Region

RegionId
name

Location

LocationId (Primary key)
CountryId
RegionId

Country

CountryId
name

Region

RegionId
name

Temp tables in Procs would allow you to select on Location Id in every case. It would reduce the over all complexity of the issue you are having: you would be troubleshooting 1 query plan, not 3.

The effort would be high, and the payoff would be until you were done, so I would suggest looking at the index approach first.

Good luck

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