Create indexes - they are the reason why querying is fast. Without indexes, we would be stuck with CPU-only solutions.
So:
- Create index for SmallTable1(id)
- Create index for SmallTable2(id)
- Create index for LargeTable(id1) and LargeTable(id2)
Important: You can create index for more than one column at the same time, like this LargeTable(id1,id2) <--- DO NOT DO THAT because it does not make sense in your case.
Next, your query is not out of the box wrong, but it does not follow the best practice querying. Relational databases are based on Set theory. Therefore, you must think in terms of "bags with marbles" instead of "cells in a table".
Roughly, your initial query translates to:
- Get EVERYTHING from LargeTable c, SmallTable1 a and SmallTable2 b
- Now when you have all this information, find items where c.id1 = a.id AND c.id2 = b.id; (there goes your 5+ seconds because this is semi-resource intensive)
Ambrish has suggested the correct query, use that although this will not be faster.
Why? Because in the end, you still pull all the data from the table out of the database.
As for the data itself goes: 10 million records is not ridiculously large table, but it is not small either. In data warehouses, the star schema is a standard. And you have a star schema basically. The problem you are actually facing is that the result has to be calculated on-the-fly and that takes time. The reason i'm telling you this is because in corporate environments, engineers are facing this problems on a daily basis. And the solution is OLAP (basically pre-calculated, pre-aggregated, pre-summarized, pre-everything data). The end users then just query this precalculated data and the query seems very fast, but it is never 100% correct, because there is a delay between OLTP (on-line transactional processing = day to day database) and OLAP (on-line analytical processing = reporting database)
The indexes will help with queries such as WHERE id = 3 etc. But when you are cross joining and basically pulling everything from DB, it probably wouldn't play a significant role in your case.
So to make long story short: if your only options are queries, it will be hard to make an improvement.