I am trying to optimize a function (MySQL), but the JOIN
is still not completely understood (I try to make a simple example) :
I have 2 tables :
Table ITEMS
ID | ID_ORDER | ID_BOX | NAME
001 | 2564 | 123 | SPOON_1
002 | 2564 | 123 | SPOON_2
006 | 2564 | 123 | SHOES_2
007 | 2564 | 123 | SHOES_1
003 | 2565 | 124 | SPOON_1
004 | 2565 | 124 | SPOON_2
008 | 2565 | 124 | SHOES_1
009 | 2565 | 124 | SHOES_2
005 | 2565 | 125 | SPOON_1
010 | 2565 | 125 | SHOES_1
The description of objects are linked to the ITEM table by ID_CONTRACT, and NAME.(Not possible to have 2 items with same name inside the same contract).
Table DESCRIPTION :
ID_ORDER | NAME_ITEM | LENGTH | WIDTH | ....
2564 | SPOON_1 | 200 | 20 ...
2564 | SPOON_2 | 300 | 20 ...
2564 | SHOES_1 | 500 | 20 ...
2564 | SHOES_2 | 600 | 20 ...
Now, I need to know all items I have in the contract, with their description, and I use this query :
SELECT *,description.* FROM items INNER JOIN description
ON (description.ID_CONTRACT=items.ID_CONTRACT AND description.NAME_ITEM=items.NAME)
WHERE ID_CONTRACT= 2564
First, I just read it is not correct query (I need to copy all description fields by hand in query?), because ID_CONTRACT
is in both tables, and sometimes it gives me mistake(sometimes not), and I read there that it is not possible to ignore duplicates.
Then I am wondering, as I make a select on ITEMS table, MySQL is looking for each line a correspondance in DESCRIPTION table?
Is there a way to optimize query (another kind of JOIN), so it will not search everytime in ITEMS table, when he meets 2 elements (or more) in ITEMS, with same ID_CONTRACT/NAME ?