I just changed from Access to mySQL. I have a few tables, called “Veranstaltung” and “Veranstaltung_User”. I want to get all events (=Veranstaltung) to which users have subsribed, exept the one, the user is viewing.
I did this with the following statment
SELECT VID, Bezeichnung, Datum
FROM tbl_Veranstaltung
WHERE year(Datum) = 2012
AND VID <> 613
AND VID IN (SELECT BewerbID
FROM Veranstaltung_User
WHERE UserID IN (SELECT UserID
FROM Veranstaltung_User
WHERE BewerbID = 613)
AND UserID <> 0)
ORDER BY Datum
In Access, this query was quite fast – in mySQL it needs 44,56 seconds – too much 😉
how can I optimize my statement for faster quering? (mySQL > 5, tables ware MyISAM)
thanks for help
Two changes:
1. Uses JOINs instead of nested IN clauses
2. Uses
<= and >instead of theYEAR()functionThe advantage of change
1is that sometime IN performance can be worse than expected, and nesting them will exascerabate that.The advantage of change
2is that it allows an index seek to find a range of records. Where-as using YEAR() forcfes a scan of the entire table/index.Both of which assume you have appropriate indexes.