Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on Why would excluding records by creating a temporary table of their primary keys be faster than simply excluding by value?

Post

Why would excluding records by creating a temporary table of their primary keys be faster than simply excluding by value?

+11
−1

I have two tables with millions of records. Every so often I need to join them and exclude just a handful of records where a bit(1) column is set to 1 instead of 0.

I can do it with either,

WHERE is_excluded !=1

or

WHERE example_table.pk NOT IN
(
  SELECT pk FROM(
    SELECT pk FROM
    example_table
    WHERE is_excluded =1) 
AS t)

For example

UPDATE example_table
SET textfield = 'X'
WHERE textfield = 'Y'
and pk not in (SELECT pk FROM (SELECT pk FROM example_table WHERE do_not_touch =1)as t) ;

is faster than

UPDATE example_table
SET textfield = 'X'
WHERE textfield = 'Y'
and do_not_touch !=1

The second way is sometimes way faster, even though it takes much longer to write out.

Why would the second way be faster?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (7 comments)
General comments
Ringi‭ wrote over 4 years ago

Please show the complete queries you are doing and the query plans for each query. Along with details of any index.

manassehkatz‭ wrote over 4 years ago

Use EXPLAIN to get some more details and (unless that results in a quick solution to the problem) post that information here. https://dev.mysql.com/doc/refman/8.0/en/using-explain.html

manassehkatz‭ wrote over 4 years ago

Are the results different if you use is_excluded = 0 instead of "!= 1"?

Charlie Brumbaugh‭ wrote over 4 years ago

@manassehkatz The query cost is slightly different but the results are not

ArtOfCode‭ wrote over 4 years ago

Why the double-select? Why not just a single subquery, as in "where not in (select where is_excluded = 1)"?

Charlie Brumbaugh‭ wrote over 4 years ago

@ArtofCode sometimes when doing sub queries like this it has to have a unique alias and it's easier to just always do the double select and copy paste that

Jack Douglas‭ wrote over 4 years ago

"Every so often I need to join…" — none of your examples have any joins.