473,503 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DBAPI Paramstyle

The following script is a one person's comparison of three methods for
accessing a postgresql database using psycopg on a debian computer
running python2.3. Following it are the results of running it six
times.

===========================
from time import time, clock
import psycopg

MAX_COUNT = 50000

def pyMethod():
for n in range(MAX_COUNT):
curs.execute('''SELECT %s;''' % n)

def formatMethod():
for n in range(MAX_COUNT):
curs.execute('''SELECT %s;''', [n])

def pyformatMethod():
for n in range(MAX_COUNT):
curs.execute('''SELECT %(n)s;''', {'n':n})

conn = psycopg.connect(host='localhost', database='template1')
curs = conn.cursor()

for method, func in (('Python method: %f, %f', pyMethod),
('Format method: %f, %f', formatMethod),
('Pyformat method: %f, %f', pyformatMethod)):
startTime = time()
startClock = clock()
func()
print method % ((time() - startTime), (clock() - startClock))
===========================

bp@debian:~/demo$ ./pyformatTst.py
Python method: 9.288770, 3.55000
Format method: 9.457663, 3.820000
Pyformat method: 9.446390, 3.700000

bp@debian:~/demo$ ./pyformatTst.py
Python method: 9.152173, 3.400000
Format method: 9.314743, 3.760000
Pyformat method: 9.329343, 3.840000

bp@debian:~/demo$ ./pyformatTst.py
Python method: 9.262013, 3.490000
Format method: 9.344197, 3.570000
Pyformat method: 9.402157, 3.500000

bp@debian:~/demo$ ./pyformatTst.py
Python method: 9.170817, 3.860000
Format method: 9.509313, 3.260000
Pyformat method: 9.380756, 3.770000

bp@debian:~/demo$ ./pyformatTst.py
Python method: 9.271831, 3.540000
Format method: 9.375170, 3.650000
Pyformat method: 9.426898, 3.780000

bp@debian:~/demo$ ./pyformatTst.py
Python method: 9.192097, 3.720000
Format method: 9.244554, 3.690000
Pyformat method: 9.368582, 3.760000

Similar results occurred with an actual database table.

I must be missing something, so perhaps someone can explain
the benefit of a paramstyle over the usual Python formatting
style and maybe suggest a test to show it. Thanks.

Bob Parnes

--
Bob Parnes
rp*****@megalink.net
Jul 18 '05 #1
6 2600
Bob Parnes wrote:
I must be missing something, so perhaps someone can explain
the benefit of a paramstyle over the usual Python formatting
style and maybe suggest a test to show it. Thanks.


set the parameter to "0; DROP DATABASE template1;" and see what
happens.

or set it to os.urandom(1000) and run your test a couple of times to see
what happens.

</F>

Jul 18 '05 #2
On Thu, 24 Mar 2005 15:03:13 +0100, Fredrik Lundh <fr*****@pythonware.com> \
wrote:
Bob Parnes wrote:
I must be missing something, so perhaps someone can explain
the benefit of a paramstyle over the usual Python formatting
style and maybe suggest a test to show it. Thanks.


set the parameter to "0; DROP DATABASE template1;" and see what
happens.

or set it to os.urandom(1000) and run your test a couple of times to see
what happens.


Thanks for the suggestion. My system does not appear to contain an
os.urandom() method. It has a /dev/urandom device, but I don't know how to
use it for this purpose, except perhaps to select the first byte that it
produces.

I have a mediocre talent at programming, which is why I chose python.
For me it was a good choice. I note this so that I hope you understand why
I say that I don't know what you are driving at. My understanding is that a
paramstyle is more efficient than the traditional python approach for repeated
use of a query. If so, then I do not see how the choice of a parameter is
relevant. If it is more efficient only in a specific abstract case, then
one would have to look for other reasons to use it in a practical application.

Bob Parnes

--
Bob Parnes
rp*****@megalink.net
Jul 18 '05 #3
Bob Parnes <rp*****@megalink.net> wrote:

I have a mediocre talent at programming, which is why I chose python.
For me it was a good choice. I note this so that I hope you understand why
I say that I don't know what you are driving at. My understanding is that a
paramstyle is more efficient than the traditional python approach for repeated
use of a query. If so, then I do not see how the choice of a parameter is
relevant. If it is more efficient only in a specific abstract case, then
one would have to look for other reasons to use it in a practical application.


In theory, using a paramstyle allows the query to be sent to the SQL
database backend and compiled like a program. Then, successive uses of the
same query can be done by sending just the parameters, instead of sending
the entire query string to be parsed and compiled again and again. This is
commonly done with large production databases like SQL Server and Oracle.
For a complicated query, it can be a significant time savings.

However, to the best of my knowledge, none of the Python dbabi
implementations actually do that. So, the primary benefit of the
paramstyle method is that the database provider inserts whatever quoting is
required; you don't have to remember to put single quotes around the
arguments, and protect single quotes within the arguments by doubling them,
and so on.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #4
Tim Roberts wrote:
In theory, using a paramstyle allows the query to be sent to the SQL
database backend and compiled like a program. Then, successive uses of the same query can be done by sending just the parameters, instead of sending the entire query string to be parsed and compiled again and again. This is commonly done with large production databases like SQL Server and Oracle. For a complicated query, it can be a significant time savings.

However, to the best of my knowledge, none of the Python dbabi
implementations actually do that.


kinterbasdb does.

Jul 18 '05 #5
Tim Roberts wrote:
In theory, using a paramstyle allows the query to be sent to the SQL
database backend and compiled like a program. Then, successive uses of the
same query can be done by sending just the parameters, instead of sending
the entire query string to be parsed and compiled again and again.
This is commonly done with large production databases like SQL Server
and Oracle. For a complicated query, it can be a significant time
savings.
Actually, the slow part is building a query execution plan (deciding
which indices to use, join orders, and such). Identifying what is
being asked for is trivially fast (compared to any I/O). The DB
optimizer works with estimates of running time for many of the possible
query plans, and chooses the cheapest of those -- that combinatorial
problem is how a DB can chew up CPU time (or save it in some cases).
However, to the best of my knowledge, none of the Python dbabi
implementations actually do that. This cacheing need not occur anywhere before the DB. There are database
systems (DB2 is one) that cache plans by the text of the query. If your
query is an exact match with a previously optimized plan (and it hasn't
been chucked out of the cache), the plan is retrieved.
So, the primary benefit of the paramstyle method is that the database
provider inserts whatever quoting is required; you don't have to
remember to put single quotes around the arguments, and protect
single quotes within the arguments by doubling them, and so on.


This _is_ a benefit.

Another benefit (greater in my estimation) ls that you are separating
"code" and data to make a more readable (and malleable) query. If
things get slow and database access is "at fault", you can carry
your SQL to a DB expert (sometimes one on site) who can check it to
help speed up your system. The DB expert is an SQL dweeb, and will
appreciate seeing the SQL done in a straightforward manner. She or he
may be able to rewrite the query to improve your access. Perhaps
several related queries can be effectively combined (but again, you
are talking about an expert in SQL/DB -- they'll need to see all
the queries easily in order to help. Perhaps the data access
pattern will suggest a change in DB indices (in which case the
database administrator can speed up your program without your
changing any of your code).

You could also be changing the format used to send the data to the
database (of that I'm not quite as sure).

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #6
Tim Roberts wrote:
In theory, using a paramstyle allows the query to be sent to the SQL
database backend and compiled like a program. Then, successive uses of the same query can be done by sending just the parameters, instead of sending the entire query string to be parsed and compiled again and again. This is commonly done with large production databases like SQL Server and Oracle. For a complicated query, it can be a significant time savings.

However, to the best of my knowledge, none of the Python dbabi
implementations actually do that.


mx.ODBC does, since it is an ODBC implementation. I would be very
surprised if the Oracle adapter did not. MySQLdb does not yet, but
probably will by the end of summer (with MySQL-4.1 or newer).

Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3584
by: Rene Pijlman | last post by:
I can't seem to find any way to specify the character encoding with the DB API implementation of PyPgSQL. There is no mention of encoding and Unicode in the DB API v2.0 spec and the PyPgSQL README....
0
1065
by: Lee Harr | last post by:
Is there a special DB API style for inserting array values in to a database? In postgres, the values needs to end up like ARRAY so I am just putting the word ARRAY in to the query string like...
11
1483
by: Gerhard Haering | last post by:
Last December, we had a short thread discussing (in python-dev) the integration of PySQLite into Python 2.4. At the time, I was against inclusion, because I thought PySQLite was not ripe for it,...
1
1969
by: gglegrp112 | last post by:
Is it possible to send an array as a parameter for an execute method in dbapi2 module? I'm using adodbapi and try to perfrom the following SQL query: select * from item where storeid in ('01',...
6
2016
by: Raphael.Benedet | last post by:
Hello, For my application, I would like to execute an SQL query like this: self.dbCursor.execute("INSERT INTO track (name, nbr, idartist, idalbum, path) VALUES ('%s', %s, %s, %s, '%s')" %...
6
1626
by: BartlebyScrivener | last post by:
With aColumn = "Topics.Topic1"' The first statement "works" in the sense that it finds a number of matching rows. c.execute ("SELECT Author, Quote, ID, Topics.Topic1, Topic2 FROM QUOTES7...
0
1004
by: Greg Copeland | last post by:
According to the SQLAlchemy list, the DBAPI specification does not define a standard error reporting mechanism which would allow for generic detection of loss of database connection without DB...
1
1277
by: =?ISO-8859-1?Q?Hans_M=FCller?= | last post by:
Hi, I'd like to modify some tables in a database in one transaction. This approach doesn't work: import MySQLdb con = MySQLdb.connect("servernam", user = "username", passwd = "verysecret,...
3
4364
by: Daniel | last post by:
Hello, I'm developing an application that accesses both a MySQL and an SQLite database. I would like to have named parameters in my SQL and have found the following: For MySQL my named...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3162
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1507
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.