-2 seconds on query baby

This commit is contained in:
2024-07-16 04:36:56 +03:00
parent 422b24f52d
commit bcb7cd66ee

View File

@@ -62,9 +62,6 @@ async def get_stocks_data(
.select_from( .select_from(
SupplierProduct SupplierProduct
) )
.join(
Product
)
.where( .where(
SupplierProduct.supplier_id.in_(supplier_ids) SupplierProduct.supplier_id.in_(supplier_ids)
) )
@@ -171,21 +168,15 @@ async def get_stocks_data(
in_block_subquery = ( in_block_subquery = (
select( select(
Product.id.label('product_id'), SupplierProduct.product_id.label('product_id'),
func.min(SupplierProduct.in_block).label('in_block_value') func.min(SupplierProduct.in_block).label('in_block_value')
) )
.select_from(
Product
)
.join(
SupplierProduct
)
.where( .where(
SupplierProduct.supplier_id.in_(supplier_ids), SupplierProduct.supplier_id.in_(supplier_ids),
(SupplierProduct.supplier_stock - SupplierProduct.sold_today) > 0 (SupplierProduct.supplier_stock - SupplierProduct.sold_today) > 0
) )
.group_by( .group_by(
Product.id SupplierProduct.product_id
) )
.subquery() .subquery()
) )
@@ -291,7 +282,11 @@ async def get_stocks_data(
slaves_stock_subquery.c.product_id == MarketplaceProduct.product_id slaves_stock_subquery.c.product_id == MarketplaceProduct.product_id
) )
) )
print(stmt.compile(compile_kwargs={
'literal_binds': True
}))
result = await session.execute(stmt) result = await session.execute(stmt)
marketplace_products = result.all() marketplace_products = result.all()
response: List[StockData] = [] response: List[StockData] = []
for (marketplace_product, for (marketplace_product,