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?

Parent

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 moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (7 comments)
Post
+4
−0

is_excluded = 1 is very different from do_not_touch != 1. Whenever possible, try to structure your data and queries so that you can do an equi-join - that is, compare things using an = comparison. > and < and != can be really bad because the database will at the very least have to do an index scan, if there's an appropriate index available - if not, full table scan, baby! Wooo! If you can use do_not_touch = 0 that would be nice, but I obviously have no idea what that field contains.

Also, it helps to have an appropriate index. For your second query

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

it would seem that an index on example_table(textfield, do_not_touch) would perhaps be helpful.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (1 comment)
General comments
Jack Douglas‭ wrote over 3 years ago · edited over 3 years ago

An index on textfield, do_not_touch will be less efficient than one on just textfield because the values in do_not_touch are heavily skewed — adding it to the index just means more IO when scaning.