title: Fastest way to load numeric data into python/pandas/numpy array from MySQL
body: <p>I want to read some numeric (double, i.e. float64) data from a MySQL table. The size of the data is ~200k rows.</p>
  
  <p>MATLAB reference:</p>
  
  <pre><code>tic;
  feature accel off;
  conn = database(...);
  c=fetch(exec(conn,'select x,y from TABLENAME'));
  cell2mat(c.data);
  toc
  </code></pre>
  
  <p>Elapsed time is ~1 second.</p>
  
  <p>Doing the same in python, using the several examples found in here (I have tried them all, i.e. using pandas read_frame, frame_query and the __processCursor function):
  <a href="https://stackoverflow.com/questions/12047193/how-to-convert-sql-query-result-to-pandas-data-structure">How to convert SQL Query result to PANDAS Data Structure?</a></p>
  
  <p>Reference python code:</p>
  
  <pre><code>import pyodbc
  import pandas.io.sql as psql
  import pandas
  connection_info = "DRIVER={MySQL ODBC 3.51 \
  Driver};SERVER=;DATABASE=;USER=;PASSWORD=;OPTION=3;"
  cnxn = pyodbc.connect(connection_info)
  cursor = cnxn.cursor()
  sql = "select x,y from TABLENAME"
  #cursor.execute(sql)
  #dataframe = __processCursor(cursor, dataframe=True)
  #df = psql.frame_query(sql, cnxn, coerce_float=False)
  df = psql.read_frame(sql, cnxn)
  cnxn.close()
  </code></pre>
  
  <p>Takes ~6 seconds. Profiler says all the time spent was in read_frame.
  I was wondering if anyone could give me some hints how could this be speedup to at least match the MATLAB code. And if that is possible at all in python.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The bottleneck seems to be inside the cursor.execute (in pymysql library) or cursor.fetchall() in pyodbc library. The slowest part is reading the returned MySQL data element by element (row by row, column by column) and converting it to the data type which it inferred previously by the same library.</p>
  
  <p>So far I have managed to speed this up to close to MATLAB by doing this really dirty solution:</p>
  
  <pre><code>import pymysql
  import numpy
  
  conn = pymysql.connect(host='', port=, user='', passwd='', db='')
  cursor = conn.cursor()
  cursor.execute("select x,y from TABLENAME")
  rez = cursor.fetchall()
  resarray = numpy.array(map(float,rez))
  finalres = resarray.reshape((resarray.size/2,2))
  </code></pre>
  
  <p><strong>The above cur.execute IS NOT THE pymysql EXECUTE!</strong> I have modified it, inside the file "connections.py". First, the function def _read_rowdata_packet, now has instead of:</p>
  
  <pre><code>rows.append(self._read_row_from_packet(packet))
  </code></pre>
  
  <p>substituted with</p>
  
  <pre><code>self._read_string_from_packet(rows,packet)
  </code></pre>
  
  <p>Here _read_string_from_packet is a simplified version of _read_row_from_packet with the code:</p>
  
  <pre><code>def _read_string_from_packet(self, rows, packet):
      for field in self.fields:
          data = packet.read_length_coded_string()
          rows.append(data)
  </code></pre>
  
  <p>This is an uber-dirty solution which gives a speedup down from 6 seconds to 2.5 seconds. I was wondering, if all of this could somehow be avoided by using a different library/passing some parameters? </p>
  
  <p>Hence the solution would be to bulk-read the entire MySQL reply to a list of strings and then bulk-type converting to numerical data types, instead of doing that element-by-element.  Does something like that already exist in python?</p>