0

I'm attempting to run a SQL query to select two columns from a table. The issue comes when I use the cursor.fetchall() method, which takes 0.2 seconds to return the columns when in other similar cases it takes <0.01 seconds. The returned list is only 1-5 tuples long, so isn't an extreme amount of data.

cursor.execute(f"SELECT Subject, Code FROM blockPeriodsTable WHERE dataIndex={num} AND Block='A'")
blockPeriodsTemp = cursor.fetchall()

Is there a way to speed this up or is it not possible?

5
  • 2
    You may find some of the answers in this question helpful: stackoverflow.com/a/17861221/4018331 Commented Sep 12, 2024 at 15:14
  • 1
    Are you sure it is fetchall(), and not execute(), which takes the extra time? Commented Sep 12, 2024 at 16:29
  • Aside: Don't use string formatting to substitute variables. See stackoverflow.com/questions/902408/… Commented Sep 12, 2024 at 17:13
  • Different queries will take different amounts of time to execute. We'd need to see these similar cases which are faster to advise. We'd also need to see your table definition, including indexes, and a rough idea of how many rows there are. Finally, the output of explain SELECT Subject, Code FROM blockPeriodsTable WHERE dataIndex={num} AND Block='A' will give a lot of information about the performance of the query. Commented Sep 12, 2024 at 17:22
  • typically, the first time tables are accessed queries can take longer as the backend needs to do a bunch of stuff to prepare for retrieval , once done, subsequent access is usually much quicker (but thats' also dependant upon other activities happening within the db (and elapsed time, table activities)) Commented Sep 12, 2024 at 22:56

0

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.