Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use IN and JOIN instead of NOT IN and simple subselect #347

Merged
merged 1 commit into from
Feb 17, 2015

Conversation

butonic
Copy link
Member

@butonic butonic commented Feb 17, 2015

The background job keeps burning CPU cycles on my poor ATOM board...

Before:

# EXPLAIN DELETE FROM "oc_music_tracks" WHERE "file_id" NOT IN (SELECT "fileid" FROM "oc_filecache");
                                      QUERY PLAN                                       
---------------------------------------------------------------------------------------
 Delete on oc_music_tracks  (cost=0.00..33871909.50 rows=13900 width=6)
   ->  Seq Scan on oc_music_tracks  (cost=0.00..33871909.50 rows=13900 width=6)
         Filter: (NOT (SubPlan 1))
         SubPlan 1
           ->  Materialize  (cost=0.00..2331.24 rows=42216 width=4)
                 ->  Seq Scan on oc_filecache  (cost=0.00..1955.16 rows=42216 width=4)

after

# EXPLAIN DELETE FROM "oc_music_tracks" WHERE "file_id" IN (SELECT "file_id" FROM "oc_music_tracks" LEFT JOIN "oc_filecache" ON "file_id"="fileid" WHERE "fileid" IS NULL);
                                                 QUERY PLAN                                                  
-------------------------------------------------------------------------------------------------------------
 Delete on oc_music_tracks  (cost=4010.11..4018.41 rows=1 width=18)
   ->  Nested Loop  (cost=4010.11..4018.41 rows=1 width=18)
         ->  HashAggregate  (cost=4010.11..4010.12 rows=1 width=16)
               ->  Hash Anti Join  (cost=2689.86..4010.11 rows=1 width=16)
                     Hash Cond: (public.oc_music_tracks.file_id = oc_filecache.fileid)
                     ->  Seq Scan on oc_music_tracks  (cost=0.00..598.00 rows=27800 width=10)
                     ->  Hash  (cost=1955.16..1955.16 rows=42216 width=10)
                           ->  Seq Scan on oc_filecache  (cost=0.00..1955.16 rows=42216 width=10)
         ->  Index Scan using music_tracks_file_id_idx on oc_music_tracks  (cost=0.00..8.27 rows=1 width=10)
               Index Cond: (file_id = public.oc_music_tracks.file_id)

@MorrisJobke ping me if you want me to explain the SQL changes. I left the other three deletes as an exercise for you ... and because I need to sleep to get better.

ref #90

@scrutinizer-notifier
Copy link

The inspection completed: No new issues

@MorrisJobke
Copy link
Contributor

👍

MorrisJobke added a commit that referenced this pull request Feb 17, 2015
use IN and JOIN instead of NOT IN and simple subselect
@MorrisJobke MorrisJobke merged commit 46ee18d into master Feb 17, 2015
@MorrisJobke MorrisJobke deleted the speedup_cleanup branch February 17, 2015 21:53
@ghost
Copy link

ghost commented Mar 11, 2015

@butonic
Could it be possible that this also fix #289 ?

@butonic
Copy link
Member Author

butonic commented Mar 11, 2015

It avoids the subselects, so I'd say yes, but I am pretty tired and might be wrong...

@ghost
Copy link

ghost commented Mar 11, 2015

Thanks for the follow-up. 👍 Will have a look after the changes here was included in the next version of the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants