I know there a similar threads around, but this is really the first time I realize that query speed might affect me – so it´s not that easy for me to really make the transfer from other folks problems.
That being said I have using the following query successfully with smaller data, but if I use it on what are mildly large tables (about 120,000 records). I am waiting for hours.
INSERT INTO anothertable
(id,someint1,someint1,somevarchar1,somevarchar1)
SELECT DISTINCT md.id,md.someint1,md.someint1,md.somevarchar1,pd.somevarchar1
FROM table1 AS md
JOIN table2 AS pd
ON (md.id = pd.id);
Tables 1 and 2 contain about 120,000 records. The query has been running for almost 2 hours right now. Is this normal? Do I just have to wait. I really have no idea, but I am pretty sure that one could do it better since it´s my very first try.
I read about indexing, but dont know yet what to index in my case?
Thanks for any suggestions – feel free to point my to the very beginners guides !
Assuming
idis an auto-incremental PK, theDISTINCTis useless, since each row would be unique. In that case, removing it should also boost the performance, as SELECT DISTINCT can be quite slow.And as previously mentioned, make sure the
idfield has index on both tables (which it does if it’s PK).