0

I have two tables contains existing file names and downloaded file names. Theres 61k+ records on existing files table and 34k+ records on downloaded files table. Im using this query to find the file names which are not downloaded yet!

SELECT * FROM files WHERE filename <> '' AND filename NOT IN (SELECT filenameFROM downloads)

This was working fine when theres few records but its not working now, few days before, when there was 50k and 20k records, it was getting slow like getting result in 5/6mins, but now its showing this error:

Internal Server Error 500
No response from subprocess (php) with exit signal: 0

filename fields are file name fields(varchar 255) of tables and both fields are indexed. Any help plz?

1
  • Is this a database that you are hosting? Commented Jun 28, 2013 at 22:15

3 Answers 3

1

First, add indexes on files and downloads. This will make searches much faster. This might take a few minutes.

ALTER TABLE files ADD INDEX (filename); ALTER TABLE downloads ADD INDEX (filename); 

Then, use a LEFT JOIN instead of a subquery.

SELECT f.* FROM files f LEFT JOIN downloads d ON d.filename = f.filename WHERE d.filename IS NULL AND f.filename <> '' 

After these changes, the search should take less than a second.

Sign up to request clarification or add additional context in comments.

2 Comments

Upvoting here because this is only answer that mentions indexed, which is likely at the root of OP's issues.
Dont know why but still this isn't working for me, i checked on phpmyadmin, this query showing 500 error after about 10minutes, may for 60k+ record on both tables???
0

A better way to write the query would be this (assuming you have an id column):

SELECT a.* FROM files a LEFT JOIN downloads b ON b.filename = a.filename WHERE b.id IS NULL AND a.filename != '' 

The error is likely coming back due to a timeout in the PHP script. If this query still doesn't run quickly enough, please post the output of the above query with EXPLAIN at the beginning so we can see what MySQL is doing.

Comments

0

It would be better to left_join 2 tables, so, we take all records from files table and add to them appropriate records from downloads. Filenames from files which have null as a pair from downloads are the ones we need, so, we filter to get only such ones.

select f.filename from files as f left join downloads as d on f.filename=d.filename where d.filename is null and f.filename<>'' 

My implementation is just 2 simple scans:

mysql> explain select f.filename from files as f left join downloads as d on f.filename=d.filename where d.filename is null and f.filename<>''; SIMPLE f Using where; Using index; SIMPLE d Using where; Using index; Not exists 

Original one uses subquery:

mysql> explain SELECT * FROM files WHERE filename <> '' AND filename NOT IN (SELECT filename FROM downloads); PRIMARY files DEPENDENT SUBQUERY downloads 

1 Comment

Please refrain from code-only answers, and please include some explanation as to why your answer solves the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.