问题
I have the following python source file to retrieve rows from MySQL database. The problem is there are too many rows to retrieve in subscriber table. How can I do limit to small chunk of rows to retrieve and process and so on?
class subinfo(peewee.Model):
sub_id = peewee.IntegerField()
active = peewee.BooleanField()
sub_type = peewee.IntegerField()
sub_cat = peewee.TextField()
class Meta:
database = locdb
class subscriber(peewee.Model):
sub_id = peewee.IntegerField(unique=True, primary_key=True)
sub_start = peewee.DateTimeField()
sub_end = peewee.DateTimeField()
ref_id = peewee.BigIntegerField()
class Meta:
database = locdb
for row in subscriber.select():
if(row.sub_start >= peewee.datetime.datetime.now()):
sub1, created = subinfo.get_or_create(sub_id=row.sub_id, active=True)
if(created == False & sub1.active == False):
q = subinfo.update(active=True).where(sub_id=row.sub_id)
q.execute()
else:
subinfo.get_or_create(sub_id=row.sub_id, active=False)
回答1:
for row in subscriber.select()
Do this for first 100 rows:
for row in subscriber.select().limit(100)
Next 100 rows:
for row in subscriber.select().limit(100).offset(100)
Etc.
来源:https://stackoverflow.com/questions/45503451/limit-number-of-rows-peewee-retrieve