Skip to content

the task #4 from HW #4 was completed - #4

Open
Dmitriy-Klv wants to merge 1 commit into
samodurOFF:masterfrom
Dmitriy-Klv:feat/SQLAlchemy-task-4
Open

the task #4 from HW #4 was completed#4
Dmitriy-Klv wants to merge 1 commit into
samodurOFF:masterfrom
Dmitriy-Klv:feat/SQLAlchemy-task-4

Conversation

@Dmitriy-Klv

Copy link
Copy Markdown

No description provided.

@samodurOFF samodurOFF left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отличная работа. Можно мержить

Comment thread HWs/DmitriyK/HM4/main.py
Comment on lines +68 to +70
all_categories = session.query(Category).all()

for category in all_categories:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

У вас выходит проблема N+1. Потому что вы извлекаете связанные объекты множеством запросов в цикле. Лучше сделать 1 большой запрос

Suggested change
all_categories = session.query(Category).all()
for category in all_categories:
data = session.query(Category, Product).join(Product).all()
for category, product in data:
# дальше сами

Либо тоже через join, но уже по конкретным полям и таблица с Product будет слева

Suggested change
all_categories = session.query(Category).all()
for category in all_categories:
data = session.query(Category.name, Product.name, Product.price).join(Category).all()
for category, product, price in data:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants