HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Has the following query been made in the optimum way?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
themadeoptimumquerywaybeenhasfollowing

Problem

SELECT p.name,m.name
FROM parts p
INNER JOIN Manufacturers m ON p.man_id=m.id
ORDER BY p.name DESC


If not, what should be done for its optimization?

Here is the two tables:

CREATE TABLE `Manufacturers` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(30) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB

CREATE TABLE `parts` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(30) NOT NULL,
    `man_id` int(11) NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB

Solution

CREATE TABLE Manufacturers ( id int(11) NOT NULL AUTO_INCREMENT, 
name varchar(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB 

CREATE TABLE parts ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(30) NOT NULL, 
man_id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB


your structure looks OK for performance, because you have index the common field where you make the join on. How ever, shouldn't you use PK and FK instead?. If you need to know a bigger description use the explain command to be sure that the index are correctly used.

Code Snippets

CREATE TABLE Manufacturers ( id int(11) NOT NULL AUTO_INCREMENT, 
name varchar(30) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB 

CREATE TABLE parts ( id int(11) NOT NULL AUTO_INCREMENT, name varchar(30) NOT NULL, 
man_id int(11) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB

Context

StackExchange Code Review Q#12585, answer score: 4

Revisions (0)

No revisions yet.