The django query gets the last n records on the database server

advertisements

Hi accroding to django query get last n records

Is it good idea to get this last n record using [:n]? Can I do it on database serwer (for example in sql "select top 10"), not on server where is django?


I think for small records in the database say for 1k records, its fine using django query get last n records but for huge records in the table say 1M rows, its not the best way.

Getting n records from database server itself will be efficient than getting last n records as done here by django, because in this case total records are ordered then fetched and among those we are slicing last n records, if you have a choice to execute the query directly on a database server, you should go for it rather that using Django ORM as Django ORM is time and memory consuming.

order_by('?') queries may be expensive and slow, depending on the database backend you’re using. Details here