Let's say I have a large database that consists of products in groups. Let's say that there are 5 groups, each of them has 100,000 products. the product ids are random integers (so are the group ids)
I need to find a product in a specific group. My question is which primary key is more efficient:
(sid, pid)
(pid, sid)
sid, pid is intuitive, but when searching in this order, MySQL will have to isolate 100,000 out of the 500,000 rows and then find a single number in 100,000. On the other hand, (pid, sid)
sounds more optimal to me since it will force mysql not to create the large 100,000 group in the first stage, but to go directly to the right item (or up to 5 items if there are similar pids in different cids).
Is #2 indeed faster?
UPDATE: OK. I copied a real table to two copies. table0 has primary key sid,pid. table1 has pid,sid.
result of query:
explain select * from items0 where sid = 22746 and pid = 2109418034 1, 'SIMPLE', 'items0', 'ref', 'PRIMARY', 'PRIMARY', '8', 'const,const', 14, ''
explain select * from items1 where sid = 22746 and pid = 2109418034
1, 'SIMPLE', 'items1', 'ref', 'PRIMARY', 'PRIMARY', '8', 'const,const', 11, ''
Yet another update: I also added the two keys to the same table and run explain. got this: (Primary starts with sid_pid1, Index2 starts with pid1,sid)
1, 'SIMPLE', 'items', 'ref', 'PRIMARY,index_2', 'index_2', '8', 'const,const', 13, ''
I'm not sure, what conclusions can I draw from this test?