I have a table hall_products with products. Each product has a NAME, is sold in specified QUANTITY and only to one city pointed by CITY_CODE. Now next table is delivery_address. Every delivery address is a city, it contains a CITY_CODE and MAX_QUANTITY that is a maximum quantity of a product that can be sent to this city (no matter what product).
So for example I have a product Milk with quantity 1 liter and addressed to the city with code 14 that means Berlin. And maximum quantity of good i can send to Berlin (city code 14) is 0,7 liter. Than i get ship the Milk to Berlin because quantity is higher than max_quantity.
What i want to get is names of all goods that can be shipped anywhere to the world. So i need to get all goods that fit into max_quantity limit of the target city.
And i wrote this query for this:
SELECT p.NAME FROM hall_products as p where p.QUANTITY >
(SELECT MAX_QUANTITY from delivery_address WHERE CITY_CODE = p.CITY_CODE )
But this query is sooo slow.
How to make it faster without database schema change aka only with change of query?
fromin the nested query and the*make it seem like it wouldn't work that well...>operator against it (actually I think it is never possible in MySQL to return more than one column in a subselect in MySQL)