维护排名表的最佳实践?

发布于 2024-09-24 04:36:15 字数 503 浏览 9 评论 0原文

使用 MySQL 和 PHP,我有一个简单的排名表,其中包含 2 列 userscore

每周一次,我重新运行排名脚本,它会计算每个用户的分数。

  • 许多用户有新分数,
  • 有些没有
  • ,有时有新用户添加到表中,

使用 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 技术交流群。

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

发布评论

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

评论(3

合约呢 2024-10-01 04:36:15

REPLACE ... INTO 语法将在需要时插入新行并删除+插入现有行:

# deletes existing and inserts new row:
REPLACE INTO users SET score = 2000, username = 'User01';
# deletes existing and inserts new row
REPLACE INTO users SET score = 3000, username = 'User02';
# inserts new row
REPLACE INTO users SET score = 4000, username = 'User04';  

http://dev.mysql.com/doc/refman/5.0/en/replace.html

我最喜欢的方法是使用 ON DUPLICATE KEY 语法 - 主要是因为我一直认为删除一行然后重新插入它会比只更新现有行更“昂贵”。假设 username 字段是 UNIQUEPRIMARY KEY 索引,您可以执行以下操作

# Updates existing User01 row
INSERT INTO users SET score = 2000, username = 'User01' ON DUPLICATE KEY UPDATE score = 2000;
# Updates existing User02 row
INSERT INTO users SET score = 3000, username = 'User02' ON DUPLICATE KEY UPDATE score = 2000;
# Inserts new User04 row
INSERT INTO users SET score = 4000, username = 'User04' ON DUPLICATE KEY UPDATE score = 2000;

: 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:

# deletes existing and inserts new row:
REPLACE INTO users SET score = 2000, username = 'User01';
# deletes existing and inserts new row
REPLACE INTO users SET score = 3000, username = 'User02';
# inserts new row
REPLACE INTO users SET score = 4000, username = 'User04';  

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 the username field is a UNIQUE or PRIMARY KEY index you can do the following:

# Updates existing User01 row
INSERT INTO users SET score = 2000, username = 'User01' ON DUPLICATE KEY UPDATE score = 2000;
# Updates existing User02 row
INSERT INTO users SET score = 3000, username = 'User02' ON DUPLICATE KEY UPDATE score = 2000;
# Inserts new User04 row
INSERT INTO users SET score = 4000, username = 'User04' ON DUPLICATE KEY UPDATE score = 2000;

http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

ㄟ。诗瑗 2024-10-01 04:36:15

INSERT 不会覆盖现有行,而是会产生错误(例如,如果您将用户列标记为唯一并尝试添加已存在的用户)。同样,UPDATE 不会插入新行,只会更改现有行。

如果大多数用户需要更新并且存在应该删除的用户,即不再包含在新的分数表中,则删除并重写整个表似乎是可接受的解决方案。

否则,我会更新表:

可移植的解决方案是检查表中是否已包含键(即用户)(通过 SELECT),然后对不存在的键使用 INSERT 或对已存在的键使用 UPDATE。当然,您也可以尝试 INSERT 并检查错误代码,如果出现约束违规错误,则返回到 UPDATE。

在 MySQL 上,您还可以使用以下语法,该语法允许您仅使用单个查询进行操作:

INSERT INTO ... ON DUPLICATE KEY UPDATE score = ...

但是,这不可移植,并且不能与其他 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:

INSERT INTO ... ON DUPLICATE KEY UPDATE score = ...

However, this is not portable and will not work with other RDBMS, only with MySQL.

转角预定愛 2024-10-01 04:36:15

正如您提到的两种方式

更新或插入,

我认为插入比更新更好。对于插入,您添加一个查询。至于更新,您必须为每个用户运行 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.

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