I'm performing a query that is looking for values in one table that are not in another. For example:
SELECT id FROM table1 WHERE id NOT IN (SELECT id FROM table2);
Both tables have about 1 million rows but only a few hundred that do not match values which are returned in the result set. The query takes about 35 seconds. Doing a show profile on the query shows that mysql is spending most of the time in the state of "preparing". Any ideas on how I can optimize this state or what is actually happening during "preparing"?
The id value in both tables is indexed and of the same type and size.
The entire profile of the query is:
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| (initialization) | 0 |
| checking query cache for query | 0 |
| Opening tables | 0.13 |
| System lock | 0 |
| Table lock | 0 |
| init | 0.01 |
| optimizing | 0 |
| statistics | 0 |
| preparing | 0 |
| executing | 0 |
| Sending data | 0 |
| optimizing | 0 |
| statistics | 0 |
| preparing | 34.83 |
| end | 0 |
| query end | 0 |
| freeing items | 0 |
| closing tables | 0 |
| logging slow query | 0 |
+--------------------------------+----------+
Any tips are appreciated.
Thanks.