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?
1 comment thread