First of all (this is my own opinion), I am against using raw SQL when we are already working with a high-level structure like Django, if we do not find what we are looking for in the framework of (Django), so I would rather use Django for this :
Check this approach, it only works if you have set automatic PK increment for you and, of course, if the data is consistent (you do not delete the record from the table so you can be sure that all identifiers automatically increase)
import random
if the condition indicated earlier is not fulfilled, I think you can do it with an raw SQL query, for example:
query = """SELECT * FROM table ORDER BY RAND() LIMIT 10""") table.objects.raw(query)
about performance, I think you need time, I hope this helps.
source share