amalashkevich
>>> from pony.orm.examples.estore import *
>>> select(c for c in Customer if 'iPad' in c.orders.items.product.name)[:]
SELECT "c"."id", "c"."email", "c"."password", "c"."name", "c"."country", "c"."address"
FROM "Customer" "c"
WHERE 'iPad' IN (
SELECT "product-1"."name"
FROM "Order" "order-1", "OrderItem" "orderitem-1", "Product" "product-1"
WHERE "c"."id" = "order-1"."customer"
AND "order-1"."id" = "orderitem-1"."order"
AND "orderitem-1"."product" = "product-1"."id"
)
But you can tell Pony to use JOIN instead of a subquery by wrapping the 'in' section into a 'JOIN' hint: >>> select(c for c in Customer if JOIN('iPad' in c.orders.items.product.name))[:]
SELECT DISTINCT "c"."id", "c"."email", "c"."password", "c"."name", "c"."country", "c"."address"
FROM "Customer" "c", "Order" "order-1", "OrderItem" "orderitem-1", "Product" "product-1"
WHERE "product-1"."name" = 'iPad'
AND "c"."id" = "order-1"."customer"
AND "order-1"."id" = "orderitem-1"."order"
AND "orderitem-1"."product" = "product-1"."id" mynewquery = myquery.filter(lambda c: c.name > 'G')
The new query will produce the following SQL: SELECT `c`.`id`
FROM `customer` `c`
LEFT JOIN `order` `order-1`
ON `c`.`id` = `order-1`.`customer`
WHERE `c`.`name` > 'G'
GROUP BY `c`.`id`
HAVING coalesce(SUM(`order-1`.`total_price`), 0) > 1000Pony had the ability to produce JOINs from the very beginning, but during that presentation we found that Pony produced subqueries for MySQL and that was not very performant, correct. Since than we've improved Pony and now it got a query optimizer which replaces subqueries with efficient JOINs where it is possible. Here is the query from that presentation:
>>> select(c for c in Customer if sum(c.orders.total_price) > 1000)[:]
The straightforward way is to use a subquery here, but Pony's optimizer produces LEFT JOIN because such query usually has better performance: SELECT `c`.`id`
FROM `customer` `c`
LEFT JOIN `order` `order-1`
ON `c`.`id` = `order-1`.`customer`
GROUP BY `c`.`id`
HAVING coalesce(SUM(`order-1`.`total_price`), 0) > 1000
In our opinion this is the main advantage of Pony ORM - the possibility to perform semantic transformations of a query in order to produce performat SQL while keeping the text of Python query as high level as possible.The idea of Pony ORM is to provide a Pythonic way to work with the database. We think that the generator syntax is very concise and convenient.
It is named Pony because a pony is a small, smart and powerful creature - these are the features which our mapper has. Our goal is to provide non-leaky abstraction and good user experience.
> are people who find the repo on github directly at risk of violating the license?
Nope, why? Using it under AGPL is a valid option.