Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why django and python MySQLdb have one cursor per database?

Example scenario:

MySQL running a single server -> HOSTNAME

Two MySQL databases on that server -> USERS , GAMES .

Task -> Fetch 10 newest games from GAMES.my_games_table , and fetch users playing those games from USERS.my_users_table ( assume no joins )

In Django as well as Python MySQLdb , why is having one cursor for each database more preferable ?

What is the disadvantage of an extended cursor which is single per MySQL server and can switch databases ( eg by querying "use USERS;" ), and then work on corresponding database

MySQL connections are cheap, but isn't single connection better than many , if there is a linear flow and no complex tranasactions which might need two cursors ?

like image 234
DhruvPathak Avatar asked Feb 20 '13 17:02

DhruvPathak


1 Answers

A shorter answer would be, "MySQL doesn't support that type of cursor", so neither does Python-MySQL, so the reason one connection command is preferred is because that's the way MySQL works. Which is sort of a tautology.

However, the longer answer is:

  1. A 'cursor', by your definition, would be some type of object accessing tables and indexes within an RDMS, capable of maintaining its state.
  2. A 'connection', by your definition, would accept commands, and either allocate or reuse a cursor to perform the action of the command, returning its results to the connection.
  3. By your definition, a 'connection' would/could manage multiple cursors.
  4. You believe this would be the preferred/performant way to access a database as 'connections' are expensive, and 'cursors' are cheap.

However:

  1. A cursor in MySQL (and other RDMS) is not a the user-accessible mechanism for performing operations. MySQL (and other's) perform operations in as "set", or rather, they compile your SQL command into an internal list of commands, and do numerous, complex bits depending on the nature of your SQL command and your table structure.
  2. A cursor is a specific mechanism, utilized within stored procedures (and there only), giving the developer a way to work with data in a procedural way.
  3. A 'connection' in MySQL is what you think of as a 'cursor', sort of. MySQL does not expose it's internals for you as an iterator, or pointer, that is merely moving over tables. It exposes it's internals as a 'connection' which accepts SQL and other commands, translates those commands into an internal action, performs that action, and returns it's result to you.
  4. This is the difference between a 'set' and a 'procedural' execution style (which is really about the granularity of control you, the user, is given access to, or at least, the granularity inherent in how the RDMS abstracts away its internals when it exposes them via an API).
like image 173
Jack Shedd Avatar answered Oct 14 '22 07:10

Jack Shedd