|
Posted by Toby A Inkster on 05/01/07 14:43
dawnerd wrote:
> I would use a database just for the information, but the inforation
> being stored is site owner defined. They can add or remove fields as
> needed. I will post some more info on the problem when I get off work.
The problem is that it will knacker your query time. Compare for example:
Surname* Forename(s)* Organisation Nationality
-------------------------------------------------------------------
Gates Bill Microsoft US
Jobs Steve Apple, Inc US
Torvalds Linus Linux Foundation FI
versus:
ID* Object
-------------------------------------------------------------
1 array('sn'=>'Gates', 'fn'=>'Gates', 'o'=>'Microsoft', 'co'=>'US')
2 array('sn'=>'Jobs', 'fn'=>'Steve', 'o'=>'Apple, Inc', 'co'=>'US')
3 array('sn'=>'Torvalds', 'fn'=>'Linus', 'o'=>'Linux Foundation', 'co'=>FI')
(An asterisk represents a primary key column. Note I'm using the case
where you'd never want to store more than one person with the same surname
and forename, which is arguably not very realistic.)
Now, say you want to store Rasmus Lerdorf of Yahoo. In the first model,
you'd just insert
'Lerdorf', 'Rasmus', 'Yahoo', 'GL'
And the database would reject it if there was already a Rasmus Lerdorf in
the database, as the Surname and Forename columns are the primary key.
In the latter, there would be no check for duplicates. If you wanted to
check for duplicates, your code would need to loop through the entire
table, read and parse each row to find duplicates, and only insert the
data when there is no duplicate. This might be fine for a table of 3
records, but when you have thousands, then it will cripple your
application's speed.
A search for all people with surname Gates who work at Microsoft would
similarly require your application to loop through each row and parse the
serialised object.
If you really do need to be able to define additional fields of data on
the fly, then you can easily do so through an "attributes" table. e.g.
[People]
Surname* Forename(s)* Organisation Nationality
-------------------------------------------------------------------
Gates Bill Microsoft US
Jobs Steve Apple, Inc US
Torvalds Linus Linux Foundation FI
[Attributes]
Surname* Forename(s)* Attribute Value
------------------------------------------------------
Gates Bill hair-colour Brown
Gates Bill favourite-os Windows Vista
Jobs Steve favourite-os Mac OS X (Leopard)
Torvalds Linus favourite-os Linux
This method should give you complete freedom to define new fields in the
Attributes table, while keeping the core fields in the People table. Easy.
You can even do complex joins to figure out, say, a list of people with
brown hair who like Windows Vista.
SELECT forename||' '||surname
FROM People p
INNER JOIN Attributes a1
ON al.surname=p.surname AND a1.forename=p.forename
INNER JOIN Attributes a2
ON a2.surname=p.surname AND a2.forename=p.forename
WHERE (a1.attribute='hair-colour' AND a1.value='brown')
AND (a2.attribute='favourite-os' AND a2.value='Windows Vista');
which should return one row:
Bill Gates
(and is actually a remarkably accurate list of all people with brown hair
who like Windows Vista.)
--
Toby A Inkster BSc (Hons) ARCS
http://tobyinkster.co.uk/
Geek of ~ HTML/SQL/Perl/PHP/Python/Apache/Linux
Navigation:
[Reply to this message]
|