开发者

Django accessing database

When using the following LIMIT clause in the Django models API --

People.objects.all()[5000000开发者_如何学运维:5000000+5]

Does the database have to go through all 5M records in order to return just these 5 records? Or is it non-linear in its access?


It will go at least through the index table for the primary key of your People table. This question has in my opinion nothing to do about how Django handles this. Django will handle it as your database will. Django's ORM will just transform it into a query similiar to this one:

SELECT * FROM peoples OFFSET 5000000 LIMIT 5;


In short it does do a scan until that point and get the number of records you specify. This actually depends on your database, different databases have different performance on this query. It is not a random access, you should expect the same records to be returned if you do this 5 times, assuming your data has not changed. If you do an order_by you'd have even more predictable results.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜