I have two tables. Each one is populated with about 50K records. Both have one common field. I want the records of both tables where the common field is matching. So I use the following SQL code. The problem is that the query is running now for already 20 minutes and nothing happening. It seems strange to me that it is taking so much time. I wonder if there is a better way to achieve my target. Thank you in advance for your replies.
My SQL code:
Select * from tableOne T1, tableTwo T2
Where T1.name = T2.name
By the way I am open to php solutions (loops or whatever if it is better…)
Two things:
You need to make sure there is an index on the name column in both tables. Otherwise the query will have to essentially scan every row of each table looking for matches.
Ideally, the index should have “included columns” that you want to select in the query. Otherwise the query won’t be able to fully use the index, but will have to go back to the table and pick up the other columns manually for each row. This means you should consider selecting only the columns you need (rather than *) in the query, and adding those columns as included columns to the index.