Comments on: MySQL: TEXT vs. VARCHAR Performance https://nicj.net/mysql-text-vs-varchar-performance/ Home to Nic Jansma, a software developer at Akamai building high-performance websites, apps and open-source tools. Sun, 30 Sep 2018 10:04:42 +0000 hourly 1 https://wordpress.org/?v=5.5.3 By: i54R4 https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1245056 Sun, 30 Sep 2018 10:04:42 +0000 http://nicj.net/?p=714#comment-1245056 Thanks for great article

]]>
By: Vahn https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1229404 Thu, 09 Mar 2017 00:56:08 +0000 http://nicj.net/?p=714#comment-1229404 Is this behavior same with VARCHAR or TEXT declared inside triggers?

]]>
By: Richard Willis-Owen https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1228306 Mon, 09 Jan 2017 10:44:31 +0000 http://nicj.net/?p=714#comment-1228306 Thanks for the useful research. This is still valid in 2017 with MySQL 5.7 as the MEMORY engine still uses a fixed length row format and the defaults for tmp_table_size and max_heap_table_size haven’t changed (16MB).

]]>
By: Kunal https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1221760 Thu, 03 Sep 2015 21:12:46 +0000 http://nicj.net/?p=714#comment-1221760 Many years later this post helped me a ton. I noticed the same drop in performance on a batch write of 8000 rows, basically transferring raw data from Redis into MySQL with some massaging in between (scraper data –> sql).

100 columns long, 1/3 decimal, 1/3 text, 1/3 string. I originally stored the string as text, worked well, started using varchar(511) at some point, performance for the batch write dropped immensely. I’m no longer diving into my swap space and in fact my CPU utilization dropped very significantly too.

This post confirmed to me what I suspected was happening but don’t have the debugging chops to prove — I also couldn’t find anything on SO directly (though ultimately a comment from SO led me here).

Thanks!

]]>
By: Nic https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1219450 Mon, 27 Oct 2014 00:25:57 +0000 http://nicj.net/?p=714#comment-1219450 @ Andrey

Makes sense! Your first query will join the two tables, apply the WHERE, then ORDER, then LIMIT to the first row. This join might be too big for the temp tables, so it persists to disk. The second query does the sub-query first (without a JOIN) then does a simple ID match.

]]>
By: Andrey https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1218849 Wed, 15 Oct 2014 03:09:45 +0000 http://nicj.net/?p=714#comment-1218849 Sorry, I was wrong saying that explain plans are the same for the second (quick) reuqest there is another explain plan:

(id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra) VALUES
(1, ‘PRIMARY’, ‘log_request’, ‘index’, NULL, ‘PRIMARY’, ‘8’, NULL, 1, ‘Using where’),
(2, ‘DEPENDENT SUBQUERY’, ‘re’, ‘ref’, ‘FKB3DF0473B2FF1006’, ‘FKB3DF0473B2FF1006’, ‘9’, ‘func’, 1, ‘Using where; Using index’),
(2, ‘DEPENDENT SUBQUERY’, ‘r’, ‘eq_ref’, ‘PRIMARY’, ‘PRIMARY’, ‘8’, ‘func’, 1, ‘Using where; Using index’)

]]>
By: Andrey https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1218848 Wed, 15 Oct 2014 02:52:31 +0000 http://nicj.net/?p=714#comment-1218848 Sorry for my English. Thank you for your post. I’ve encountered same problem and found this post looking for a solution. I have 2 tables with about 400000 and 200000 rows which are joined by FK and PK. Minimizing the slow request I noticed that there are 3 conditions which altogether make the request slow: 1) join or 2 tables in FROM and their connection in WHERE; 2) order by (even simple order by id (PK); 3) two fields in SELECT are varchar(2000).

I found the following explanation from MySQL developers: http://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html and it seems the problem is in their “modified filesort algorithm”. For me it looks like a MySQL bug. Simple question: why at all MySQL operates with varchar fields of all row ignoring the fact that we need only a few rows in result? In your example you limited the result. I did the same limiting to 1 record. What stops MySQL from generation only some row ids on the first phase and asking later full number of fields specified in SELECT? They did it before optimization. So I think they have to optimize algorithm better.

And at the end the simple solution.

I changed my SQL from

SELECT
r.query, r.headers
FROM
log_request r , log_request_estimate re
WHERE
r.id = re.request_id
ORDER BY
r.id
LIMIT 1;

wrapping it with another SELECT:

SELECT body, headers FROM log_request
WHERE id IN (
SELECT r.id
FROM
log_request r , log_request_estimate re
WHERE
r.id = re.request_id and r.id<1000000
)
ORDER BY id
LIMIT 1;

I don't know exactly why does it work and I haven't yet tried it with my original request which is much more complex.
Explain command gives me the same result as for the slow request:

(id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra) VALUES
('1', 'SIMPLE', 'this_', 'index', 'FKB3DF0473B2FF1006', 'FKB3DF0473B2FF1006', '9', NULL, '76512', 'Using index; Using temporary; Using filesort'),
('1', 'SIMPLE', 'request1_', 'eq_ref', 'PRIMARY', 'PRIMARY', '8', 'petapi.this_.request_id', '1', '')

]]>
By: pip https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-1210962 Sat, 17 May 2014 00:25:23 +0000 http://nicj.net/?p=714#comment-1210962 Thanks for this! It’s an interesting journey and well documented. I’d never really thought about the impact of a temporary table having a different storage engine to the “source” tables.

So the key takeout here is VARCHARs are represented as fixed-size in memory tables (i.e. effectively as CHARS and losing their advantage of shrinking to the data), whereas TEXT columns cause temp tables to skip memory entirely and go to disk. Both can run slow… depending on the shape of your data.

This is why most of the S.O. posts and mySQL say benchmark for your hardware and data I guess!

And another reason to avoid unnecessary select *’s

]]>
By: Nic https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-9086 Mon, 15 Oct 2012 14:47:40 +0000 http://nicj.net/?p=714#comment-9086 In reply to gino.

Yes, that’s a good option as well. Be sure to read the MySQL manual for times that you shouldn’t use tmpfs (eg, replication slaves).

http://dev.mysql.com/doc/refman/5.0/en/temporary-files.html

]]>
By: gino https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-9078 Thu, 11 Oct 2012 21:19:31 +0000 http://nicj.net/?p=714#comment-9078 loved this very informative well written article ! what about using an in-memory filesystem for mysql tmp dir ? so even ‘on disk’ temporary table would fit in ram.

]]>
By: Patrick Killelea https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-9073 Wed, 03 Oct 2012 19:29:31 +0000 http://nicj.net/?p=714#comment-9073 Thanks for this article. I was just about to try converting my own text columns to varchar, but now I think I won’t do that.

]]>
By: xsign https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-7511 Wed, 16 May 2012 21:51:07 +0000 http://nicj.net/?p=714#comment-7511 Perfect! Helped a lot!

]]>
By: MySQL VARCHAR Performance « Constantly Underwhelmed https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6987 Tue, 17 Jan 2012 01:47:40 +0000 http://nicj.net/?p=714#comment-6987 […] http://nicj.net/2011/01/20/mysql-text-vs-varchar-performance […]

]]>
By: unity https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6842 Sat, 12 Nov 2011 09:05:33 +0000 http://nicj.net/?p=714#comment-6842 Great article really helped me . thx

]]>
By: xiaochong0302 https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6841 Wed, 09 Nov 2011 03:40:43 +0000 http://nicj.net/?p=714#comment-6841 Thank you nic, it let me know more about how mysql works.
I think store text in another table is always a good idea.

]]>
By: sharin ahmad https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6839 Fri, 28 Oct 2011 03:01:17 +0000 http://nicj.net/?p=714#comment-6839 Great article and very useful for me. Thanks for sharing.

]]>
By: Jose Luis Loya https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6830 Sat, 15 Oct 2011 17:42:07 +0000 http://nicj.net/?p=714#comment-6830 Really good article. Thank you for taking the time to explain us.

]]>
By: Anjum https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6819 Wed, 21 Sep 2011 12:51:34 +0000 http://nicj.net/?p=714#comment-6819 Hi,

I am working on one project. We were asking users to share their stories. The file in DB against that story is varchar (512) , but the users posted stories greater than varchar (512), now client wants us to retrieve full story.
Is there any way in mysql where i can retrieve the full stories OR simple this is not possible ?

]]>
By: oDesk https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6755 Wed, 11 May 2011 14:40:56 +0000 http://nicj.net/?p=714#comment-6755 you’re Testing maniac =)

]]>
By: Nic https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6754 Wed, 11 May 2011 05:36:36 +0000 http://nicj.net/?p=714#comment-6754 @Melle
That might work be a good solution to this type of issue as well — great idea.

]]>
By: Nic https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6753 Wed, 11 May 2011 05:33:16 +0000 http://nicj.net/?p=714#comment-6753 @Shetil
Agreed, and Shame On Me for not testing any of this first! I made the original change blindly with preconceived assumptions about how my change would behave. Had I tested this first, I would have noticed the problem right away.

As for trying to improve the original 30ms query, the query was actually a significant part of the average page load time. The page that it was on took an average in 70ms before my change, so the query was contributing 42% of that time.

]]>
By: Avoin Tyopaikka https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6752 Tue, 10 May 2011 20:11:36 +0000 http://nicj.net/?p=714#comment-6752 Thanks for great article, I didn’t realize the performance difference is so big. Greets, Avoin Tyopaikka

]]>
By: MySQL Type Performance (varchar vs text) | My Thought Exactly https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6750 Tue, 10 May 2011 14:00:01 +0000 http://nicj.net/?p=714#comment-6750 […] full article can be viewed here. Posted by administrator on Tuesday, May 10, 2011, at 7:59 AM. Filed under Uncategorized. Follow […]

]]>
By: Mobile Phone https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6749 Tue, 10 May 2011 13:30:51 +0000 http://nicj.net/?p=714#comment-6749 Great article. Did you also try creating these fields as indexes, I would be very interested to see if by making them both indexes if this has the same result…..

]]>
By: Dave Rapin https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6748 Tue, 10 May 2011 13:06:47 +0000 http://nicj.net/?p=714#comment-6748 Very informative post about some of the MySQL varchar nuances I was not aware of. I usually limit them to around 8k then switch to text, but for no good reason that I can remember. This in depth explanation will definitely come in useful.

]]>
By: Melle https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6747 Tue, 10 May 2011 11:10:26 +0000 http://nicj.net/?p=714#comment-6747 Hi Nic,

Thanks for your post! Just pondering this issue (without actually testing it) there might be another solution: Have you considered performing a smaller join operation (i.e. on t1.id and t2.id only) in a subquery first and left joining the changed VARCHAR column later?

Kind regards,
Melle

]]>
By: Shetil https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6746 Tue, 10 May 2011 11:06:40 +0000 http://nicj.net/?p=714#comment-6746 Nice read, but this a good example how not to do optimization. To change stuff based on assumption, without performance profiling, is the wrong way of doing things. Your own testing afterwards shows that the query only took .03 second and I would guess that there is several other areas in your code that is much slower than that.

]]>
By: expert https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6745 Tue, 10 May 2011 10:20:04 +0000 http://nicj.net/?p=714#comment-6745 Well, this one solves my long time quesiton about vchar and text.
Thanks for sharing!

]]>
By: Joey https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6744 Tue, 10 May 2011 10:11:55 +0000 http://nicj.net/?p=714#comment-6744 Really nice article.

]]>
By: Guy Kastenbaum https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6743 Tue, 10 May 2011 09:56:20 +0000 http://nicj.net/?p=714#comment-6743 Thanks for the article. And I like your way of digging things. Sorry for all the work “to put the server back to normal”.

]]>
By: Richard Wendrock https://nicj.net/mysql-text-vs-varchar-performance/comment-page-1/#comment-6742 Mon, 09 May 2011 14:31:23 +0000 http://nicj.net/?p=714#comment-6742 This is a very good article! Thanks for taking the time to share.

]]>