2

Im trying to process data from a database table. Loading them all simultaneously will hog most of computer resource. Heres my current code

cursor.execute("SELECT * from sometable")
rs = cursor.fetchall()
.
# process 1: set operation
.
for a in rs:
    # process 2: put data in another db

Is there a way to reduce resource usage? Like getting rows bit by bit under a loop?

2 Answers 2

1

You can use cursor.fetchone(). Read about it here. Example usage:

cursor.execute(query)
numrows = int(cursor.rowcount)
for x in range(0,numrows):
    row = cursor.fetchone()

You can also consider using LIMIT in the mysql query:

cursor.execute("SELECT * from sometable LIMIT 0, 100")

Finally, avoid using the star operator and only select the columns you need.

Sign up to request clarification or add additional context in comments.

1 Comment

I read about it before but never knew much of the detail. Here a link containing an explanation and an example how to use fetchone : bytes.com/topic/python/answers/…
1

Using cursor.fetchone() will probably still cause resource issues because of the SELECT * statement before it. I believe the best way to do this is to limit the query results with LIMIT and just loop through them all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.