patternsqlMinor
Has the following query been made in the optimum way?
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 DESCIf 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=InnoDBSolution
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=InnoDByour 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=InnoDBContext
StackExchange Code Review Q#12585, answer score: 4
Revisions (0)
No revisions yet.