维护排名表的最佳实践?
使用 MySQL 和 PHP,我有一个简单的排名表,其中包含 2 列 user
和 score
。
每周一次,我重新运行排名脚本,它会计算每个用户的分数。
- 许多用户有新分数,
- 有些没有
- ,有时有新用户添加到表中,
使用 MySQL 解决此问题的最佳方法是什么?如果我需要添加新行,update
是否有效? insert
是否会覆盖现有行,以便我可以再次插入整个表?最好删除该表并从头开始重写吗?
**Sample Data**
User01 2500
User02 3000
User03 100
**New Data to be Added**
User01 2700
User02 4000
User04 1000 // new account
除了成千上万的用户......
Using MySQL and PHP, I have a simple ranking table with 2 columns user
and score
.
Once a week, I re-run the ranking script and it computes scores for each user.
- many users have new scores
- some do not
- some times there are new users to add to the table
What's the best way to approach this with MySQL? Does update
work if I need to add new rows? Would insert
override existing rows such that I could just insert the entire table again? Would it be best to just drop the table and re-write it from scratch?
**Sample Data**
User01 2500
User02 3000
User03 100
**New Data to be Added**
User01 2700
User02 4000
User04 1000 // new account
Except with thousands of users...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
REPLACE ... INTO 语法将在需要时插入新行并删除+插入现有行:
http://dev.mysql.com/doc/refman/5.0/en/replace.html
我最喜欢的方法是使用
ON DUPLICATE KEY
语法 - 主要是因为我一直认为删除一行然后重新插入它会比只更新现有行更“昂贵”。假设username
字段是UNIQUE
或PRIMARY KEY
索引,您可以执行以下操作: mysql.com/doc/refman/5.0/en/insert-on-duplicate.html" rel="nofollow noreferrer">http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate .html
The
REPLACE ... INTO
syntax will insert new rows when needed and delete + insert for existing rows:http://dev.mysql.com/doc/refman/5.0/en/replace.html
My favorite method though is to use the
ON DUPLICATE KEY
syntax - mainly because I've always assumed that deleting a row then re-inserting it would be more 'expensive' then just updating an existing row. Assuming that theusername
field is aUNIQUE
orPRIMARY KEY
index you can do the following:http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html
INSERT 不会覆盖现有行,而是会产生错误(例如,如果您将用户列标记为唯一并尝试添加已存在的用户)。同样,UPDATE 不会插入新行,只会更改现有行。
如果大多数用户需要更新并且存在应该删除的用户,即不再包含在新的分数表中,则删除并重写整个表似乎是可接受的解决方案。
否则,我会更新表:
可移植的解决方案是检查表中是否已包含键(即用户)(通过 SELECT),然后对不存在的键使用 INSERT 或对已存在的键使用 UPDATE。当然,您也可以尝试 INSERT 并检查错误代码,如果出现约束违规错误,则返回到 UPDATE。
在 MySQL 上,您还可以使用以下语法,该语法允许您仅使用单个查询进行操作:
但是,这不可移植,并且不能与其他 RDBMS 一起使用,只能与 MySQL 一起使用。
INSERT will not overwrite existing rows, but instead yield an error (if, for example, you marked the user column as unique and try to add an already existing user). Similarly, UPDATE will not insert new rows, only change existing ones.
Dropping and rewriting the whole table seems like an acceptable solution, if most users need updating and there are users that should be dropped, i.e. not be contained in the new score tables any longer.
Otherwise, I would update the table:
The portable solution is to check whether a key (i.e. user) is already contained in the table (via SELECT) and use then either INSERT for non-existing keys or UPDATE for already existing keys. Of course, you could also just try INSERT and check the error code, dropping back to UPDATE, if you get an constraint violation error.
On MySQL, you can also use the following syntax which allows you the operation with only a single query:
However, this is not portable and will not work with other RDBMS, only with MySQL.
正如您提到的两种方式
更新或插入,
我认为插入比更新更好。对于插入,您添加一个查询。至于更新,您必须为每个用户运行 2 个查询。首先选择用户,然后根据userId更新。
所以最快的是插入,你可以随时使用 sql 查询求和。
As you mention two way
UPDATE OR INSERT
I think INSERT would be good over update. For insert you add one query. Where as for update you have to run 2 query for each user. first select user and then update based on userId.
So fastest would be insert and you can sum it any time using sql query.