So this is a tough MySQL question for MySQL GODs here
Say we have a Table called client_data, which contains base client data such as: id, address, email etc.
We have a Table called client_contacts, which contains emails we have sent to clients based on XYZ reasons with data such as: client_id, email, reason_id, etc.
How can we generate a SELECT which is based on clients from Table client_data & Table client_contacts which contains clients that have NOT been emailed to for a given reason_id
We tried this, but 1st it does not work and 2nd it Kills MySQL:
SELECT cd.email, cd.id, cd.category FROM client_data AS cd
LEFT JOIN client_contacts AS cc ON (cd.email != cc.email)
WHERE cc.reason_id = 991 AND cd.category IN (49, 149) ORDER BY RAND() LIMIT 10000;
SELECT cd.email
, cd.id
, cd.category
FROM client_data AS cd
LEFT OUTER
JOIN client_contacts AS cc
ON cc.email = cd.email
AND cc.reason_id = 991
WHERE cd.category IN (49, 149)
AND cc.email IS NULL
r9, this does work as far as getting the results goes.
But it sends MySQL usage from typical of 3.5% of CPU to 105% of CPU and it took about 2 Minutes for answers to come back and that was with LIMIT only at 10. And we need typical such emails to be at 5000 or so LIMIT
So obviously this SELECT as is is not practical, any ideas to get it to perform like typical SELECT which returns result in under 0.02 sec and barely effects CPU?
Of course we know about Indexed, but questions are:
1- Which fields to Index
2- And should we shut down a DB while it is being Indexed
3- Will it really make that much of a difference since our typical select returns results in 0.005 second for 10000+ records and this select took 2 minutes for 10 records
r9, sorry for long delay in replying. This is what you get for doing 100 peoples job.
Anyway, to your points:
Can you point me to what you consider to be best writeup on Mysql Indexing as it pertains to above.
And how would you issue such as command and what options should we worry about?
r9, just wanted to say Thanks on this Index heads up.
It has been a real Eye opener to delve deep into Indexs and boy we made some SELECTs much faster. Like 100 times faster! But one Key thing I am having difficult grasping with is:
Which Columns to Index? For best results. I mean what is best logic for deciding this?