如何使用 php 和 mysql 优化这个简单的数据库和查询?

发布于 2024-12-20 10:17:26 字数 540 浏览 5 评论 0原文

我从数据库中提取一系列(例如限制 72、24)游戏,根据这些游戏被评为最受欢迎。我有一个单独的表用于跟踪游戏数据,还有一个用于跟踪游戏的个人投票(评分从 1 到 5,每个游戏每个用户一票)。当该游戏在该游戏的所有评级投票中具有最高平均评级时,该游戏被认为是“最受欢迎”或“更受欢迎”。票数少于 5 票的游戏不予考虑。这些表如下所示(两个表,“游戏”和“投票”):

games:
gameid(key)
gamename
thumburl

votes:
userid(key)
gameid(key)
rating

现在,我了解到有一种称为“索引”的东西,它可以通过本质上预查询我的表并构建单独的表来加速我的查询索引表(我真的不知道......这只是我的印象)。

我还读到,当多个查询可以压缩为一个更长的查询(我认为包含联接和嵌套选择语句)时,mysql 运行速度最快。

但是,我目前没有使用索引,并且我正在进行多个查询以获得最终结果。

应该对我的数据库进行哪些更改(如果有的话——包括构建索引表等)?我的查询应该是什么样的?

谢谢。

I pull a range (e.g. limit 72, 24) of games from a database according to which have been voted most popular. I have a separate table for tracking game data, and one for tracking individual votes for a game (rating from 1 to 5, one vote per user per game). A game is considered "most popular" or "more popular" when that game has the highest average rating of all the rating votes for said game. Games with less than 5 votes are not considered. Here is what the tables look like (two tables, "games" and "votes"):

games:
gameid(key)
gamename
thumburl

votes:
userid(key)
gameid(key)
rating

Now, I understand that there is something called an "index" which can speed up my queries by essentially pre-querying my tables and constructing a separate table of indices (I don't really know.. that's just my impression).

I've also read that mysql operates fastest when multiple queries can be condensed into one longer query (containing joins and nested select statements, I presume).

However, I am currently NOT using an index, and I am making multiple queries to get my final result.

What changes should be made to my database (if any -- including constructing index tables, etc.)? And what should my query look like?

Thank you.

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

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

发布评论

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

评论(4

陈独秀 2024-12-27 10:17:26

计算每场比赛平均值的查询可能如下所示:

SELECT gamename, AVG(rating)
FROM games INNER JOIN votes ON games.gameid = votes.gameid
GROUP BY games.gameid
HAVING COUNT(*)>=5 
ORDER BY avg(rating) DESC
LIMIT 0,25

您必须在游戏和投票上都有一个 gameid 索引。 (如果您已将 gameid 定义为桌面游戏的主键,那就可以了)

Your query that calculates the average for every game could look like:

SELECT gamename, AVG(rating)
FROM games INNER JOIN votes ON games.gameid = votes.gameid
GROUP BY games.gameid
HAVING COUNT(*)>=5 
ORDER BY avg(rating) DESC
LIMIT 0,25

You must have an index on gameid on both games and votes. (if you have defined gameid as a primary key on table games that is ok)

情话难免假 2024-12-27 10:17:26

根据 MySQL 文档,当您在 表创建。这值得一提,因为并非所有 RDBMS 都以这种方式运行。

我认为您的想法是正确的,您的“投票”表充当“游戏”和“用户”之间的桥梁来处理多对多关系。只需确保“userid”和“gameid”在“votes”表上建立索引即可。

According to the MySQL documentation, an index is created when you designate a primary key at table creation. This is worth mentioning, because not all RDBMS's function this way.

I think you have the right idea here, with your "votes" table acting as a bridge between "games" and "user" to handle the many-to-many relationship. Just make sure that "userid" and "gameid" are indexed on the "votes" table.

得不到的就毁灭 2024-12-27 10:17:26

如果您有权为表使用 InnoDB 存储,则可以创建 votes 表中 gameid 上的“nofollow">外键将使用为 games 中的主键创建的索引桌子。然后,当您执行连接这两个表的查询时(例如 ... INNER JOIN votes ON games.gameid = votes.gameid),它将使用该索引来加快速度。

您对索引的理解本质上是正确的 - 它基本上创建一个单独的查找表,可以在执行查询时在幕后使用该表。

使用索引时,使用 EXPLAIN 语法(只需在 SELECT 前面加上 EXPLAIN 即可尝试一下)。它提供的输出显示可用于查询的可能键的列表以及查询正在使用哪个键。这在优化查询时非常有帮助。

If you have access to use InnoDB storage for your tables, you can create foreign keys on gameid in the votes table which will use the index created for your primary key in the games table. When you then perform a query which joins these two tables (e.g. ... INNER JOIN votes ON games.gameid = votes.gameid) it will use that index to speed things up.

Your understanding of an index is essentially correct — it basically creates a separate lookup table which it can use behind the scenes when the query is executed.

When using an index it is useful to use the EXPLAIN syntax (simply prepend your SELECT with EXPLAIN to try this out). The output it gives show you the list of possible keys available for the query as well as which key the query is using. This can be very helpful when optimising your query.

梦亿 2024-12-27 10:17:26

索引是一种物理数据结构,用于帮助加速检索类型查询;它不仅仅是一张桌子上的一张桌子 ->不过对于一个概念来说很好。另一个概念是索引在教科书后面的工作方式(唯一的区别是,对于您的书,搜索键可以指向多个页面/匹配项,而对于索引,搜索键仅指向一页/匹配项)。索引是由数据结构定义的,因此您可以使用 B+ 树索引,甚至还有哈希索引。这是从数据库的物理/内部级别进行的数据库/查询优化 - 我假设您知道您正在 DBMS 的更高级别上工作,这更容易。索引植根于内部级别,这使得数据库查询优化更加有效和有趣。

我从你的问题中注意到你甚至还没有开发查询。首先关注查询。事实上,在任何研究生或研究生数据库课程中,索引都是在数据库维护之后出现的,而不一定是开发。

另外注意,我看到很多人都说,作为一个规则,要创建所有主键索引。这不是真的。在许多情况下,主键索引会减慢数据库速度。事实上,如果我们只使用主索引,那么应该使用哈希索引,因为它们比 B+ 树工作得更好!

综上所述,针对查询和索引提出问题是没有意义的。首先寻求有关查询的帮助。然后给定你的表(关系模式)和 SQL 查询,然后我才能建议你最好的索引 - 记住它的维护。如果开发是0,我们就无法进行维护。

亲切的问候,

请注意,许多计算机课程的研究生级别有关索引的大多数问题如下:我们为学生提供一个关系模式(即您的表)和一个查询,然后询问:批判性地为以下查询建议一个合适的索引表格 ---->如果他们没有疑问,我们就不能问这样的问题

An index is a PHYSICAL DATA STRUCTURE which is used to help speed up retrieval type queries; it's not simply a table upon a table -> good for a concept though. Another concept is the way indexes work at the back of your text book (the only difference is with your book a search key could point to multiple pages / matches whereas with indexes a search key points to only one page/match). An index is defined by data structures so you could use a B+ tree index and there are even hash indexes. It's Database/Query optimization from the physical/internal level of the Database - I'm assuming that you know that you're working at the higher levels of the DBMS which is easier. An index is rooted within the internal levels and that make DB query optimization much more effective and interesting.

I've noticed from your question that you have not even developed the query as yet. Focus on the query first. Indexing comes after, as a matter of a fact, in any graduate or post graduate Database course, indexing falls under the maintenance of a Database and not necessarily the development.

Also N.B. I have seen quite many people say as a rule to make all primary keys indexes. This is not true. There are many instances where a primary key index would slow up the Database. Infact, if we were to go with only primary indexes then should use hash indexes since they work better than B+ trees!

In summary, it doesn't make sense to ask a question for a query and an index. Ask for help with the query first. Then given your tables (relational schema) and SQL query, then and only then could I advice you on the best index - remember its maintenance. We can't do maintanance if there is 0 development.

Kind Regards,

N.B. most questions concerning indexes at the post graduate level of many computing courses are as follows: we give the students a relational schema (i.e. your tables) and a query and then ask: critically suggest a suitable index for the following query on the tables ----> we can't ask a question like this if they dont have a query

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