php - Using indexes correctly in SQL query
I'm kind of new to using SQL and I am trying to run a query comparing two values in two separate tables. I have indexed the columns that are being used, but I am not entirely sure how indexing works. I am embedding it in PHP and it currently is taking a long time (sometimes even timing out) when I try to run the query. The two tables have about 250k rows in each so it shouldn't be too big of a query. Here is my code:
$users1 = mysql_num_rows(mysql_query("SELECT DISTINCT idtracker.uuid
FROM idtracker, download_tracker
WHERE idtracker.uuid = download_tracker.pluginId
&& idtracker.date > (NOW() - (2000000))", $con));
I am not sure if it is very efficient comparing two rows directly like I am doing here. If I could just get some insight on what I could do to improve the speed of this query it would be very helpful.
Thanks -Ryan
Answer
Solution:
Before you can do any real testing / optimizing, you need to fix the query:
is wrong.
If your
idtracker.date
is of typeDATE
orDATETIME
and you want to compare it to another date (NOW()
minus something), you need to use the mysqlfunction or something similar.