2
In the search for how to make paging using psycopg2 I discovered the attributes cursor itersize. and cursor., and the methodscursor.fetchmany() and scroll cursor.()
I wanted to check with the staff here if my understanding is correct about these methods and attributes.
fetchmany()
returns many lines, whose quantity can be passed as parameter.
scroll()
from what I understand serves to scroll the cursor to some position among the lines loaded by the query, but I did not understand the parameter it receives.
Already the attribute itersize
as I understand it, it controls the number of lines the cursor should receive from the bank. Perhaps behind the driver is making use of the parameter LIMIT
postgresql which limits the number of lines that must be returned by the query.
The attribute arraysize
sets the number of lines that will be returned by the method fetchmany()
. I did not understand the purpose of this attribute in the object being that the method fetchmany()
already receives a parameter to define this.
I need to display the rows of a table in a Gui table component, from some graphical api I haven’t decided on yet. But I think it is more appropriate that I make use of these attributes and methods to create a pagination, as creating an object cursor
with all data from a table that has many records can consume many resources.
So I have no preference for where to make the pagination. I thought at bank level not to generate a monstrous cursor full of records, taking into account that it will be a server client application the consumption on the network will also be great in the case of a very swollen table. You are right about the premature optimization, but I am sure that the records in this table will be many, since a new record will be inserted with each transaction. Then it will grow gradually and soon, it will have thousands of records. I wanted to leave it treated so that I do not have to move later.
– Matheus Saraiva
I could make this pagination through direct SQL, but if psycopg2 has Features to do this I prefer it. That’s why I wanted to know more about these methods and attributes that I mentioned, because it seems to me that they serve precisely this. If possible with examples.
– Matheus Saraiva
Special the atibuto
itersize
and the methodscroll()
because they seem to be exactly what I’m looking for.– Matheus Saraiva
Opa joão, rereading your comment and reading again the doc I think maybe I understood what you meant. From what I understand, by default psycopg2 behind 2000 bank records, number this set in attribute
Cursor.itersize
. When you say "search for results in a "Lazy" way - that is, search for results only as they are needed." You mean if I need more records the driver himself will take care of searching?– Matheus Saraiva
Exactly - the driver himself picks more results as you consume them (I just checked the source code of Psicopg2 and he does it himself)
– jsbueno