简体   繁体   English

哪个更快的mysql(查询)或PHP切换语句?

[英]which is faster mysql(querys) or php switch statements?

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? 我想知道哪个更快.....我有一个游戏即时创建的情况有超过630,000组合我想知道如果我的脚本将寻求数据库的一个结果,会更快然后说一个大的开关声明? .... 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 ?
}
?>

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). php需要花费很长时间来解析页面,这可能会使它变慢,无论执行时间如何,这可能会或可能不会更慢(我认为我希望查询在这里更快)。 You may also consider associative array instead of switch, if query can do that, but it won't make parsing much faster. 你也可以考虑关联数组而不是switch,如果查询可以做到这一点,但它不会使解析更快。 And think of memory consumption too. 并考虑内存消耗。

And you can just try. 你可以尝试一下。

I would almost certainly go with the MySQL query, especially if the table(s) are indexed correctly. 我几乎肯定会使用MySQL查询,特别是如果表格索引正确。 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 doesn't use switch tables to optimize case selections. PHP不使用切换表来优化案例选择。 It would be the equivalent of a gigantic if-elseif-else... statement. 它将相当于一个巨大的if-elseif-else ...语句。

It's better to use queries to properly indexed tables for this. 为此,最好将查询用于正确索引的表。

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. 如果这是一个高流量(许多用户)的游戏/网站,我会在至少两个表中分割数据,一个只持有ID,一些GROUP信息以及你所谓的COMBINATION。 All other additional data would then be in the second table, accessable over a JOIN to that ID. 然后,所有其他附加数据将位于第二个表中,可通过JOIN访问该ID。

table 1
ID | GROUP | COMBINATION
1 | island | ABCDE
2 | house | FGHIJ

table2
ID | MORE INFO
1 | ...
2 | ...

Also I would (if possible) split those GROUPs into table chunks. 我也会(如果可能的话)将这些GROUP分成表格块。

// ok, this is an example for ID and range of IDs, but I think you can get it
partition by range (id)
(
PARTITION P1 VALUES LESS THAN (10),
PARTITION P2 VALUES LESS THAN (20)
)

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 使用分区的4个主要原因: - 使单个插入和选择更快 - 使范围选择更快 - 帮助跨不同路径分割数据 - 有效地存储历史数据 - 如果您需要立即删除大块数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM