Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When you have a TEXT field in MySQL or PostgreSQL, should you put it in a separate table?

I've heard that if you have a table with a TEXT column that will hold a large chunk of text data, it's better for performance to move that column into a separate table and get it via JOINs to the base record.

Is this true, and if so why?

like image 477
dan Avatar asked Mar 11 '11 03:03

dan


People also ask

Can you have MySQL and PostgreSQL?

Can postgresql and mysql run together in the same machine ? yes. Ehm. There is no conflict between the 2 so you install mysql the normal way ( sudo apt-get install mysql-server ) , you install postgres ( sudo apt-get install postgresql postgresql-contrib ) the normal way.

How does PostgreSQL store TEXT?

Using text data type in PostgreSQL, we can store the unlimited length of the string. The text data type is stored data up to 1 GB in the field of a column. Varchar and text data type performance is the same in PostgreSQL. But varchar allows only to store 255 characters into the column.

Which is better between PostgreSQL and MySQL?

MySQL is a simpler database that's fast, reliable, well understood, and easy to set up and manage. PostgreSQL is an object-relational database (ORDBMS) with features like table inheritance and function overloading, whereas MySQL is a pure relational database (RDBMS).


1 Answers

Not with PostgreSQL, from the manual:

Very long values are also stored in background tables so that they do not interfere with rapid access to shorter column values.

So a large character column (such as TEXT or VARCHAR without a specified size limit) is stored away from the main table data. So, PostgreSQL has your "put it in a separate table" optimization built in. If you're using PostgreSQL, arrange your table sensibly and leave the data layout to PostgreSQL.

I don't know how MySQL or other RDBMs arrange their data.

The reason behind this optimization is that the database will usually keep the data for each row in contiguous blocks on disk to cut down on seeking when the row needs to be read or updated. If you have a TEXT (or other variable length type) column in a row then the size of the row is variable so more work is needed to go from row to row. An analogy would be the difference between accessing something in a linked list versus accessing an array; with a linked list, you have to read three elements one at a time to get to the fourth one, with an array you just offset 3 * element_size bytes from the beginning and you're there in one step.

like image 157
mu is too short Avatar answered Nov 10 '22 00:11

mu is too short