@@ -121,6 +121,31 @@ Go 1.1, but not all drivers are smart enough to do that. Caveat Emptor.
121
121
Naturally prepared statements and the management of prepared statements cost
122
122
resources. You should take care to close statements when they are not used again.
123
123
124
+ Avoiding Prepared Statements
125
+ ============================
126
+
127
+ Go creates prepared statements for you under the covers. A simple
128
+ ` db.Query(sql, param1, param2) ` , for example, works by preparing the sql, then
129
+ executing it with the parameters and finally closing the statement.
130
+
131
+ Sometimes a prepared statement is not what you want, however. There might be
132
+ several reasons for this:
133
+
134
+ 1 . The database doesn't support prepared statements. When using the MySQL
135
+ driver, for example, you can connect to MemSQL and Sphinx, because they
136
+ support the MySQL wire protocol. But they don't support the "binary" protocol
137
+ that includes prepared statements, so they can fail in confusing ways.
138
+ 2 . The statements aren't reused enough to make them worthwhile, and security
139
+ issues are handled in other ways, so performance overhead is undesired. An
140
+ example of this can be seen at the
141
+ [VividCortex blog](https://vividcortex.com/blog/2014/11/19/analyzing-prepared-statement-performance-with-vividcortex/).
142
+
143
+ If you don't want to use a prepared statement, you need to use ` fmt.Sprint() ` or
144
+ similar to assemble the SQL, and pass this as the only argument to ` db.Query() `
145
+ or ` db.QueryRow() ` . And your driver needs to support plaintext query execution,
146
+ which is added in Go 1.1 via the ` Execer ` interface,
147
+ [ documented here] ( http://golang.org/pkg/database/sql/driver/#Execer ) .
148
+
124
149
Single-Row Queries
125
150
==================
126
151
0 commit comments