MySQL Table with TEXT column

后端 未结 6 544
太阳男子
太阳男子 2021-01-18 02:08

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 fr

相关标签:
6条回答
  • 2021-01-18 02:23

    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.

    0 讨论(0)
  • 2021-01-18 02:33

    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.

    0 讨论(0)
  • 2021-01-18 02:34

    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.

    0 讨论(0)
  • 2021-01-18 02:41

    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!

    0 讨论(0)
  • 2021-01-18 02:43

    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.

    You probably saw this, from the MySQL manual http://dev.mysql.com/doc/refman/5.5/en/optimize-character.html

    If a table contains string columns such as name and address, but many queries do not retrieve those columns, consider splitting the string columns into a separate table and using join queries with a foreign key when necessary. When MySQL retrieves any value from a row, it reads a data block containing all the columns of that row (and possibly other adjacent rows). Keeping each row small, with only the most frequently used columns, allows more rows to fit in each data block. Such compact tables reduce disk I/O and memory usage for common queries.

    Which indeed is telling you that in MySQL you are discouraged from keeping TEXT data (and BLOB, as written elsewhere) in tables frequently searched

    0 讨论(0)
  • 2021-01-18 02:49

    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

    0 讨论(0)
提交回复
热议问题