开发者

Query fast in latin1, slow in utf8 - why?

I have a query that goes something like this:

 SELECT DISTINCT table1.id, {long list of fields} FROM table1 
     INNER JOIN table2 ON table1.table2_id = table2.id 
     {... more joins ...} 
     LEFT JOIN table_last ON table_last.id=some_table.last_id
     WHERE ( table_last.id IS NULL) AND {...more conditions...}
     ORDER BY table1.date_entered desc LIMIT 0,6

This query on the same database runs fine (<1s runtime) when run with latin1 as client charset, and is extremely slow (couldn't wait for it to finish) after SET NAMES 'utf8'. The query returns 70 rows (the part before the limit, of course), so the result set size should not be a problem. I checked all tables in all joins and they all seem to have UTF-8 as their charset (I checked with SHOW TABLE CREATE).

What could caus开发者_JAVA百科e such strange behavior? How utf8 in this case is so much worse that latin1? In case it's relevant, the ID field are char(36) everywhere, and joins have conditions based on such fields and integer fields and varchar fields.

P.S. I know DISTINCT can take time, but I can't remove it and it's 70 rows anyway and it is fast on default (latin1) setting! So it looks like something external to the query, but what?


When you table us using utf8 it allocates 3x the length of a varchar for each row (256 * 3 = 768 bytes)!

This will mean that your queries use up more resources now that the strings take up three times the space - so buffers aren't as big and you may have to swap out if there are many queries going on at the same time - this would further degrade your query/server's performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜