tags:

views:

126

answers:

3

Recently one of my colleagues made a comment that I should not use

LIKE '%'||?||'%'

rather use

LIKE ?

in the SQL and then replace the LIKE ? marker with LIKE '%'||?||'%' before I execute the SQL. He made the point that with a single parameter marker DB2 database will cache the statement always and thus cut down on the SQL prepare time.

However, I am not sure if it is accurate or not. To me it should be the other way around since we are doing more processing by doing a string replace on the SQL everytime the query is getting executed.

Does anyone know if a single marker really speeds up execution? Just FYI - I am using Spring 2.5 JDBC framework and the DB2 version is 9.2.

My question is - does DB2 treat "LIKE ?" differently from "LIKE '%'||?||'%'" as far as caching and preparation goes.

+1  A: 

I haven't done too much DB2, not since the 90's and I'm not really sure if I'm understanding what your underlying question is. Way back then I got a phone call from the head of the DBA team. "What are you doing different than every other programmer we've got!??" Mind you, this was early in my career, so tentatively I answered, "Nothing....", imagine it in kind of a whiny voice. "Well then, why do your queries take 50% of the cpu resources of any the other guys???". I took a quick poll of all the other guys and found I was the only one using prepared statements. Now under the covers Spring automatically makes prepared statements, and they've improved statement caching in the database a lot over the years, but if you make use of the properly, you can get the speedup there, AND it'll make the statement cache swap things out less often. It really depends on your use case, if you're only going to hit the query once, then there would be no difference, if its a few thousand times, obviously it would make a much greater difference.

mezmo
+1  A: 

in the SQL and then replace the LIKE ? marker with LIKE '%'||?||'%' before I execute the SQL. He made the point that with a single parameter marker DB2 database will cache the statement always and thus cut down on the SQL prepare time.

Unless DB2 is some sort of weird alien SQL database, or if it's driver does some crazy things, then the database server will never see your prepared statement until you actually execute it. So you can swap clauses in and out of the PreparedStatement all day long, and it will have no effect until you actually send it to the server when you execute it.

matt b
I think so too. I don't know much about DB2 myself - so that's why I posted it as a question to see if there are DB2 experts who can help me clear it up.
CoolBeans
Does database know about it when you create the PreparedStmt object or does it only know about it when you have done the execute query on it only?
CoolBeans
I've always assumed the latter. You could test this pretty trivially by setting up a test program that constructs the PS, waits 10 seconds before executing it, and uses WireShark to inspect the traffic between the machine and the database.
matt b
+1  A: 

'LIKE ?' is a PreparedStatement. Prepared statements are an optimization at the JDBC driver level. The thinking is that databases analyze queries to decide how to most efficiently process them. The DB can then cache the resulting query plan, keyed on the full statement. Reusing identical statements reuses the query plan. So basically if you are running the same query multiple times with different comparison strings, and if the query plan stays cached, then yes, using 'LIKE ?' will be faster.

Some useful (though somewhat dated) info on PreparedStatements:

Jeff
Isn't LIKE ‘%’||?||‘%’ also dynamic parameter? Or you don't think so because the % in the quotes?
CoolBeans
No, that isn't a dynamic parameter in this case. You shouldn't need to use quotes with parameters (one of the reasons they help prevent SQL injection issues) and you definitely don't want to do concatenation with something that is intended to be a placeholder.
Jeff