Last active
February 16, 2016 10:47
-
-
Save ievgrafov/e4750f67d90d465848b1 to your computer and use it in GitHub Desktop.
Difference between IN and ANY + array in Postgres (this difference shows itself only with several IN, one IN works just as ANY + array)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Запрос через IN: | |
SELECT "documents".* FROM "documents" | |
WHERE "documents"."archived_at" IS NULL | |
AND ( | |
contractor_id IN (SELECT id FROM companies WHERE lower(COALESCE(companies.title, '')) ~* 'основ') | |
OR stock_to_id IN (SELECT id FROM stocks WHERE lower(COALESCE(stocks.title, '')) ~* 'основ') | |
OR stock_from_id IN (SELECT id FROM stocks WHERE lower(COALESCE(stocks.title, '')) ~* 'основ') | |
); | |
EXPLAIN ANALYZE: | |
>> Append (cost=135.96..2696.11 rows=3680 width=291) (actual time=4.539..52.993 rows=1940 loops=1) | |
ActiveRecord::Base.connection.execute: | |
(65.2ms) | |
Запрос через array::integer[]: | |
SELECT "documents".* FROM "documents" | |
WHERE "documents"."archived_at" IS NULL | |
AND ( | |
contractor_id = ANY(( | |
select array( | |
SELECT id FROM companies WHERE lower(COALESCE(companies.title, '')) ~* 'основ' | |
) | |
)::integer[]) | |
OR stock_from_id = ANY(( | |
select array( | |
SELECT id FROM stocks WHERE lower(COALESCE(stocks.title, '')) ~* 'основ' | |
) | |
)::integer[]) | |
OR stock_to_id = ANY(( | |
select array( | |
SELECT id FROM stocks WHERE lower(COALESCE(stocks.title, '')) ~* 'основ' | |
) | |
)::integer[]) | |
); | |
EXPLAIN ANALYZE: | |
>> Append (cost=135.98..827.82 rows=1444 width=189) (actual time=4.219..11.064 rows=1940 loops=1) | |
ActiveRecord::Base.connection.execute: | |
(23.2ms) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment