Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#3151 closed enhancement (fixed)

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

Reported by: smurf@… Owned by: adrian
Component: Database layer (models, ORM) Version: master
Severity: minor Keywords: mysql
Cc: mir@…, dev@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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 (1)

3151.diff (814 bytes) - added by dev@… 8 years ago.

Download all attachments as: .zip

Change History (5)

comment:1 Changed 8 years ago by anonymous

  • Cc mir@… added

comment:2 Changed 8 years ago by dev@…

  • Cc dev@… added
  • Keywords mysql added
  • Version set to SVN

Passes all tests using the mysql backend at r4227.

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

OK

comment:3 Changed 8 years ago by dev@…

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

Changed 8 years ago by dev@…

comment:4 Changed 8 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

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

Note: See TracTickets for help on using tickets.
Back to Top