Customizing SMF > SMF Coding Discussion
Question about sharing tables
[Unknown]:
You wouldn't need the braces. It's either {$db_prefix['table']} or $db_prefix- not {$db_prefix}.
-[Unknown]
Christian Land:
--- Quote from: Oldiesmann on April 20, 2004, 05:58:26 PM ---That would either create a huge server load (having it look at Settings.php everytime it wanted to run a query) or add a ton of global variables (one for each table name), which would in turn increase the size of Settings.php and just about every other file. The way it's done now is much easier.
--- End quote ---
::) Sorry, oldiesmann, but you're telling "some not so right things" ;D
1. "That would either create a huge server load (having it look at Settings.php everytime it wanted to run a query) "
Nope... it wouldn't increase server-load at all... currently you already have to open Settings.php if you query the database, because thats where all settings for the DB-Connection are stored ($db_prefix, $db_name,...) - and you only have to open it once for each script-call, not for each query!
2. "add a ton of global variables (one for each table name), which would in turn increase the size of Settings.php and just about every other file. The way it's done now is much easier."
Again: wrong! It would add 33 variables (or constants) which isn't much. Together, they would probably take 1kb of memory (more likely: even less). The filesize wouldn't change much, either. There is no real difference if you write:
--- Code: --- $result = db_query("
SELECT t.ID_BOARD, MAX(m.ID_MSG) AS ID_MSG, MAX(m.posterTime) AS posterTime
FROM {$db_prefix}messages AS m, {$db_prefix}topics AS t
WHERE m.ID_MSG = t.ID_LAST_MSG
AND t.ID_BOARD" . (count($setboards) == 1 ? " = $setboards[0]" : ' IN (' . implode(', ', $setboards) . ')') . "
GROUP BY t.ID_BOARD", __FILE__, __LINE__);
--- End code ---
or
--- Code: --- $result = db_query('
SELECT t.ID_BOARD, MAX(m.ID_MSG) AS ID_MSG, MAX(m.posterTime) AS posterTime
FROM '.TABLE_MESSAGES.' AS m, '.TABLE_TOPICS.' AS t
WHERE m.ID_MSG = t.ID_LAST_MSG
AND t.ID_BOARD' . (count($setboards) == 1 ? " = $setboards[0]" : ' IN (' . implode(', ', $setboards) . ')') . "
GROUP BY t.ID_BOARD", __FILE__, __LINE__);
--- End code ---
depending on the query, it would use a few bytes less or a few bytes more than the current version...
If you use constants for the table name, you could even remove all "global $db_prefix" lines from the current source...
Thats the way nearly all professional PHP-scripts are handling their table-names...
Oldiesmann:
Ok... I was wrong... I'm not an expert by any means and wasn't thinking in terms of having to look at Settings.php only once... :)
[Unknown]:
Constants are actually slower than scalars in PHP. I don't know why, but they are a joke and just are basically unchangable auto globals. Using an indexed array might be as slow, but not as ugly :P.
-[Unknown]
Christian Land:
I tested it with 10.000.000 assignments of constants to variables and variables to variables.... there was no measurable difference.... both took roughly 10s... of course i don't know how php treats variable-assignments in loops... but i guess we are talking about differences in really small fractions of a second.... so it doesn't matter (IMHO) if you use variables or constants... but moving the table names into special vars/consts would make the code more "useable"...
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version