mysql(查询) 或 php switch 语句哪个更快?
我想知道是否有人能指出我正确的方向。我想知道哪个更快...我正在创建的游戏有一个情况,有超过 630,000 个组合,我想知道我的脚本是否要在数据库中查找一个结果,会比说更快一个大的 switch 语句? ....我创建的这个游戏(我希望)应该是一个轻量级的热门游戏,我不希望出现任何问题
<?php
// is this quicker
mysql_query(....) - meanwhile remeber this table should have anywere from 600,000-630,000 rows
// or is this quicker
switch{
case --
....
case--
....
were here this will be in one page with anywwhere from 600,000 - 630,000 different case's ?
}
?>
i was wonder if anyone could point me in the right direction . i was wondering which is faster ..... i have a situation on a game im creating were there are over 630,000 combinations and i wanted to know is if my script is going to seek the database for one result , would be quicker then say a large switch statement? .... this game im creating is (im hoping) suppose to be a lightwight hit and i dnt want any problems
<?php
// is this quicker
mysql_query(....) - meanwhile remeber this table should have anywere from 600,000-630,000 rows
// or is this quicker
switch{
case --
....
case--
....
were here this will be in one page with anywwhere from 600,000 - 630,000 different case's ?
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
php 将花费很长时间来解析页面,这可能会使其变得更慢,无论执行时间如何,这可能会或可能不会更慢(我认为查询在这里也会更快)。如果查询可以做到这一点,您也可以考虑使用关联数组而不是开关,但它不会使解析速度更快。还要考虑内存消耗。
你可以尝试一下。
php will take ages to parse the page, which will probably make it slower regardless of execution time, which may or may not be slower (thought I'd expect query to be faster here as well). You may also consider associative array instead of switch, if query can do that, but it won't make parsing much faster. And think of memory consumption too.
And you can just try.
我几乎肯定会使用 MySQL 查询,特别是如果表索引正确的话。 switch 方法将很难维护 - 例如,如果除了您之外的其他人需要向 switch 添加新组合怎么办?
I would almost certainly go with the MySQL query, especially if the table(s) are indexed correctly. The switch method will be very hard to maintain - for example, what if someone other than you needed to add a new combination to the switch?
PHP 不使用切换表来优化大小写选择。这相当于一个巨大的 if-elseif-else... 语句。
为此,最好使用对正确索引的表进行查询。
PHP doesn't use switch tables to optimize case selections. It would be the equivalent of a gigantic if-elseif-else... statement.
It's better to use queries to properly indexed tables for this.
好的,只需阅读该内容,接受或保留它(不知道您的游戏是什么样子)。
如果这是一个高流量(许多用户)的游戏/网站,我会将数据分成至少两个表,一个只保存 ID、一些组信息和所谓的组合。所有其他附加数据将位于第二个表中,可通过 JOIN 访问该 ID。
另外,我会(如果可能)将这些组拆分为表块。
逻辑分裂:
- 无需创建单独的表
- 无需跨文件移动数据块
使用分区的 4 个主要原因:
- 使单个插入和选择更快
- 使范围选择更快
- 帮助将数据分割到不同的路径上
- 有效存储历史数据
- 如果您需要立即删除大块数据
Ok, just read that, take it or leave it (don't know what your game looks like).
If that is a high traffic (many users) game/website, I would split data in at least two tables, one holding just ID, some GROUP information and what you call COMBINATION. All other additional data would then be in the second table, accessable over a JOIN to that ID.
Also I would (if possible) split those GROUPs into table chunks.
Logical Splitting:
- No need to create separate tables
- no need to move chunks of data across files
4 main reasons to use partitions:
- to make single inserts and selcts faster
- to make range selects faster
- to help split the data across different paths
- to store historical data efficiently
- if you need to delete large chunks of data instantly