Ok, so I have created the following php script to populate the tables with some random data
Code:
$seed = 100;
srand($seed);
// Fill users table with 1000 users
for ($i = 0; $i < 1000; $i++) {
$name = rand(0, 100000);
$sql = "INSERT INTO users (name) VALUES ('$name')";
mysql_query($sql, $con);
}
// Fill transactions table with 200k transactions
for ($i = 0; $i < 200000; $i++) {
$buyerid = rand(0, 999);
while (($sellerid = rand(0, 999)) == $buyerid);
$price = rand(1, 100);
$sql = "INSERT INTO transactions (buyerid, sellerid, price) ";
$sql .= "VALUES ($buyerid, $sellerid, $price)";
mysql_query($sql, $con);
}
Now executing the SELECT statement with two JOINs returns buyer=72396 (corresponding to userid=148) and seller=88733 (corresponding to userid=184) pretty instantaneously.
And EXPLAIN gives an output as per screenshot.
Based on the speed of returning the result I would think MySQL is optimizing.
And, then I would assume any other respectable database system would do the same.
Bookmarks