Search code examples
mysqlperformancetextvarchar

varchar or text - mysql performance


if we have table such:

create table x(
   id int primary key,
   something_else_1 int,
   something_else_2 int,
   something_else_3 int,

   char_data text, -- or varchar(1000)
);

this table will be queried on all fields except char_data.

most queries will be similar to:

select id, something_else_1
from x
where something_else_2 = 2 and something_else_3 = 5;

question is - if we have indexes etc,

what configuration will be better - text or varchar.

Just one final note -

I know I can separate this into two tables, buy separation in this case will be not the best idea, since all fields except the blob's will be something like unique index or similar.


Solution

  • this table will be queried on all fields except char_data.

    Then data type of char_data has no influence over performance. Only if you select char_data it'll consume more bandwidth. Nothing else.

    Its not a problem. Because you are not using in your sql. SELECT * will become slow but SELECT id, something_else_1 will not make it slow. WHERE id=2 and something_else_2=1 has no effect, but WHERE char_data like '%charsequence%'. As long as you are not searching your table with char_data you are safe.
    Besides if you still want to search by char_data, you should enable full text search.

    ALTER TABLE `x` ADD FULLTEXT(`char_data`); 
    

    Note: Full text search is only supported in MyISAM table engine.