Django

Code

Ticket #3151 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

[patch] "SET NAMES utf8" is sent before every query

Reported by: smurf@smurf.noris.de Assigned to: adrian
Milestone: Component: Database layer (models, ORM)
Version: SVN Keywords: mysql
Cc: mir@noris.de, dev@simon.net.nz Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

No, we do not need to send a "SET NAMES utf8" to the server before every mysql request. Just before the first one.

Depending on the latency of your database connection and/or the prevalence of cached queries, this change may significantly reduce your response time.

diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
index e7e060e..28c5b1c 100644
--- a/django/db/backends/mysql/base.py
+++ b/django/db/backends/mysql/base.py
@@ -98,9 +98,11 @@ class DatabaseWrapper(local):
                 kwargs['port'] = int(settings.DATABASE_PORT)
             kwargs.update(self.options)
             self.connection = Database.connect(**kwargs)
-        cursor = self.connection.cursor()
-        if self.connection.get_server_info() >= '4.1':
-            cursor.execute("SET NAMES 'utf8'")
+            cursor = self.connection.cursor()
+            if self.connection.get_server_info() >= '4.1':
+                cursor.execute("SET NAMES 'utf8'")
+        else:
+            cursor = self.connection.cursor()
         if settings.DEBUG:
             return util.CursorDebugWrapper(MysqlDebugWrapper(cursor), self)
         return cursor

Attachments

3151.diff (0.8 kB) - added by dev@simon.net.nz on 12/18/06 16:26:18.

Change History

12/16/06 16:23:59 changed by anonymous

  • cc set to mir@noris.de.

12/18/06 16:24:49 changed by dev@simon.net.nz

  • cc changed from mir@noris.de to mir@noris.de, dev@simon.net.nz.
  • keywords set to mysql.
  • version set to SVN.

Passes all tests using the mysql backend at r4227.

----------------------------------------------------------------------
Ran 66 tests in 14.864s

OK

12/18/06 16:25:57 changed by dev@simon.net.nz

  • summary changed from "SET NAMES utf8" is sent before every query to [patch] "SET NAMES utf8" is sent before every query.

12/18/06 16:26:18 changed by dev@simon.net.nz

  • attachment 3151.diff added.

12/30/06 01:21:02 changed by adrian

  • status changed from new to closed.
  • resolution set to fixed.

(In [4267]) Fixed #3151 -- Improved MySQL backend not to send 'SET NAMES utf8' before every query. Thanks for the patch, smurf@smurf.noris.de


Add/Change #3151 ([patch] "SET NAMES utf8" is sent before every query)




Change Properties
Action