Answer the question
In order to leave comments, you need to log in
Which option is faster composite primary or AUTO_INCREMENT + composite UNIQUE INDEX?
Data (number of records):
product ~ 100K
product_rel ~ 3M
Queries:
INSERT, UPDATE - none
Usually executed, queries like:
SELECT p.*,pr.rating
FROM product_rel pr
JOIN product p ON(pr.product_id_2 = p.id)
WHERE pr.product_id_1 = 12345
ORDER BY pr.rating
Which option is better (faster) for queries (SELECT), and which type of database (MYISAM,INNODB) to choose
CREATE TABLE product_rel(
product_id_1 MEDIUMINT(9) UNSIGNED NOT NULL DEFAULT 0,
product_id_2 MEDIUMINT (9) UNSIGNED NOT NULL DEFAULT 0,
rating FLOAT(9, 9) UNSIGNED NOT NULL DEFAULT 0.000000000,
PRIMARY KEY (product_id_1, product_id_2),
INDEX IX_product_rel_product_id_2 (product_id_2)
)
or
CREATE TABLE product_rel(
id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
product_id_1 MEDIUMINT(9) UNSIGNED NOT NULL DEFAULT 0,
product_id_2 MEDIUMINT(9) UNSIGNED NOT NULL DEFAULT 0,
rating FLOAT(9, 9) UNSIGNED NOT NULL DEFAULT 0.000000000,
PRIMARY KEY (id),
UNIQUE INDEX UK_product_rel (product_id_1, product_id_2),
INDEX IX_product_rel_product_id_2 (product_id_2)
)
Answer the question
In order to leave comments, you need to log in
The second option will hypothetically be slightly faster, but it is worth choosing for reasons of business logic - where will the table keys be used outside the database? Do what is more convenient, without thinking about speed. Or test the speed if you think that this will be a bottleneck in the system.
(Apparently, this is a table in the sense of something like "they also buy with this product ...", I would use the first option for this, without bothering with a separate ID.)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question