2

I have one query which I ran on mysql, mssql or oracle like

select * from user

that query give me 0.5 million records/rows. I want to get only 10 rows. Below is python code :

 cursor.execute(query)
 columns = cursor.description
 rows = cursor.fetchall()

I tried using cursor.fetchmany(10) and that condition is giving me 10 row but it takes a lot of time. I want to set a limit before executing the query so that it will be not take much time and also got fast execution. Please give me any solution in python. Please help me...

6
  • can't you set condition on query itself, using rownum? Commented Nov 30, 2018 at 9:59
  • that query like a example any one random to pass query. but i need to set limit on my side. because i don't known how much data in this query. but i need only 10 rows. Commented Nov 30, 2018 at 10:01
  • what I am saying you can add/bind rownum to the query you are executing, otherwise you have to wait for query to return all the records then only you can filter it. Commented Nov 30, 2018 at 10:09
  • if query have nested or multiple joins with multiple table. If cursor object have that property then please tell me Commented Nov 30, 2018 at 10:12
  • can you please try setting this to 10 and then iterate over cursor intead of using fetchall, also break in first loop. initd.org/psycopg/docs/cursor.html#cursor.itersize Commented Nov 30, 2018 at 10:22

1 Answer 1

1

When you only need 10 rows, use select * from user LIMIT 10.

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

2 Comments

I need to reduce execution time. Like java have that type of property using prepared statement. You can set statement.setFetchSize(500);
You can user SSCursor, but configuring fetch size doesn't reduce execution time on MySQL server side. Adding LIMIT clause is much better than limit fetch size on client side.

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.