MySQL Forums
Forum List  »  General

Varchar Memory Usage
Posted by: Steve Howard
Date: July 29, 2013 10:47AM

I've created a table with a single varchar(256) column. I then inserted 500 K rows of varying size with the vast majority being substantially less than 256 characters.

I then attempt the following queries:

#1) select avg(length(pname)) from (
select substr(page_name,1,40) as pname from memory_test
) a;

-> returns 25.6563 in 0.811 sec

#2) select avg(length(pname)) from (
select substr(page_name,1,length(page_name)) as pname from memory_test
) a;

-> returns 26.6383 in 1.123 sec

#3) select avg(length(pname)) from (
select page_name as pname from memory_test
) a;

-> returns 26.6383 in 23.151 sec

As I understand it, the difference in performance between #2 and #3 is caused by the allocation of enough memory in #3 to accommodate each page_name being 256 characters.

Am I assuming correctly that application of a substr allows for dynamic determination of the amount of memory required? Is there another way to accomplish this without using the substr?

I have confirmed that there is at least 1 long varchar in the table:

select max(length(page_name)) from memory_test;

returns 255

I realize that I could rewrite the query without requiring the derived table. I've simplified a more complex query in order to clarify my question.

Any guidance appreciated,
Steve

Options: ReplyQuote


Subject
Written By
Posted
Varchar Memory Usage
July 29, 2013 10:47AM
July 30, 2013 09:10PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.