With vs Without Primary Key in a particular case
1. I have 2 MyISAM tables A and B.
2. B has a column COL_ID INT UNSIGNED, which is the primary key.
3. A has only one column COL_BID INT UNSIGNED, which holds a subset of values from B.COL_ID. Values in A.COL_BID are unique.
The query:
SELECT ... FROM A INNER JOIN B WHERE A.COL_BID = B.COL_ID;
The question is, if a PRIMARY KEY (COL_BID) is added to table A, would it help the select?
I tried with explain. Without a primary key, it says full table scan for table A, which is obvious; with a primary key, it says use index for both A and B, which are primary keys. However, the row count is same: all the rows in A has to be accessed anyway.
I wonder the difference between table scan and using index here. Does using index here mean load A.MYI and use it without touching A.MYD? Since A has only one column, it seems it's not necessary to load A.MYD.
Suppose A.MYD is not loaded in this particular case, then which one is faster, use A.MYD or use A.MYI? A.MYD should contain unsorted data and A.MYI should contain sort information, values of A.COL_BID, and file positions of the data blocks in A.MYD. In this particular case, it seems using A.MYD is faster.
So I think in this case, without a primary key is better.
Please let me know what do you think. Thanks.
Edited 1 time(s). Last edit at 07/19/2006 11:02PM by Martin Feng.