views:

133

answers:

5

Hello. I've been working on a database and I have to deal with a TEXT field.

Now, I believe I've seen some place mentioning it would be best to isolate the TEXT column from the rest of the table(putting it in a table of its own).

However, now I can't find this reference anywhere and since it was quite a while ago, I'm starting to think that maybe I misinterpreted this information.

Some research revealed this, suggesting that

Separate text/blobs from metadata, don't put text/blobs in results if you don't need them.

However, I am not familiar with the definition of "metadata" being used here.

So I wonder if there are any relevant advantages in putting a TEXT column in a table of its own. What are the potential problems of having it with the rest of the fields? And potential problems of keeping it in a separated table?

This table(without the TEXT field) is supposed to be searched(SELECTed) rather frequently. Is "premature optimization considered evil" important here? (If there really is a penalty in TEXT columns, how relevant is it, considering it is fairly easy to change this later if needed).

Besides, are there any good links on this topic? (Perhaps stackoverflow questions&answers? I've tried to search this topic but I only found TEXT vs VARCHAR discussions)

+2  A: 

Yep, it seems you've misinterpreted the meaning of the sentence. What it says is that you should only do a SELECT including a TEXT field if you really need the contents of that field. This is because TEXT/BLOB columns can contain huge amounts of data which would need to be delivered to your application - this takes time and of course resources.

Best wishes, Fabian

halfdan
+1  A: 

There might be some good reasons to separate a text field out of your table definition. For instance, if you are using an ORM that loads the complete record no matter what, you might want to create a properties table to hold the text field so it doesn't load all the time. However if you are controlling the code 100%, for simplicity, leave the field on the table, then only select it when you need it to cut down on data trasfer and reading time.

Zak
I'd rather ditch product that uses bad practices. However, if you want to keep it, and you can't directly control its SELECT list, you could always give it a VIEW.
Craig Young
+1  A: 

The concern is that a large text field—like way over 8,192 bytes—will cause excessive paging and/or file i/o during complex queries on unindexed fields. In such cases, it's better to migrate the large field to another table and replace it with the new table's row id or index (which would then be metadata since it doesn't actually contain data).

The disadvantages are: a) More complicated schema b) If the large field is using inspected or retrieved, there is no advantage c) Ensuring data consistency is more complicated and a potential source of database malaise.

wallyk
**Not at all!** TEXT data is not stored in the same pages as the actual data. All you _gain_ by moving your TEXT field to another table is additional overheads when you want to retrieve your text data.
Craig Young
+1  A: 

This is probably premature optimisation. Performance tuning MySQL is really tricky and can only be done with real performance data for your application. I've seen plenty of attempts to second guess what makes MySQL slow without real data and the result each time has been a messy schema and complex code which will actually make performance tuning harder later on.

Start with a normalised simple schema, then when something proves too slow add a complexity only where/if needed.

As others have pointed out the quote you mentioned is more applicable to query results than the schema definition, in any case your choice of storage engine would affect the validity of the advice anyway.

If you do find yourself needing to add the complexity of moving TEXT/BLOB columns to a separate table, then it's probably worth considering the option of moving them out of the database altogether. Often file storage has advantages over database storage especially if you don't do any relational queries on the contents of the TEXT/BLOB column.

Basically, get some data before taking any MySQL tuning advice you get on the Internet, including this!

Dan Head
A: 

The data for a TEXT column is already stored separately. Whenever you SELECT * from a table with text column(s), each row in the result-set requires a lookup into the text storage area. This coupled with the very real possibility of huge amounts of data would be a big overhead to your system.

Moving the column to another table simply requires an additional lookup, one into the secondary table, and the normal one into the text storage area.

The only time that moving TEXT columns into another table will offer any benefit is if there it a tendency to usually select all columns from tables. This is merely introducing a second bad practice to compensate for the first. It should go without saying the two wrongs is not the same as three lefts.

Craig Young