Optimization in SQL

Asked

Viewed 62 times

0

Is there any way to improve that stretch?

2 answers

4


It was not very clear if by optimization you refer to writing or efficiency, but if written, I believe that the ideal would be to replace this with an IN, example:

SELECT products.name, categories.name FROM products
INNER JOIN categories ON categories.id = products.id_categories
WHERE products.amount > 100 AND categories.id IN (1, 2, 3, 6, 9);
  • 3

    If it’s performance optimization, just create an index in the column categories.

1

Hello live based on the previous answers perform the query using IN and create indexing if it does not exist, the complete solution to optimize your query.

Consultation

SELECT products.name, categories.name FROM products
INNER JOIN categories ON categories.id = products.id_categories
WHERE products.amount > 100 AND categories.id IN (1, 2, 3, 6, 9);

Indexing

CREATE INDEX IDX_products_id_categories
ON products (id_categories);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.