in reply to (Ovid) Re: database design issue: one big table vs hundred of smaller tables
in thread database design issue: one big table vs hundred of smaller tables

Thank you all for your replies, they offered me some really good points to start digging on. I haven't posted my actual database structure in my first note on this topic so here it is:

-- 'attrs' keeps the attributes list in CSV form CREATE TABLE categories ( id INT UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT, name CHAR(255) NOT NULL UNIQUE DEFAULT '', parent INT UNSIGNED NOT NULL DEFAULT '', attrs CHAR(255) NOT NULL DEFAULT '', time TIMESTAMP, PRIMARY KEY(id), KEY(name(16)), KEY(parent) ); -- _[id] is the id of the category as existent in 'categories' table CREATE TABLE cat_[id] ( id INT UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT, name CHAR(255) NOT NULL UNIQUE DEFAULT '', time TIMESTAMP, ... attribute ... attribute ... attribute PRIMARY KEY(id), KEY(name(16)) ); -- bloat table to speed up common search patterns CREATE TABLE items_all ( id INT UNSIGNED NOT NULL UNIQUE AUTO_INCREMENT, name CHAR(255) NOT NULL DEFAULT '', category CHAR(255) NOT NULL DEFAULT '', cat_id INT UNSIGNED NOT NULL, PRIMARY KEY(id), KEY(name(16)), KEY(category(16)), KEY(cat_id), );
As you see, it's quite a clean/lame implementation of the second variant, as stated in my previous post. footpad suggested a 'fields/attributes' table instead of my 'attrs' column in the categories table. It should be an improvement for a large number of attributes, but I'm thinking there will be no such category with more than let's say 20 attributes for each category.
Normalization is a good thing, not that I was not thinking about it, but the problem at the first (the second too :) glance is that it can't be no normalization as I want to keep the bazaar feature ;-) meaning that this system should be able to store people as well as books, tools, flavours, images or anything else disregarding any resemblance between categories. In fact the only relation that I'm thinking to enhance somehow is the one of father-child category: as many of you have noted, there could be many overlapping attributes in some cases (humans-:grown-ups-:artists) so maybe there some normalization should be considered. Heh, some OOP techniques would be better in these cases as inheritance. Even more, polimorphism would be needed if i.e. would like to move around items from one category to another (i.e. 'musicians' from 'humans/grownups/artists/' in 'music/') :)
Heh, it will get so complex that I think that KISS should come up.For speed up purposes there may be used additional redundant databases containing specialized table in various things etc (as my all_items table).
I have already used Swish and maybe I will use it for this project too. Cascade, never heard of, I'll give it a try.
tnx.

--
AltBlue.

Replies are listed 'Best First'.
Re: (2) database design
by mandog (Curate) on Nov 28, 2001 at 22:48 UTC
    It doesn't feel like you need a tree structure at all. Maybe I am missing something...

    If you really do need a tree then Joe Celko probably has what you need. (skip the travelogue at the start)

    table: Categories: category_desc | catID table: Items: item_desc | itemID | catID table: Attributes attribute_desc | AttributID | itemID SELECT category_desc, item_desc, attribute_desc FROM Categories, Items, Attributes WHERE Categories.catID = Items.catID AND Items.itemID = Attributes.itemID;
    This scheme has many advantages over the "multiple tables" solution. A couple are:
    1. You won't have to add a table every time you add a category.

    2. With cascading deletes, you don't have delete tables when you delete a category.

    This scheme has many advantages over the "one big table" solution. Three are:
    1. You won't have to add/delete columns if you add/delete attributes
    2. In a normalized table structure you have a lot more flexibility with the queries.

    3. if you change a description you will do it in only one place

    This scheme has no disadvantages over the two you have been considering. If performance is an issue. (and it is unlikely that it is) You are better off exploring caching of one sort or another.

    Apart from being off topic, you might do better by asking database questions on a database forum. The monks are a nice bunch, but not every problem is a nail.



    email: mandog