Comments on: Be careful when joining on CONCAT http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/ Everything about MySQL Performance Sat, 21 Nov 2009 05:23:57 -0800 http://wordpress.org/?v=2.8.4 hourly 1 By: techblog.tilllate.com » Peter Zaitsev of the MySQL Performance Blog speaks in Zurich- http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-197105 techblog.tilllate.com » Peter Zaitsev of the MySQL Performance Blog speaks in Zurich- Sun, 18 Nov 2007 18:29:35 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-197105 [...] He and his partners of Percona write about topics like eliminating ORDER BY function or Be careful when joining on CONCAT. Peter also held presentations at all Mysql Conferences. In 2007, for example he talked about [...] [...] He and his partners of Percona write about topics like eliminating ORDER BY function or Be careful when joining on CONCAT. Peter also held presentations at all Mysql Conferences. In 2007, for example he talked about [...]

]]>
By: Aurimas http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-178108 Aurimas Wed, 17 Oct 2007 11:56:54 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-178108 Paul, thanks. Actually, what you mentioned - was my very first recommendation for the customer. Thing is, in this case - there is a number of different tables in place of tb2 joined and in each case - different prefix is used. As query is really executed only occasionally, having it executed in less than a second (instead of few minutes) is enough and it does not cause additional disk/memory waste. But, I agree- schema should be planned to avoid that sort of join in advance. Paul,

thanks. Actually, what you mentioned – was my very first recommendation for the customer. Thing is, in this case – there is a number of different tables in place of tb2 joined and in each case – different prefix is used. As query is really executed only occasionally, having it executed in less than a second (instead of few minutes) is enough and it does not cause additional disk/memory waste.

But, I agree- schema should be planned to avoid that sort of join in advance.

]]>
By: paul http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-178080 paul Wed, 17 Oct 2007 10:34:21 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-178080 @peter I understand where your coming from and whilst I don't recommend wrapping crap design with hacks, you could actually eliminate the concat from the select query by adding an additional column to the table and use a trigger which updates this additional column by performing the concat at point of data insert/update. Then by changing the select query to use our new column we have eliminated the concat on where clause and have got all the speed benefits of a properly designed schema. @peter

I understand where your coming from and whilst I don’t recommend wrapping crap design with hacks, you could actually eliminate the concat from the select query by adding an additional column to the table and use a trigger which updates this additional column by performing the concat at point of data insert/update. Then by changing the select query to use our new column we have eliminated the concat on where clause and have got all the speed benefits of a properly designed schema.

]]>
By: peter http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-178045 peter Wed, 17 Oct 2007 09:14:24 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-178045 Thanks Daniel, I wanted to add one more thing about it - generally this choice of making Number a binary string is counter intuitive to me. The number is a string and for numbers it does not matter if it is case sensitive and case insensitive so I'd see CONCAT to simply handle numbers by converting them to the type of other argument. Though I do not know may be this more intuitive solution would some ugly side effects. Thanks Daniel,

I wanted to add one more thing about it – generally this choice of making Number a binary string is counter intuitive to me.

The number is a string and for numbers it does not matter if it is case sensitive and case insensitive so I’d see CONCAT to simply handle numbers by converting them to the type of other argument.

Though I do not know may be this more intuitive solution would some ugly side effects.

]]>
By: peter http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-178038 peter Wed, 17 Oct 2007 09:10:05 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-178038 Dale, Paul This would be the case if we would live in the perfect world. True in the database with good design you would not join on CONCAT but there are a lot of databases which are written with not so good design, and these are the ones which we're called to fix quite commonly. Often to do it right you would need to redo quite a lot, however the customer may not be ready for that in many cases so we end up squeezing as much as we can from the current schema which brings us to deal with strange queries which bring various weird issues. Dale, Paul

This would be the case if we would live in the perfect world. True in the database with good design you would not join on CONCAT but there are a lot of databases which are written with not so good design, and these are the ones which we’re called to fix quite commonly.

Often to do it right you would need to redo quite a lot, however the customer may not be ready for that in many cases so we end up squeezing as much as we can from the current schema which brings us to deal with strange queries which bring various weird issues.

]]>
By: Daniel Schneller http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-178037 Daniel Schneller Wed, 17 Oct 2007 09:07:56 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-178037 While I generally agree that the schema design is suboptimal if you get into situations like this, sometimes you just cannot help it. Unfortunately(?) we do not live in a perfect world, and existing systems cannot always easily be changed. So it is important to know the caveats that might bite you if you have to do such things. While I generally agree that the schema design is suboptimal if you get into situations like this, sometimes you just cannot help it. Unfortunately(?) we do not live in a perfect world, and existing systems cannot always easily be changed. So it is important to know the caveats that might bite you if you have to do such things.

]]>
By: Dale http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-177856 Dale Wed, 17 Oct 2007 00:52:06 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-177856 I agree. I had the same initial thought. I could not think of a time that I would need to use a concat() within the where clause. The whole idea behind a good scheme model is that you use good keys that match up without needing further tweaking. I agree. I had the same initial thought. I could not think of a time that I would need to use a concat() within the where clause. The whole idea behind a good scheme model is that you use good keys that match up without needing further tweaking.

]]>
By: paul http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/comment-page-1/#comment-177821 paul Tue, 16 Oct 2007 23:24:35 +0000 http://www.mysqlperformanceblog.com/2007/10/16/be-careful-when-joining-on-concat/#comment-177821 I am surprised to see this blog posting, as there is nothing to be careful about joining on concat, simply if you are doing that your DB design is grossly wrong. Stop and think how ridiculous it is to be concat'ing a string on every query and joining on it. I am surprised to see this blog posting, as there is nothing to be careful about joining on concat, simply if you are doing that your DB design is grossly wrong. Stop and think how ridiculous it is to be concat’ing a string on every query and joining on it.

]]>