|
|
phones = Telephone.objects.filter(number__startswith="310")[:50]
for phone in phones:
print(phone.user)
DEMO TIME
Telephone.objects.filter(...).select_related('user')
SELECT "main_telephone"."id",
"main_telephone"."number",
"main_telephone"."user_id",
"main_user"."id",
"main_user"."name"
FROM "main_telephone"
INNER JOIN "main_user" ON ("main_telephone"."user_id" = "main_user"."id")
WHERE "main_telephone"."number" LIKE '310%'
list(User.objects.all().prefetch_related('telephone_set')[:50])
SELECT "main_user"."id",
"main_user"."name"
FROM "main_user" LIMIT 50
SELECT "main_telephone"."id",
"main_telephone"."number",
"main_telephone"."user_id"
FROM "main_telephone"
WHERE "main_telephone"."user_id" IN (257402,
257403,
...
257450,
257451)
SELECT *
FROM product
ORDER BY name DESC
OFFSET 12500 LIMIT 100
…the rows are first sorted according to the <order by clause> and then limited by dropping the number of rows specified in the <result offset clause> from the beginning…
SELECT *
FROM product
ORDER BY name DESC
LIMIT 100
SELECT *
FROM product
ORDER BY name DESC
OFFSET 100 LIMIT 100
SELECT *
FROM product
ORDER BY name DESC
OFFSET 200 LIMIT 100
SELECT *
FROM product
ORDER BY name DESC
OFFSET 300 LIMIT 100
SELECT *
FROM product
ORDER BY name DESC
LIMIT 100
SELECT *
FROM product
WHERE name > ${last_name_previous_page}
ORDER BY name DESC
LIMIT 100
SELECT *
FROM product
WHERE name > ${last_name_previous_page}
ORDER BY name DESC
LIMIT 100
SELECT *
FROM product
WHERE name > ${last_name_previous_page}
ORDER BY name DESC
LIMIT 100
En Django: infinite scroll pagination
Escoger cuáles columnas traer
QuerySet.defer('html_content')
QuerySet.only('id', 'name')
QuerySet.values('id', 'name')
QuerySet.values_list('id', 'name')
Estructura de datos auxiliar que permite ubicar/seleccionar registros de una tabla, sin tener que examinar toda la tabla.
DEMO TIME
Tomado de engineering-blog.alphasights.com
DEMO TIME
LIKE 'Texto%'
)Un índice contiene una copia de los datos indexados, y apuntadores a los registros correspondientes en la tabla.
Todos los índices de una tabla deben ser actualizados al crear, actualizar* , o borrar registros.
|
|
Impide modificaciones a los registros seleccionados, hasta que termine la transacción actual.
from django.db import transaction
|
|
Estrategia: actualizar la columna del registro sólo si ésta no ha cambiado
SELECT amount
FROM main_account
WHERE id = 1
-- (id: 1, amount: 200)
UPDATE main_account
SET amount = 500
WHERE id = 1
AND amount = 200
Estrategia general: actualizar el registro sólo si su versión no ha cambiado:
SELECT id
, version
, amount
FROM main_account
WHERE id = 1
-- (id: 1, amount: 200, version: 3)
UPDATE main_account
SET amount = 500, version = version + 1
WHERE id = 1
AND version = 3 -- versión anterior
https://github.com/saxix/django-concurrency
|
|
UPDATE main_account
SET amount = amount + 300
WHERE id = 1
https://docs.djangoproject.com/en/1.9/ref/models/expressions/#f-expressions
from django.db.models import F
Account.objects.filter(id=1).update(amount=F('amount')+300)
UPDATE "main_account"
SET "amount" = ("main_account"."amount" + 300)
WHERE "main_account"."id" = 1