Using EXPLAIN for the query below shows
SELECT cha.cid AS cid,
cha.data AS dl
FROM cha, c_users
WHERE uid = 808
AND cha.cid = c_users.cid;
- it does a full scan on
chatable - uses the multi column index(
cid,uid) fromc_users.
Why doesn’t it not use the primary key index from cha and rather does a full table scan. Is there a better way to optimise the query/table.
Edit:
cha.cid is my primary key. Added after the comment below.
In a normal BTREE, and an index on (cid,uid), a scan will be needed if you don’t specify cid and only search for uid. An index on (uid,cid) (notice the order) would / could help.
This is by no means a guarantee it will be used, MySQL can guess that a full scan might be quicker (continuous read) if a certain index / join most likely will require a certain large percentage of the index to be used, and other reasons. You can check with
FORCE INDEXif using the key compared to a scan is actually any quicker or not (disable query cache on a testserver before testing this).