views:

219

answers:

1

Hello,

I'm trying to build (right now just thinking/planning/drawing relations :] ) little modular system to build basic websites (mostly to simplify common tasks we as webdesigners do routinely).

I got little stuck with database design / whole idea of storing content.

1., What is mostly painful on most of websites (from my experience), are pages with quasi same layout/skelet, with different information - e.g. Title, picture, and set of information - but, making special templates / special modules in cms happens to cost more energy than edit it as a text - however, here we lose some operational potential - we can't get "only titles", because, CMS/system understands whole content as one textfield

So, I would like to this two tables - one to hold information what structure the content has (e.g. just variable amount of photos <1;500) :], title & text & photo (large) & gallery) - HOW - and another table with all contents, modules and parts of "collections" (my working name for various structured information) - WHAT

table module_descriptors (HOW)  
id int  
structure - *???*  

table modules (WHAT)  
id int  
module_type - @link to module_descriptors id
content - *???*  

2., What I like about this is - I don't need many tables - I don't like databases with 6810 tables, one for each module, for it's description, for misc. number to text relations, ... and I also don't like tables with 60 columns, like content_us, content_it, category_id, parent_id.

I'm thinking I could hold the structure description and content itself (noted the ??? ?) as either XML or CSV, but maybe I'm trying to reinvent the wheel and answer to this is hidden in some design pattern I haven't looked into.

Hope I make any sense at all and would get some replies - give me your opinion, pros, cons... or send me to hell. Thank you

EDIT: My question is also this: Does this approach make sense? Is it edit-friendly? Isn't there something better? Is it moral? Don't do kittens die when I do this? Isn't it too much for server, If I want to read&compare 30 XMLs pulled from DB (e.g. I want to compare something)? The technical part - how to do it - is just one part of question:)

A: 

The design pattern you're hinting at is called Serialized LOB. You can store some data in the conventional way (as columns) for attributes that are the same for every entry. For attributes that are variable, format them as XML or MarkDown or whatever you want, and store it in a TEXT BLOB.

Of course you lose the ability to use SQL expressions to query individual elements within the BLOB. Anything you need to use in searching or sorting should be in conventional columns.


Re comment: If your text blob is in XML format, you could search it with XML functions supported by MySQL 5.1 and later. But this cannot benefit from an index, so it's going to result in very slow searches.

The same is true if you try to use LIKE or RLIKE with wildcards. Without using an index, searches will result in full table-scans.

You could also try to use a MySQL FULLTEXT index, but this isn't a good solution for searching XML data, because it won't be able to tell the difference between text content and XML tag names and XML attributes.

So just use conventional columns for any fields you want to search or sort by. You'll be happier that way.


Re question: If your documents really require variable structure, you have few choices. When used properly, SQL assumes that every row has the same structure (that is, columns). Your alternatives are:

Some people resort to an antipattern called Entity-Attribute-Value (EAV) to store variable attributes, but honestly, don't go there. For a story about how bad this can go wrong, read this article: Bad CaRMa.

Bill Karwin
If it's in XML, I can still search for contents of it, can't I? I also added EDIT with another question :) Thank you so far.
Adam Kiss
Thank you for Re's unfortunately I have no energy to look into it and translate all that right now - will read it and post later :)
Adam Kiss
Also see my presentation: http://www.slideshare.net/billkarwin/practical-object-oriented-models-in-sql
Bill Karwin
@Bill - wouldn't solution to search be something like - module structure in BLOB/TEXT in XML and another row called "words","flags", "tags" or "content" for search? BTW, I'll probably vote for you not because you're the only one, but for awesome responses.
Adam Kiss
Sure, you could use another column (not row) for words you want to search for, if that would allow you to find the document you need. Then use a FULLTEXT index on that "words" column.
Bill Karwin
solution found, thank you (yes, i thought column, but as english is not my main language and i'm pretty tired, my english is getting sloppy :] ), you've hard earned your 10 points for reputation :D
Adam Kiss