Joining 2 Tables with 1 Million rows

cyrnsnpdr's profile image cyrnsnpdr posted 10 months ago in Running SQL scripts Permalink

Hello, can someone recommend a query for Joining 2 Tables with 1 Million rows, I used LEFT JOIN Clause but it took me 7 hours to execute or output the query and I used JOIN Clause and which took me 4 Hours and 30 mins to execute or output. Is there any query that can handle a million rows but will take just about maximum 15mins? Thank you!

TTSneko's profile image TTSneko posted 10 months ago Permalink

Of course the runtime (and results!) of both JOIN and LEFT JOIN differ, the question is which result you actually want(ed). You do know that various factors influence processing, not only "the query"? A million lines is nothing; however caching on a HDD instead of an SSD, not enough allocated RAM, an extremely wide array or the nonsensical use of included binary objects will of course slow down processing - and those are only a few general factors. Even bad housekeeping (indices not cleaned up, etc.) may negatively influence runtime operations, just like using a front-end (here: Heidi) instead of working via CLI.

So no, there is no "magic" query. It is up to you to know what you are doing and provide a fitting surrounding as to achieve your goal.

ansgar's profile image ansgar posted 10 months ago Permalink

I think a common mistake in LEFT JOIN's is that users leave out or forget an important ON clause. That can result in a maximum of row_count_table * row_count_joined_table. So if both tables contain 1 million rows, you get 1 mio * 1 mio = 1 trillion rows, and the server requires a long time to calculate and return these.

Please login to leave a reply, or register at first.