473,398 Members | 2,212 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

MySQLdb

I have just started playing around with MySQLdb for a project I am planning.

As a test I have written a script that executes 3000 insert statements
on a table. The table contains 10 fields with a mix of text and numbers
- its a product table for a website eg UPC, ProductName, Price etc.

The problem I have is that it takes just over two minuted to execute the
3000 insert statements which seems really slow! I am running it on a
machine with a 1.5 Ghz Pentium M Processor and Gig Of Ram. I dont think
the machine is to blame for the speed because during execution the
processor sits at about 10% and there is loads of free RAM.

Does anyone know if this sort of speed sounds right?

Cheers,

Dan.
Jul 18 '05 #1
6 2410
> The problem I have is that it takes just over two minuted to execute the
3000 insert statements which seems really slow!


Are you creating a new DB connection for every insert?

I just did a test on my system (Athlon 2500+), 3000 rows with an
auto_increment field and a randomly generated 128 character field. 1.9
seconds.

Jul 18 '05 #2
On Tue, 25 Jan 2005 20:43:54 +0000, Daniel Bowett
<da****@bowettsolutions.com> declaimed the following in
comp.lang.python:
As a test I have written a script that executes 3000 insert statements
on a table. The table contains 10 fields with a mix of text and numbers
- its a product table for a website eg UPC, ProductName, Price etc.
How many indices?
The problem I have is that it takes just over two minuted to execute the
3000 insert statements which seems really slow! I am running it on a
I recall reading that, for some RDBMs, when doing such batch
inserts, they recommend turning off the indices at the start, do the
inserts, then reactivate the indices -- apparently it is faster to
rebuild an index after the data has been inserted, then to continually
update the index.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #3
> How many indices?

Just the primary key (id).

Jul 18 '05 #4
Dennis Lee Bieber wrote:
On Tue, 25 Jan 2005 20:43:54 +0000, Daniel Bowett
<da****@bowettsolutions.com> declaimed the following in
comp.lang.python:

As a test I have written a script that executes 3000 insert statements
on a table. The table contains 10 fields with a mix of text and numbers
- its a product table for a website eg UPC, ProductName, Price etc.


How many indices?

The problem I have is that it takes just over two minuted to execute the
3000 insert statements which seems really slow! I am running it on a

I recall reading that, for some RDBMs, when doing such batch
inserts, they recommend turning off the indices at the start, do the
inserts, then reactivate the indices -- apparently it is faster to
rebuild an index after the data has been inserted, then to continually
update the index.


UPC is my only index - its a varchar with 20 characters. I am only
opening the connection once, then doing 3000 execute statements in a for
loop.

I do have two "TEXT" fields in the table which contain the long and
short description. The average length of the long description is about
167 characters, the longest is 1800 characters. Is this whats making it
slow?

Jul 18 '05 #5
On Wed, 26 Jan 2005 08:27:34 +0000, Daniel Bowett
<da****@bowettsolutions.com> declaimed the following in
comp.lang.python:
I do have two "TEXT" fields in the table which contain the long and
short description. The average length of the long description is about
167 characters, the longest is 1800 characters. Is this whats making it
slow?
Varying length fields /might/ have an effect -- I don't have the
experience to know. Sorry.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #6
Hi Daniel,

You should probably take a look at the executemany method of the cursor.
Your insert times might drop by a factor 20 . Here's an example.

Cheers,

Fedor

import time
import MySQLdb
db=MySQLdb.Connect(user="me",passwd="my password",db="test")
c=db.cursor()
n=0
tic=time.time()
for i in range(3000):
n+=c.execute('INSERT INTO testtable VALUES (%s)', (i,))
toc=time.time()
t1=toc-tic
print 'separate sql statements: %s, inserted %s records' % (t1,n)

tic=time.time()
n=c.executemany('INSERT INTO testtable VALUES (%s)', [(i,) for i in
range(3000)])
toc=time.time()
t2=toc-tic
print 'all at once %s inserted %s records' % (t2,n)

OUTPUT>>>
separate sql statements: 0.571248054504, inserted 3000 records
all at once 0.0253219604492 inserted 3000 records
Jul 18 '05 #7

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

Similar topics

0
by: Dave Harrison | last post by:
Hi all, got a problem combinging mx and MySQLdb, when I build and install both for my Python2.1 install on a Solaris 9 box I can import mx fine, but importing MySQLdb causing python to core dump. ...
5
by: Chris Stromberger | last post by:
When issuing updates in mysql (in the console window), mysql will tell you if any rows matched and how many rows were updated (see below). I know how to get number of rows udpated using MySQLdb,...
1
by: Peter Nikolaidis | last post by:
Greetings, I am attempting to get MySQLdb 0.9.2 installed on Mac OS 10.2 with a Fink distribution of Python 2.2.2. I have seen only a few posts on the subject, some of them relate to...
1
by: Derek Fountain | last post by:
I was trying to use MySQLdb to connect to a database. All is OK, except I can't figure out how to get the details of an error. Suppose I try to connect to a non existant server, or with the wrong...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
2
by: ws Wang | last post by:
MySQLdb is working fine at command line, however when I tried to use it with mod_python, it give me a "server not initialized" error. This is working fine: ----------------------- testmy.py...
1
by: Steve | last post by:
Darwin steve.local 8.3.0 Darwin Kernel Version 8.3.0: Mon Oct 3 20:04:04 PDT 2005; root:xnu-792.6.22.obj~2/RELEASE_PPC Power Macintosh powerpc MacOSX 10.4.3 mysql Ver 14.7 Distrib 4.1.14, for...
2
by: Mondal | last post by:
Hi, I am using MySQL 5.0 beta and Active Python 2.4. I have the correct version of MySQLdb installed. The problem is that I can't connect to MySQL through the Active Python interactive window. ...
1
by: Yi Xing | last post by:
Hi, I met the following error when I tried to install MySQLdb. I had no problem installing numarray, Numeric, Rpy, etc. Does anyone know what's the problem? Thanks! running install running...
0
by: Steve Holden | last post by:
Vaibhav.bhawsar wrote: imported The point here is that MySQLdb is a package, not a module. Some packages have their top-level __init__.py import the package's sub-modules or sub-packages to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.