<p>What sort of database schema would you use to store email messages, with as much header information as practical/possible, into a database?</p>

<p>Assume that they have been fed into a script from the MTA and parsed into the relevant headers/body/attachments.</p>

<p>Would you store the message body whole in the database table, or split any MIME-parts apart?  What about attachments?</p>

[{'answer_id': 65013, 'author': 'ceejayoz', 'author_id': 1902010, 'author_profile': 'https://Stackoverflow.com/users/1902010', 'pm_score': 2, 'selected': False, 'text': "<p>Depends on what you're going to be doing with it.  If you're going to need to do frequent searching against certain bits of it, you'll want to break it up in a way that makes sense for your usage case.  If it's just for something like storage of e-mail for Sarbanes-Oxley compliance, you'd probably be okay storing the whole thing - headers, parts, etc. - as one big text field.</p>\n"}, {'answer_id': 65019, 'author': 'Sklivvz', 'author_id': 7028, 'author_profile': 'https://Stackoverflow.com/users/7028', 'pm_score': 1, 'selected': False, 'text': '<p>It all depends on what you want to do with the data, but in general I would want to store all data and also make sure that the semantics interpreted by the MUA are preserved in the db, so for example:\n- All headers that are parsed should have their own column\n- A column should contain the whole headers\n- The attachments (including body, multipart) should be in a many to one table with the email table.</p>\n'}, {'answer_id': 65044, 'author': 'Ivan Bosnic', 'author_id': 3221, 'author_profile': 'https://Stackoverflow.com/users/3221', 'pm_score': 2, 'selected': False, 'text': '<p>Suggestion: create a well defined table for storing e-mail with a column for each relevant part of a message: sender, header, subject, body. It is going to be much simpler later if you want to query, for example, by subject field. In the same table you can define a field to keep the path of a attachment and store the attached file on the file system, rather than storing it in blob fields.</p>\n'}, {'answer_id': 65145, 'author': 'Charles Graham', 'author_id': 7705, 'author_profile': 'https://Stackoverflow.com/users/7705', 'pm_score': 0, 'selected': False, 'text': '<p>If it is already split up, and you can be sure that the routine to split the data is sound, then I would split up the table as granular as possible.  You can always parse it back together in your middle tier.  If space is not an issue, you could always store it twice.  One, split up into the relevant fields, and another field that has the whole thing as one blob, if putting it back together is hard.</p>\n'}, {'answer_id': 66526, 'author': 'Chase Seibert', 'author_id': 7679, 'author_profile': 'https://Stackoverflow.com/users/7679', 'pm_score': 3, 'selected': False, 'text': "<p>You may want to use a schema where the message body and attachment records can be shared between multiple recipients on the message. It's not uncommon to see email servers where fully 50% of the disk storage is used by duplicate emails. </p>\n\n<p>A simple hash of the body/attachment would be enough to see if that record was already in the database. However, you would still need to keep separate headers.</p>\n"}, {'answer_id': 84280, 'author': 'Kluge', 'author_id': 8752, 'author_profile': 'https://Stackoverflow.com/users/8752', 'pm_score': 1, 'selected': False, 'text': "<p>You'll probably want to at least store attachments separately to optimize storage.  It's astonishing to see the size and quantity of attachments (videos, etc.) that most users unhesitatingly attach to emails.</p>\n\n<p>In the case of outgoing emails you may have multiple emails sending the same attachment.  It's far more efficient to store a single copy of the attachment that is referenced by all emails that share it.</p>\n\n<p>Another reason for storing attachments separately is that it gives you some archiving options later on.  Should storage space become an issue, you can always go back and delete large attachments older than a given date in order to compact the database.</p>\n"}, {'answer_id': 85442, 'author': 'Milen A. Radev', 'author_id': 15785, 'author_profile': 'https://Stackoverflow.com/users/15785', 'pm_score': 4, 'selected': False, 'text': '<p>You may want to check the <a href="http://www.archiveopteryx.org/architecture" rel="noreferrer">architecture</a> and the <a href="http://www.archiveopteryx.org/schema" rel="noreferrer">DB schema</a> of "Archiveopteryx".</p>\n'}, {'answer_id': 4149301, 'author': 'Gareth Rees', 'author_id': 68063, 'author_profile': 'https://Stackoverflow.com/users/68063', 'pm_score': 2, 'selected': False, 'text': '<p>An important step in database schema design is to figure out what types of entity you want to model. For this application the entities might be:</p>\n\n<ul>\n<li>Messages</li>\n<li>E-mail addresses</li>\n<li>Conversation threads (perhaps: if you want to do efficient threading)</li>\n<li>Attachments (perhaps: as suggested in other answers)</li>\n<li>...</li>\n</ul>\n\n<p>Once you know the entities, you can identify relationships between entities, which can be represented by tables:</p>\n\n<ul>\n<li>Messages have a many-many relationship to messages (<code>In-Reply-To</code> and <code>References</code> headers).</li>\n<li>Messages have a many-many relationship to e-mail addresses (<code>From</code>, <code>To</code>, <code>Cc</code> etc headers).</li>\n<li>Messages have a many-one relationship with threads.</li>\n<li>Messages have a many-many relationship with attachments.</li>\n<li>...</li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/65001', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6216/']